home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / ED8N1T2I.ZIP / ED8N1T2I / education / ED8N1T2I / ComponentPin.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  10.9 KB  |  228 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // ComponentPin.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 Component Pin
  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.lang.InterruptedException;
  31. import java.lang.Integer;
  32. import java.lang.Math;
  33. import java.util.Vector;
  34. import java.io.StreamTokenizer;
  35. import java.io.InputStream;
  36. import java.io.IOException;
  37. import java.net.URL;
  38.  
  39. class ComponentPin {
  40.     protected Vector ConnComps;   // Connected components to this pin
  41.     protected int Level = 0;     // level of this pin.
  42.     protected int OldLevel = 0;  // Old level of this pin.
  43.     protected Point PinPos;       // Position of this pin relative to component
  44.     protected Dimension PinDim;   // Dimension of this pin
  45.     protected String Name;        // Name of this pin
  46.     protected int Flags;          // Flags of this pin
  47.     protected int TextXoffs;      // Text x offset
  48.     protected int TextYoffs;      // Text y offset
  49.     static final Color PinColor = Color.blue;
  50.     static final Color PinTextColor = Color.white;
  51.     static final int PIN_NORMAL = 0x0000;
  52.     static final int PIN_NEGATIVE = 0x0001;
  53.     static final int PIN_TEXT_INVISIBLE = 0x0002;
  54.     static final int PIN_EDGETRIGGERED = 0x0004;
  55.     static final int PIN_NOACTION = 0x0008;
  56.     public Font PinFont;
  57.     public FontMetrics PinFontMetrics;
  58.  
  59. //----------------------------------------------------------------------------
  60. // The constructor of a new component pin
  61. //----------------------------------------------------------------------------
  62.     public ComponentPin (ComponentPin PinToCopy) {
  63.         Name = PinToCopy.Name;
  64.         PinPos = new Point (PinToCopy.PinPos.x, PinToCopy.PinPos.y);
  65.         PinDim = new Dimension (PinToCopy.PinDim.width, PinToCopy.PinDim.height);
  66.         Flags = PinToCopy.Flags;
  67.         TextXoffs = PinToCopy.TextXoffs;
  68.         TextYoffs = PinToCopy.TextYoffs;
  69.         PinFont = new Font("TimesRoman",Font.PLAIN, 10);
  70.     }
  71.  
  72. //----------------------------------------------------------------------------
  73. // The constructor of a new component pin with the specified variables
  74. //----------------------------------------------------------------------------
  75.     public ComponentPin (String name, int x, int y, int w, int h, int txo, int tyo, int fl) {
  76.         Name = name;
  77.         TextXoffs = txo;
  78.         TextYoffs = tyo;
  79.         PinPos = new Point(x, y);
  80.         PinDim = new Dimension (w, h);
  81.         Flags = fl;
  82.         PinFont = new Font("TimesRoman",Font.PLAIN, 10);
  83.     }
  84.  
  85. //----------------------------------------------------------------------------
  86. // Draw this component pin
  87. //----------------------------------------------------------------------------
  88.     public void draw (Graphics g, int x, int y, int gs) {
  89.         double offset = 0;
  90.         int TextWidth, TextHeight;
  91.         g.setColor (PinColor);
  92.         if ((Flags & PIN_NEGATIVE) > 0) {
  93.             if (PinDim.width > 0) { // pin is left of component
  94.                 g.drawLine ((x + PinPos.x) * gs, (y + PinPos.y) * gs,
  95.                             (x + PinPos.x + PinDim.width - 1) * gs,
  96.                             (y + PinPos.y + PinDim.height) * gs);
  97.                             g.setColor (ElectronicComponent.ComponentColor);
  98.                 g.drawOval ((int)((x + PinPos.x + PinDim.width - 0.875) * gs),
  99.                             (int)((y + PinPos.y - 0.375) * gs), (int)(gs * 0.75), (int)(gs * 0.75));
  100.             } else {                // pin is right of component
  101.                 g.drawLine ((x + PinPos.x) * gs, (y + PinPos.y) * gs,
  102.                             (x + PinPos.x + PinDim.width + 1) * gs,
  103.                             (y + PinPos.y + PinDim.height) * gs);
  104.                             g.setColor (ElectronicComponent.ComponentColor);
  105.                 g.drawOval ((int)((x + PinPos.x + PinDim.width) * gs + 1),
  106.                             (int)((y + PinPos.y - 0.375) * gs), (int)(gs * 0.75), (int)(gs * 0.75));
  107.             }
  108.         } else {
  109.             g.drawLine ((x + PinPos.x) * gs, (y + PinPos.y) * gs,
  110.                         (x + PinPos.x + PinDim.width) * gs,
  111.                         (y + PinPos.y + PinDim.height) * gs);
  112.         }
  113.  
  114.         if ((Flags & PIN_EDGETRIGGERED) > 0) {  // Draw >
  115.             offset = 0.5;
  116.             g.setColor (ElectronicComponent.ComponentColor);
  117.             if (PinDim.width > 0) {     // pin is left of component
  118.                 g.drawLine ((int)((x + PinPos.x + PinDim.width) * gs), (int)((y + PinPos.y - 0.5) * gs),
  119.                             (int)((x + PinPos.x + PinDim.width + 0.5) * gs), (int)((y + PinPos.y) * gs) );
  120.                 g.drawLine ((int)((x + PinPos.x + PinDim.width) * gs), (int)((y + PinPos.y + 0.5) * gs),
  121.                             (int)((x + PinPos.x + PinDim.width + 0.5) * gs), (int)((y + PinPos.y) * gs) );
  122.             } else {                    // pin is right of component
  123.                 g.drawLine ((int)((x + PinPos.x + PinDim.width) * gs), (int)((y + PinPos.y - 0.5) * gs),
  124.                             (int)((x + PinPos.x + PinDim.width - 0.5) * gs), (int)((y + PinPos.y) * gs) );
  125.                 g.drawLine ((int)((x + PinPos.x + PinDim.width) * gs), (int)((y + PinPos.y + 0.5) * gs),
  126.                             (int)((x + PinPos.x + PinDim.width - 0.5) * gs), (int)((y + PinPos.y) * gs) );
  127.  
  128.             }
  129.         }
  130.  
  131.         if ((Flags & PIN_TEXT_INVISIBLE) == 0) {
  132.             PinFontMetrics = g.getFontMetrics(PinFont);
  133.             TextWidth = PinFontMetrics.stringWidth(Name);
  134.             TextHeight = PinFontMetrics.getHeight();
  135.             g.setColor (PinTextColor);
  136.             g.setFont (PinFont);
  137.             if (PinDim.width > 0) {     // pin is left of component
  138.                 g.drawString (Name, (int)((x + PinPos.x + PinDim.width + offset + TextXoffs) * gs + 2),
  139.                                     (int)((y + PinPos.y + 0.5 + TextYoffs) * gs));
  140.                 if ((Flags & PIN_NEGATIVE) > 0) { // Draw _ above Name
  141.                     g.drawLine ( (int)((x + PinPos.x + PinDim.width + offset + TextXoffs) * gs + 2),
  142.                                  (int)((y + PinPos.y - 0.5 + TextYoffs) * gs ),
  143.                                  (int)((x + PinPos.x + PinDim.width + offset + TextXoffs) * gs + TextWidth ),
  144.                                  (int)((y + PinPos.y - 0.5 + TextYoffs) * gs) );
  145.                 }
  146.             } else if (PinDim.width < 0) {  // pin is right of component
  147.                 g.drawString (Name, (int)((x + PinPos.x + PinDim.width - offset + TextXoffs) * gs - TextWidth - 1),
  148.                                     (int)((y + PinPos.y + 0.5 + TextYoffs) * gs));
  149.                 if ((Flags & PIN_NEGATIVE) > 0) { // Draw _ above Name
  150.                     g.drawLine ( (int)((x + PinPos.x + PinDim.width - offset + TextXoffs) * gs - TextWidth - 1),
  151.                                  (int)((y + PinPos.y - 0.5 + TextYoffs) * gs ),
  152.                                  (int)((x + PinPos.x + PinDim.width - offset + TextXoffs ) * gs ),
  153.                                  (int)((y + PinPos.y - 0.5 + TextYoffs) * gs));
  154.                 }
  155.             } else if (PinDim.height < 0) {  // pin is under component
  156.                 g.drawString (Name, (int)((x + PinPos.x + TextXoffs) * gs - TextWidth / 2  ),
  157.                                     (int)((y + PinPos.y + PinDim.height + TextYoffs) * gs));
  158.                 if ((Flags & PIN_NEGATIVE) > 0) { // Draw _ above Name
  159.                     g.drawLine ( (int)((x + PinPos.x + PinDim.width - offset - 1 + TextXoffs) * gs),
  160.                                  (int)((y + PinPos.y - 0.5 + TextYoffs) * gs - 1),
  161.                                  (int)((x + PinPos.x + PinDim.width - offset + TextXoffs) * gs ),
  162.                                  (int)((y + PinPos.y - 0.5 + TextYoffs) * gs) - 1);
  163.                 }
  164.             } else if (PinDim.height > 0) {  // pin is above component
  165.                 g.drawString (Name, (int)((x + PinPos.x + TextXoffs) * gs - TextWidth / 2  ),
  166.                                     (int)((y + PinPos.y + PinDim.height + TextYoffs - 1) * gs));
  167.                 if ((Flags & PIN_NEGATIVE) > 0) { // Draw _ above Name
  168.                     g.drawLine ( (int)((x + PinPos.x + PinDim.width - offset - 1 + TextXoffs) * gs),
  169.                                  (int)((y + PinPos.y - 0.5 + TextYoffs) * gs - 1),
  170.                                  (int)((x + PinPos.x + PinDim.width - offset + TextXoffs) * gs ),
  171.                                  (int)((y + PinPos.y - 0.5 + TextYoffs) * gs) - 1);
  172.                 }
  173.             }
  174.         }
  175.     }
  176.  
  177. //----------------------------------------------------------------------------
  178. // Get the level of this pin
  179. //----------------------------------------------------------------------------
  180.     public int getLevel() {
  181.         if ((Flags & PIN_NEGATIVE) == 0) {
  182.             return Level;
  183.         } else {
  184.             if (Level == 0) {
  185.                 return 5;
  186.             } else {
  187.                 return 0;
  188.             }
  189.         }
  190.     }
  191.  
  192. //----------------------------------------------------------------------------
  193. // Get the old level of this pin
  194. //----------------------------------------------------------------------------
  195.     public int getOldLevel() {
  196.         if ((Flags & PIN_NEGATIVE) == 0) {
  197.             return OldLevel;
  198.         } else {
  199.             if (OldLevel == 0) {
  200.                 return 5;
  201.             } else {
  202.                 return 0;
  203.             }
  204.         }
  205.     }
  206.  
  207. //----------------------------------------------------------------------------
  208. // Set the level of this pin
  209. //----------------------------------------------------------------------------
  210.     public void setLevel(int l) {
  211.         OldLevel = Level;
  212.         Level = l;
  213.     }
  214.  
  215. //----------------------------------------------------------------------------
  216. // Get the name of this pin
  217. //----------------------------------------------------------------------------
  218.     public String getName() {
  219.         return Name;
  220.     }
  221.  
  222. //----------------------------------------------------------------------------
  223. // Set the name of this pin
  224. //----------------------------------------------------------------------------
  225.     public void setName(String n) {
  226.         Name = n;
  227.     }
  228. }