home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / ed8n1t2i / led.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.8 KB  |  122 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // LED.java              v 1.00 b1
  5. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  6. // URL:                  http://www/bart.nl/~ivr
  7. // Initial release:
  8. // Released in public domain:
  9. //
  10. // ---- Description ----
  11. // Java class containing methods for a LED
  12. //
  13. // This program and the Java source is in the public domain.
  14. // Permission to use, copy, modify, and distribute this software
  15. // and its documentation for NON-COMMERCIAL purposes and
  16. // without fee is hereby granted.
  17. //
  18. //    Copyright 1996
  19. //
  20. //    Iwan van Rienen
  21. //    Joan Maetsuyckerstr. 145
  22. //    2593 ZG  The Hague
  23. //    The Netherlands
  24. //
  25. // I am not responsible for any bugs in this program and
  26. // possible damage to hard- or software when using this program.
  27. //****************************************************************************
  28. import java.applet.Applet;
  29. import java.awt.*;
  30. import java.util.Vector;
  31. import java.io.PrintStream;
  32.  
  33. class LED extends ElectronicComponent {
  34.     protected Color LEDColorOn;
  35.     final static Color LEDColorOff = Color.gray;
  36.  
  37. //----------------------------------------------------------------------------
  38. // The constructor of a new LED
  39. //----------------------------------------------------------------------------
  40.     public LED (Pin PinGrid[][], Color MyColor, int x, int y) {
  41.         super (x, y, 9, 4, 4, 1, 2, 2, 2, 0);  // x,y,w,h HitBox x,y,w,h,I,O
  42.         LEDColorOn = MyColor;
  43.         ComponentName = "LED";
  44.         IPin[0] = new InputPin("Anode", 1, 2, 3, 0, 0, 0, ComponentPin.PIN_TEXT_INVISIBLE); // name, x, y, w, h, flags
  45.         IPin[1] = new InputPin("Kathode", 8, 2, -2, 0, 0, 0, ComponentPin.PIN_TEXT_INVISIBLE); // name, x, y, w, h, flags
  46.         RegisterPins (PinGrid, x, y);
  47.     }
  48.  
  49. //----------------------------------------------------------------------------
  50. // The constructor of a new LED, which is a copy of CompToCopy
  51. //----------------------------------------------------------------------------
  52.     public LED (ElectronicComponent CompToCopy, int xo, int yo) {
  53.         super (CompToCopy, xo, yo);
  54.     }
  55.  
  56. //----------------------------------------------------------------------------
  57. // Method for copying this component.
  58. //----------------------------------------------------------------------------
  59.     public ElectronicComponent Copy(int xo, int yo) {
  60.         LED NewComponent = new LED(this, xo, yo);
  61.         NewComponent.LEDColorOn = LEDColorOn;
  62.         return NewComponent;
  63.     }
  64.  
  65. //----------------------------------------------------------------------------
  66. // Method for drawing this LED.
  67. //----------------------------------------------------------------------------
  68.     public void draw(Graphics g, int xp, int yp, int gs) {
  69.         super.draw (g, xp, yp, gs);
  70.         int x = Pos.x - xp;
  71.         int y = Pos.y - yp;
  72.  
  73.         // Draw HitBox and Dimension
  74.         // DrawHitBox(g, x, y, gs);
  75.  
  76.         // Draw all I Pins
  77.         DrawInputPins(g, x, y, gs);
  78.  
  79.         // Draw plus
  80.         g.setColor (Color.red);
  81.         g.drawLine ((x + 3) * gs - gs / 4, (y + 2) * gs + gs / 2, (x + 3) * gs + gs / 4, (y + 2) * gs + gs / 2);
  82.         g.drawLine ((x + 3)  * gs , (y + 2) * gs + gs / 4, (x + 3) * gs, (y + 3) * gs - gs / 4);
  83.         // Draw body
  84.         if (IPin[0].getLevel() == 5 && IPin[1].getLevel() == 0) {
  85.             g.setColor (LEDColorOn);
  86.         } else {
  87.             g.setColor (LEDColorOff);
  88.         }
  89.         g.fillOval ((x + 4) * gs, (y + 1) * gs, 2 * gs + 1, 2 * gs + 1);
  90.         g.setColor (LEDColorOn);
  91.         g.drawOval ((x + 4) * gs, (y + 1) * gs, 2 * gs, 2 * gs);
  92.     }
  93.  
  94. //----------------------------------------------------------------------------
  95. // Save this LED
  96. //----------------------------------------------------------------------------
  97.     public void Save (PrintStream myPrintStream) {
  98.         myPrintStream.println ("describe component LED");
  99.         myPrintStream.println (" pos " + Pos.x + " " + Pos.y);
  100.         if (LEDColorOn == Color.red) {
  101.             myPrintStream.println (" color red");
  102.         } else if (LEDColorOn == Color.green) {
  103.             myPrintStream.println (" color green");
  104.         } else if (LEDColorOn == Color.yellow) {
  105.             myPrintStream.println (" color yellow");
  106.         }
  107.  
  108.         myPrintStream.println ("end describe");
  109.     }
  110.  
  111. //----------------------------------------------------------------------------
  112. // Initialize the input pins of this LED, preventing the LED is lightning
  113. // when there's no power.
  114. //----------------------------------------------------------------------------
  115.     public void InitBeforeSimulate() {
  116.         IPin[0].InitBeforeSimulate();
  117.         IPin[1].InitBeforeSimulate();
  118.         IPin[0].setLevel (-1);
  119.         IPin[1].setLevel (-1);
  120.     }
  121.  
  122. }