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

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // SevenSegementDisplay.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 7-segment display with common kathode.
  12. // Segment description:
  13. //       a
  14. //      ----
  15. //   f |    | b
  16. //     | g  |
  17. //      ----
  18. //   e |    | c
  19. //     |    |
  20. //      ---- .
  21. //       d
  22. //
  23. // This program and the Java source is in the public domain.
  24. // Permission to use, copy, modify, and distribute this software
  25. // and its documentation for NON-COMMERCIAL purposes and
  26. // without fee is hereby granted.
  27. //
  28. //    Copyright 1996
  29. //
  30. //    Iwan van Rienen
  31. //    Joan Maetsuyckerstr. 145
  32. //    2593 ZG  The Hague
  33. //    The Netherlands
  34. //
  35. // I am not responsible for any bugs in this program and
  36. // possible damage to hard- or software when using this program.
  37. //****************************************************************************
  38. import java.applet.Applet;
  39. import java.awt.*;
  40. import java.util.Vector;
  41. import java.io.PrintStream;
  42.  
  43. class SevenSegmentDisplay extends IntegratedCircuit {
  44.     protected Color LEDColorOn;
  45.     final static Color LEDColorOff = Color.darkGray;
  46.     static final int seg_a = 0;
  47.     static final int seg_b = 1;
  48.     static final int seg_c = 2;
  49.     static final int seg_d = 3;
  50.     static final int seg_e = 4;
  51.     static final int seg_f = 5;
  52.     static final int seg_g = 6;
  53.     static final int seg_dp = 7;
  54.     static final int ck = 8;
  55.  
  56. //----------------------------------------------------------------------------
  57. // The constructor of a new 7-segment display
  58. //----------------------------------------------------------------------------
  59.     public SevenSegmentDisplay(Pin PinGrid[][], Color MyColor, int x, int y) {
  60.         super (x, y, 12, 12, 3, 1, 6, 8, 9, 0);      // x,y,w,h HitBox x,y,w,h,I,O
  61.         LEDColorOn = MyColor;
  62.         IPin[seg_a] = new InputPin("a", 1, 2, 2, 0, 0, 0, ComponentPin.PIN_NORMAL); // name, x, y, w, h, inv
  63.         IPin[seg_b] = new InputPin("b", 1, 3, 2, 0, 0, 0, ComponentPin.PIN_NORMAL); // name, x, y, w, h, inv
  64.         IPin[seg_c] = new InputPin("c", 1, 4, 2, 0, 0, 0, ComponentPin.PIN_NORMAL); // name, x, y, w, h, inv
  65.         IPin[seg_d] = new InputPin("d", 1, 5, 2, 0, 0, 0, ComponentPin.PIN_NORMAL); // name, x, y, w, h, inv
  66.         IPin[seg_e] = new InputPin("e", 1, 6, 2, 0, 0, 0, ComponentPin.PIN_NORMAL); // name, x, y, w, h, inv
  67.         IPin[seg_f] = new InputPin("f", 1, 7, 2, 0, 0, 0, ComponentPin.PIN_NORMAL); // name, x, y, w, h, inv
  68.         IPin[seg_g] = new InputPin("g", 1, 8, 2, 0, 0, 0, ComponentPin.PIN_NORMAL); // name, x, y, w, h, inv
  69.         IPin[seg_dp] = new InputPin("dp", 11, 8, -2, 0, 0, 0, ComponentPin.PIN_TEXT_INVISIBLE); // name, x, y, w, h, inv
  70.         IPin[ck] = new InputPin("CK", 6, 11, 0, -2, 0, 0, ComponentPin.PIN_NORMAL); // name, x, y, w, h, inv
  71.         ComponentName = "7-segment display";
  72.         ClassName = "SevenSegmentDisplay";
  73.         RegisterPins (PinGrid, x, y);
  74.     }
  75.  
  76. //----------------------------------------------------------------------------
  77. // The constructor of a new 7-seg display, which is a copy of CompToCopy
  78. //----------------------------------------------------------------------------
  79.     public SevenSegmentDisplay (ElectronicComponent CompToCopy, int xo, int yo) {
  80.         super (CompToCopy, xo, yo);
  81.     }
  82.  
  83. //----------------------------------------------------------------------------
  84. // Method for copying this component.
  85. //----------------------------------------------------------------------------
  86.     public ElectronicComponent Copy(int xo, int yo) {
  87.         SevenSegmentDisplay NewComponent = new SevenSegmentDisplay(this, xo, yo);
  88.         NewComponent.LEDColorOn = LEDColorOn;
  89.         return NewComponent;
  90.     }
  91.  
  92. //----------------------------------------------------------------------------
  93. // Here is the code that's simulating the component
  94. // It's empty because this is not an active component
  95. //----------------------------------------------------------------------------
  96.     public void SimulateLogic() {
  97.     }
  98.  
  99. //----------------------------------------------------------------------------
  100. // Save this 7-segment display
  101. //----------------------------------------------------------------------------
  102.     public void Save (PrintStream myPrintStream) {
  103.         myPrintStream.println ("describe component SevenSegmentDisplay");
  104.         myPrintStream.println (" pos " + Pos.x + " " + Pos.y);
  105.         if (LEDColorOn == Color.red) {
  106.             myPrintStream.println (" color red");
  107.         } else if (LEDColorOn == Color.green) {
  108.             myPrintStream.println (" color green");
  109.         } else if (LEDColorOn == Color.yellow) {
  110.             myPrintStream.println (" color yellow");
  111.         }
  112.  
  113.         myPrintStream.println ("end describe");
  114.     }
  115.  
  116. //----------------------------------------------------------------------------
  117. // Draw the display.
  118. //----------------------------------------------------------------------------
  119.     public void draw(Graphics g, int xp, int yp, int gs) {
  120.         int x = Pos.x - xp;
  121.         int y = Pos.y - yp;
  122.  
  123.         // clear background
  124.         g.setColor (Color.black);
  125.         g.fillRect ((x + HitBox.x) * gs, (y + HitBox.y) * gs, HitBoxSize.width * gs, HitBoxSize.height * gs);
  126.  
  127.         // Draw segment a
  128.         if (IPin[seg_a].getLevel() == 5 && IPin[ck].getLevel() == 0)  {
  129.             g.setColor (LEDColorOn);
  130.         } else {
  131.             g.setColor (LEDColorOff);
  132.         }
  133.         g.fillRect ( (int)((x + 4.5) * gs + 1), (int)((y + 1.25) * gs), (int)(gs * 2.75), gs / 2);
  134.  
  135.         // Draw segment b
  136.         if (IPin[seg_b].getLevel() == 5 && IPin[ck].getLevel() == 0)  {
  137.             g.setColor (LEDColorOn);
  138.         } else {
  139.             g.setColor (LEDColorOff);
  140.         }
  141.         g.fillRect ( (int)((x + 7.5) * gs), (int)((y + 1.5) * gs), gs / 2, gs * 3 - 1);
  142.  
  143.         // Draw segment c
  144.         if (IPin[seg_c].getLevel() == 5 && IPin[ck].getLevel() == 0)  {
  145.             g.setColor (LEDColorOn);
  146.         } else {
  147.             g.setColor (LEDColorOff);
  148.         }
  149.         g.fillRect ( (int)((x + 7.5) * gs), (int)((y + 4.5) * gs + 1), gs / 2, gs * 3 - 1);
  150.  
  151.         // Draw segment d
  152.         if (IPin[seg_d].getLevel() == 5 && IPin[ck].getLevel() == 0)  {
  153.             g.setColor (LEDColorOn);
  154.         } else {
  155.             g.setColor (LEDColorOff);
  156.         }
  157.         g.fillRect ( (int)((x + 4.5) * gs + 1), (int)((y + 7.25) * gs), (int)(gs * 2.75), gs / 2);
  158.  
  159.         // Draw segment e
  160.         if (IPin[seg_e].getLevel() == 5 && IPin[ck].getLevel() == 0)  {
  161.             g.setColor (LEDColorOn);
  162.         } else {
  163.             g.setColor (LEDColorOff);
  164.         }
  165.         g.fillRect ( (int)((x + 4) * gs), (int)((y + 4.5) * gs + 1), gs / 2, gs * 3 - 1);
  166.  
  167.         // Draw segment f
  168.         if (IPin[seg_f].getLevel() == 5 && IPin[ck].getLevel() == 0)  {
  169.             g.setColor (LEDColorOn);
  170.         } else {
  171.             g.setColor (LEDColorOff);
  172.         }
  173.         g.fillRect ( (int)((x + 4) * gs), (int)((y + 1.5) * gs), gs / 2, gs * 3 - 1);
  174.  
  175.         // Draw segment g
  176.         if (IPin[seg_g].getLevel() == 5 && IPin[ck].getLevel() == 0)  {
  177.             g.setColor (LEDColorOn);
  178.         } else {
  179.             g.setColor (LEDColorOff);
  180.         }
  181.         g.fillRect ( (int)((x + 4.5) * gs + 1), (int)((y + 4.25) * gs), (int)(gs * 2.75), gs / 2);
  182.  
  183.         // Draw dp
  184.         if (IPin[seg_dp].getLevel() == 5 && IPin[ck].getLevel() == 0)  {
  185.             g.setColor (LEDColorOn);
  186.         } else {
  187.             g.setColor (LEDColorOff);
  188.         }
  189.         g.fillOval ( (int)((x + 8) * gs + 1), (int)((y + 7.75) * gs), (int)(gs / 1.5 + 1), (int)(gs / 1.5 + 1));
  190.  
  191.         // Integrated Circuit draw pins and outline
  192.         super.draw (g, xp, yp, gs);
  193.     }
  194.  
  195. //----------------------------------------------------------------------------
  196. // Initialize all inputs before simulating.
  197. // It prevents the LEDs will continue light.
  198. //----------------------------------------------------------------------------
  199.     public void InitBeforeSimulate() {
  200.         for (int ix = 0; ix < 9 ; ix++) {
  201.             IPin[ix].InitBeforeSimulate();
  202.             IPin[ix].setLevel (-1);
  203.         }
  204.     }
  205.  
  206. }