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

  1. import java.applet.Applet;
  2. //****************************************************************************
  3. // ---- version information ----
  4. //
  5. // Switch.java           v 1.00 b1
  6. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  7. // URL:                  http://www/bart.nl/~ivr
  8. // Initial release:
  9. // Released in public domain:
  10. //
  11. // ---- Description ----
  12. // Java class containing methods for a Switch
  13. //
  14. // This program and the Java source is in the public domain.
  15. // Permission to use, copy, modify, and distribute this software
  16. // and its documentation for NON-COMMERCIAL purposes and
  17. // without fee is hereby granted.
  18. //
  19. //    Copyright 1996
  20. //
  21. //    Iwan van Rienen
  22. //    Joan Maetsuyckerstr. 145
  23. //    2593 ZG  The Hague
  24. //    The Netherlands
  25. //
  26. // I am not responsible for any bugs in this program and
  27. // possible damage to hard- or software when using this program.
  28. //****************************************************************************
  29. import java.awt.*;
  30. import java.util.Vector;
  31.  
  32. class Switch extends ElectronicComponent {
  33.     boolean SwitchClosed = false;
  34.     Vector ConnCompsA;  // Connected components to pin A
  35.     Vector ConnCompsB;  // Connected components to pin B
  36.     Vector ConnCompsQ;  // Connected components to pin Q
  37.     int ALevel = -1;
  38.     int BLevel = -1;
  39.     int QLevel = -1;
  40.  
  41. //----------------------------------------------------------------------------
  42. // The constructor of a new Switch
  43. //----------------------------------------------------------------------------
  44.     public Switch (Pin PinGrid[][], int x, int y) {
  45.         super (x, y, 10, 5, 3, 1, 4, 4, 0, 0);      // x,y,w,h HitBox x,y,w,h,I,O
  46.         ComponentName = "Switch";
  47.         ClassName = "Switch";
  48.         RegisterPins (PinGrid, x, y);
  49.     }
  50.  
  51. //----------------------------------------------------------------------------
  52. // The constructor of a new Switch, which is a copy of CompToCopy
  53. //----------------------------------------------------------------------------
  54.     public Switch (ElectronicComponent CompToCopy, int xo, int yo) {
  55.         super (CompToCopy, xo, yo);
  56.     }
  57.  
  58. //----------------------------------------------------------------------------
  59. // Method for copying this component.
  60. //----------------------------------------------------------------------------
  61.     public ElectronicComponent Copy(int xo, int yo) {
  62.         Switch NewComponent = new Switch(this, xo, yo);
  63.         return NewComponent;
  64.     }
  65.  
  66. //----------------------------------------------------------------------------
  67. // The user clicked this button. Toggle the position
  68. //----------------------------------------------------------------------------
  69.     public boolean SimMouseDown() {
  70.         SwitchClosed = !SwitchClosed;
  71.         return true;
  72.     }
  73.  
  74. //----------------------------------------------------------------------------
  75. // Inform all components at the specified position
  76. //----------------------------------------------------------------------------
  77.     public void InformComponents(int id, int ActLevel, Vector ActVector, int x, int y) {
  78.         ElectronicComponent ConnectedComponent;
  79.         for (int ix = 0; ix < ActVector.size(); ix++) {
  80.             ConnectedComponent = (ElectronicComponent) ActVector.elementAt(ix);
  81.             if (ConnectedComponent != this) {
  82.                 // System.out.println ("Connected component to switch found");
  83.                 ConnectedComponent.ReceivePotential(id, ActLevel, x, y);
  84.             }
  85.         }
  86.     }
  87.  
  88. //----------------------------------------------------------------------------
  89. // The switch receives a potential v at pos xr, yr
  90. //----------------------------------------------------------------------------
  91.     public void ReceivePotential(int id, int v, int xr, int yr) {
  92.         int ix;
  93.         // System.out.println ("Switch ReceivePotential()");
  94.  
  95.        if (xr == Pos.x + 1 && yr == Pos.y + 2) {
  96.            // System.out.println ("Switch pin A");
  97.            ALevel = v;
  98.            if (!SwitchClosed) {
  99.                QLevel = v;
  100.                InformComponents(id, v, ConnCompsQ, Pos.x + 9, Pos.y + 3);
  101.            }
  102.            return;
  103.        }
  104.        if (xr == Pos.x + 1 && yr == Pos.y + 4) {
  105.             // System.out.println ("Switch pin B");
  106.             BLevel = v;
  107.             if (SwitchClosed) {
  108.                 QLevel = v;
  109.                 InformComponents(id, v, ConnCompsQ, Pos.x + 9, Pos.y + 3);
  110.             }
  111.             return;
  112.         }
  113.         if (xr == Pos.x + 9 && yr == Pos.y + 3) {
  114.             // System.out.println ("Switch pin Q");
  115.             QLevel = v;
  116.             if (SwitchClosed) {
  117.                 BLevel = v;
  118.                 InformComponents(id, v, ConnCompsB, Pos.x + 1, Pos.y + 4);
  119.             } else {
  120.                 ALevel = v;
  121.                 InformComponents(id, v, ConnCompsA, Pos.x + 1, Pos.y + 2);
  122.             }
  123.             return;
  124.         }
  125.     }
  126.  
  127. //----------------------------------------------------------------------------
  128. // Set up the Switch before simulating
  129. //----------------------------------------------------------------------------
  130.     public void SimulateSetUp(int x, int y, Vector ActComps) {
  131.         // System.out.println ("Simulate Set Up switch pos " + x + ", " + y);
  132.         if (Pos.x + 1 == x && Pos.y + 2 == y) {
  133.             // System.out.println ("Pin A");
  134.             ConnCompsA = ActComps;
  135.         }
  136.         if (Pos.x + 1 == x && Pos.y + 4 == y) {
  137.             // System.out.println ("Pin B");
  138.             ConnCompsB = ActComps;
  139.         }
  140.         if (Pos.x +9 == x && Pos.y + 3 == y) {
  141.             // System.out.println ("Pin Q");
  142.             ConnCompsQ = ActComps;
  143.         }
  144.     }
  145.  
  146. //----------------------------------------------------------------------------
  147. // Remove all pins from PinGrid
  148. //----------------------------------------------------------------------------
  149.     public void RemovePinsGrid(Pin PinGrid[][]) {
  150.         // System.out.println ("Switch RemovePinsGrid()");
  151.         RemovePin(PinGrid[ Pos.x + 1] [Pos.y + 2]);
  152.         RemovePin(PinGrid[ Pos.x + 1] [Pos.y + 4]);
  153.         RemovePin(PinGrid[ Pos.x + 9] [Pos.y + 3]);
  154.     }
  155.  
  156. //----------------------------------------------------------------------------
  157. // Register all Pins
  158. //----------------------------------------------------------------------------
  159.     public void RegisterPins(Pin PinGrid[][], int x, int y) {
  160.         if (PinGrid == null) return;
  161.         // System.out.println ("Switch RegisterPins()");
  162.         RegisterPin(PinGrid[x + 1][y + 2]);
  163.         RegisterPin(PinGrid[x + 1][y + 4]);
  164.         RegisterPin(PinGrid[x + 9][y + 3]);
  165.     }
  166.  
  167. //----------------------------------------------------------------------------
  168. // Register all Pins in PinGrid
  169. //----------------------------------------------------------------------------
  170.     public void PlacePinsHere(Pin PinGrid[][]) {
  171.         if (PinGrid == null) return;
  172.         RegisterPins(PinGrid, Pos.x, Pos.y);
  173.     }
  174.  
  175. //----------------------------------------------------------------------------
  176. // Draw this switch
  177. //----------------------------------------------------------------------------
  178.     public void draw(Graphics g, int xp, int yp, int gs) {
  179.         super.draw (g, xp, yp, gs);
  180.         int x = Pos.x - xp;
  181.         int y = Pos.y - yp;
  182.  
  183.         // Draw HitBox
  184.         // DrawHitBox(g, x, y, gs);
  185.  
  186.         // Draw pins
  187.         g.setColor (ComponentPin.PinColor);
  188.         g.drawLine ((x + 1) * gs, (y + 2) * gs, (x + 3) * gs, (y + 2) * gs);          // A
  189.         g.drawLine ((x + 1) * gs, (y + 4) * gs, (x + 3) * gs, (y + 4) * gs);           // B
  190.         g.drawLine ((x + 8) * gs, (y + 3) * gs, (x + 9) * gs, (y + 3) * gs);    // Q
  191.         // Draw Component
  192.         g.setColor (ComponentColor);
  193.         g.fillRect ((x + 3) * gs, (y + 2) * gs - gs / 3, gs / 2, gs / 2 + 1);
  194.         g.fillRect ((x + 3) * gs, (y + 4) * gs - gs / 3, gs / 2, gs / 2 + 1);
  195.         g.fillRect ((int)((x + 7.5) * gs), (y + 3) * gs - gs / 3 + 1, gs/2, gs/2);
  196.         switch (QLevel) {
  197.             case 0:
  198.                 g.setColor (Color.green);
  199.                 break;
  200.             case 5:
  201.                 g.setColor (Color.red);
  202.                 break;
  203.             default:
  204.                 g.setColor (Color.gray);
  205.                 break;
  206.         }
  207.         if (SwitchClosed) {
  208.             g.drawLine ((x + 3) * gs, (y + 4) * gs, (x + 8) * gs, (y + 3) * gs);
  209.         } else {
  210.             g.drawLine ((x + 3) * gs, (y + 2) * gs, (x + 8) * gs, (y + 3) * gs);
  211.         }
  212.     }
  213. }