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

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // Wire.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 Wire
  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 Wire extends ElectronicComponent {
  34.     protected int x1, y1, x2, y2;
  35.     protected int ChangingWire = 0;
  36.     int ActLevel = -1;
  37.     Vector ConnComps1;
  38.     Vector ConnComps2;
  39.     int ReceivedSimulationCycleID = -1;
  40.  
  41. //----------------------------------------------------------------------------
  42. // The constructor of a new Wire
  43. //----------------------------------------------------------------------------
  44.     public Wire (Pin PinGrid[][], int x, int y) {
  45.         ComponentName = "wire";
  46.         x1 = x; y1 = y;
  47.         ActLevel = -1;
  48.     }
  49.  
  50. //----------------------------------------------------------------------------
  51. // The constructor of a new Wire, with the specified coords.
  52. // This constructor is used when a Wire is loaded or copied.
  53. //----------------------------------------------------------------------------
  54.     public Wire (Pin PinGrid[][], int rx1, int ry1, int rx2, int ry2) {
  55.         ComponentName = "wire";
  56.         x1 = rx1; y1 = ry1;
  57.         x2 = rx2; y2 = ry2;
  58.         ActLevel = -1;
  59.         PlacePinsHere(PinGrid);
  60.     }
  61.  
  62. //----------------------------------------------------------------------------
  63. // The constructor of a new Wire, with the specified coords and level
  64. //----------------------------------------------------------------------------
  65.     public Wire (Pin PinGrid[][], int InitLevel, int rx1, int ry1, int rx2, int ry2) {
  66.         ComponentName = "wire";
  67.         x1 = rx1; y1 = ry1;
  68.         x2 = rx2; y2 = ry2;
  69.         ActLevel = InitLevel;
  70.         PlacePinsHere(PinGrid);
  71.     }
  72.  
  73. //----------------------------------------------------------------------------
  74. // The constructor of a new Wire, which is a copy of CompToCopy
  75. //----------------------------------------------------------------------------
  76.     public Wire (ElectronicComponent CompToCopy, int xo, int yo) {
  77.         ComponentName = "wire";
  78.         ActLevel = -1;
  79.     }
  80.  
  81. //----------------------------------------------------------------------------
  82. // Method for copying this component.
  83. //----------------------------------------------------------------------------
  84.     public ElectronicComponent Copy(int xo, int yo) {
  85.         Wire NewComponent = new Wire(this, xo, yo);
  86.         NewComponent.x1 = x1 - xo;
  87.         NewComponent.y1 = y1 - yo;
  88.         NewComponent.x2 = x2 - xo;
  89.         NewComponent.y2 = y2 - yo;
  90.         CheckPosition();            // Should be OK, but to really be sure
  91.  
  92.         return NewComponent;
  93.     }
  94.  
  95. //----------------------------------------------------------------------------
  96. // Check the position of this wire. if there is an error, adjust
  97. // the position
  98. //----------------------------------------------------------------------------
  99.     public void CheckPosition() {
  100.         if (x1 < 1) x1 = 1;
  101.         if (x1 >= DigSim.MaxXPoints) x1 = DigSim.MaxXPoints-1;
  102.         if (y1 < 1) y1 = 1;
  103.         if (y1 >= DigSim.MaxYPoints) y1 = DigSim.MaxYPoints-1;
  104.         if (x2 < 1) x2 = 1;
  105.         if (x2 >= DigSim.MaxXPoints) x2 = DigSim.MaxXPoints-1;
  106.         if (y2 < 1) y2 = 1;
  107.         if (y2 >= DigSim.MaxYPoints) y2 = DigSim.MaxYPoints-1;
  108.     }
  109.  
  110. //----------------------------------------------------------------------------
  111. // Set up this wire before simulating.
  112. //----------------------------------------------------------------------------
  113.     public void SimulateSetUp(int x, int y, Vector ActComps) {
  114.         // System.out.println ("Simulate Set Up Wire at pos " + x + ", " + y);
  115.         // System.out.println ("xy1 = " + x1 + ", " + y1 + " xy2 = " + x2 + ", " + y2);
  116.         if (x == x1 && y == y1  ) {
  117.             // System.out.println ("WireEnd 1");
  118.             ConnComps1 = ActComps;
  119.         }
  120.         if (x == x2  && y == y2  ) {
  121.             // System.out.println ("WireEnd 2");
  122.             ConnComps2 = ActComps;
  123.         }
  124.     }
  125.  
  126. //----------------------------------------------------------------------------
  127. // Initialize this wire before simulating.
  128. //----------------------------------------------------------------------------
  129.     public void InitBeforeSimulate() {
  130.         ReceivedSimulationCycleID = -1;
  131.     }
  132.  
  133. //----------------------------------------------------------------------------
  134. // The wire is receiving a potential.
  135. // 'Send' this potential to the components connected to the other
  136. // wire-end
  137. //----------------------------------------------------------------------------
  138.     public void ReceivePotential(int id, int v, int xr, int yr) {
  139.         int ix;
  140.         // System.out.println ("Wire ReceivePotential( " + ActLevel + " )");
  141.         if (id == ReceivedSimulationCycleID && ActLevel == v) {
  142.             // System.out.println ("*!*!*!*!* ALREADY RECEIVED !*!*!*!*!*");
  143.             // return to avoid a software loop.
  144.             return;
  145.         }
  146.         ActLevel = v;
  147.  
  148.         ReceivedSimulationCycleID = id;
  149.         // System.out.println ("Check Wireend 1");
  150.         if (x1 == xr && y1 == yr) {
  151.             // System.out.println ("Wireend 1");
  152.             InformComponents(id, ConnComps2, x2, y2);
  153.             return;
  154.         }
  155.         // System.out.println ("Check Wireend 2");
  156.         if (x2 == xr && y2 == yr) {
  157.             // System.out.println ("Wireend 2");
  158.             InformComponents(id, ConnComps1, x1, y1);
  159.             return;
  160.         }
  161.     }
  162.  
  163. //----------------------------------------------------------------------------
  164. // Inform all components connected at the specified wire-end
  165. //----------------------------------------------------------------------------
  166.     public void InformComponents(int id, Vector ActVector, int x, int y) {
  167.         ElectronicComponent ConnectedComponent;
  168.         int ix;
  169.  
  170.         SimulateLogic();
  171.         for (ix = 0; ix < ActVector.size(); ix++) {
  172.             ConnectedComponent = (ElectronicComponent) ActVector.elementAt(ix);
  173.             if (ConnectedComponent != this) {
  174.                 // System.out.println ("Connected component to other wireend found");
  175.                 ConnectedComponent.ReceivePotential(id, ActLevel, x, y);
  176.             }
  177.         }
  178.     }
  179.  
  180. //----------------------------------------------------------------------------
  181. // Adjust the position of this wire.
  182. //----------------------------------------------------------------------------
  183.     public boolean AdjustPosition (Pin PinGrid[][], int x, int y) {
  184.         int nx1 = x1;
  185.         int ny1 = y1;
  186.         int nx2 = x2;
  187.         int ny2 = y2;
  188.  
  189.         if ((ChangingWire & 1) == 1) {
  190.             nx1 += x; ny1 += y;
  191.         }
  192.         if ((ChangingWire & 2) == 2) {
  193.             nx2 += x; ny2 += y;
  194.         }
  195.         if (nx1 == nx2 && ny1 == ny2) {
  196.             return false;       // Can't change, wire size would be 0.
  197.         }
  198.         x1 = nx1; y1 = ny1;
  199.         x2 = nx2; y2 = ny2;
  200.         return true;
  201.     }
  202.  
  203. //----------------------------------------------------------------------------
  204. // Remove both pins from the grid.
  205. //----------------------------------------------------------------------------
  206.     public void RemovePinsGrid(Pin PinGrid[][]) {
  207.         // System.out.println ("Remove pins xy1 = " + x1 + ", " + y1);
  208.         // System.out.println ("Remove pins xy2 = " + x2 + ", " + y2);
  209.         RemovePin(PinGrid[x1][y1]);
  210.         RemovePin(PinGrid[x2][y2]);
  211.     }
  212.  
  213. //----------------------------------------------------------------------------
  214. // Add both pins to the grid.
  215. //----------------------------------------------------------------------------
  216.     public void RegisterPins(Pin PinGrid[][], int x, int y) {
  217.         if (PinGrid == null) return;
  218.         // System.out.println ("Register pins xy1 = " + x1 + ", " + y1);
  219.         // System.out.println ("Register pins xy2 = " + x2 + ", " + y2);
  220.         RegisterPin(PinGrid[x1][y1]);
  221.         RegisterPin(PinGrid[x2][y2]);
  222.     }
  223.  
  224. //----------------------------------------------------------------------------
  225. // Add both pins to the grid.
  226. //----------------------------------------------------------------------------
  227.     public void PlacePinsHere(Pin PinGrid[][]) {
  228.         if (PinGrid == null) return;
  229.         RegisterPins(PinGrid, 0, 0);
  230.     }
  231.  
  232. //----------------------------------------------------------------------------
  233. // The user released the mouse-button when drawing a new wire,
  234. // set the second coord.
  235. //----------------------------------------------------------------------------
  236.     public void Set2ndCoord (Pin PinGrid[][], int x, int y) {
  237.         x2 = x; y2 = y;
  238.     }
  239.  
  240. //----------------------------------------------------------------------------
  241. // Draw this wire.
  242. //----------------------------------------------------------------------------
  243.     public void draw(Graphics g, int GridXOffset, int GridYOffset, int gs) {
  244.         if (Selected) {
  245.             g.setColor (Color.white);
  246.             if ((ChangingWire & 1) == 1) {
  247.                 g.drawRect ((int)((x1 - GridXOffset - 0.25) * gs), (int)((y1 - GridYOffset - 0.25) * gs), gs / 2, gs / 2);
  248.             }
  249.             if ((ChangingWire & 2) == 2) {
  250.                 g.drawRect ((int)((x2 - GridXOffset - 0.25) * gs), (int)((y2 - GridYOffset - 0.25) * gs), gs / 2, gs / 2);
  251.             }
  252.         }
  253.         if (ActLevel == 0)  g.setColor (Color.green);
  254.         else if (ActLevel == 5) g.setColor (Color.red);
  255.         else g.setColor (Color.gray);
  256.         g.drawLine ((x1- GridXOffset) * gs, (y1 - GridYOffset) * gs, (x2 - GridXOffset) * gs, (y2 - GridYOffset) * gs);
  257.     }
  258.  
  259. //----------------------------------------------------------------------------
  260. // Return the sign
  261. //----------------------------------------------------------------------------
  262.     public int sgn(int val) {
  263.         if (val > 0) return 1;
  264.         if (val < 0) return -1;
  265.         return 0;
  266.     }
  267.  
  268. //----------------------------------------------------------------------------
  269. // This is a sort of line-drawing function, but it doesn't paint pixels at
  270. // the calculated position but it calls a function.
  271. // This is used to determine if the user pointed somewhere in the middle
  272. // of the wire when moving the wire or at placing junctions.
  273. //----------------------------------------------------------------------------
  274.     public boolean CheckIfPointInWire(int a, int b, int c, int d, int x, int y) {
  275.         int u, s, v, i, d1x, d1y, d2x, d2y, m, n;
  276.  
  277.         u = c - a;
  278.         v = d - b;
  279.         d1x = sgn(u);
  280.         d1y = sgn(v);
  281.         d2x = sgn(u);
  282.         d2y = 0;
  283.         m = Math.abs(u);
  284.         n = Math.abs(v);
  285.         if (m <= n) {
  286.             d2x = 0 ;
  287.             d2y = sgn(v);
  288.             m = Math.abs(v);
  289.             n = Math.abs(u);
  290.          }
  291.          s = m / 2;
  292.         for (i = 0; i < m ; i++) {
  293.             if (Math.abs (x - a) < 1 &&
  294.                 Math.abs (y - b) < 1) return true;
  295.             s = s + n;
  296.             if (s >=m) {
  297.                 s = s - m;
  298.                 a = a + d1x;
  299.                 b = b + d1y;
  300.             } else {
  301.                 a = a + d2x;
  302.                 b = b + d2y;
  303.             }
  304.         }
  305.         return false;
  306.     }
  307.  
  308. //----------------------------------------------------------------------------
  309. // Check if this wire is clicked at the specified coord.
  310. //----------------------------------------------------------------------------
  311.     public boolean CheckIfComponentClicked(int x, int y) {
  312.         // Override ElectronicComponent function.
  313.         // // System.out.println ("CheckIfWireClicked");
  314.         if (x == x1 && y == y1) {
  315.             ChangingWire = 1;
  316.             return true;
  317.         }
  318.         if (x == x2 && y == y2) {
  319.             ChangingWire = 2;
  320.             return true;
  321.         }
  322.         // Check if user clicked somewhere in the middle of the line.
  323.         if (CheckIfPointInWire(x1, y1, x2, y2, x, y)) {
  324.             ChangingWire = 1 | 2;
  325.             return true;
  326.         }
  327.         return false;
  328.     }
  329.  
  330. //----------------------------------------------------------------------------
  331. // Check if the wire is in the box.
  332. //----------------------------------------------------------------------------
  333.     public boolean CheckIfComponentInSelectBox(int tx1, int ty1, int tx2, int ty2) {
  334.         Selected = false;
  335.         ChangingWire = 0;
  336.         // Wire-end 1 selected?
  337.         if (tx1 <= x1 && x1 <= tx2 && ty1 <= y1 && y1 <= ty2) {
  338.             ChangingWire |= 1;
  339.             Selected = true;
  340.         }
  341.         // Wire-end 2 selected?
  342.         if (tx1 <= x2 && x2 <= tx2 && ty1 <= y2 && y2 <= ty2) {
  343.             ChangingWire |= 2;
  344.             Selected = true;
  345.         }
  346.         return Selected;
  347.     }
  348.  
  349. //----------------------------------------------------------------------------
  350. // Save this wire.
  351. //----------------------------------------------------------------------------
  352.     public void Save (PrintStream myPrintStream) {
  353.         myPrintStream.println ("describe component Wire");
  354.         myPrintStream.println (" pos " + x1 + " " + y1);
  355.         myPrintStream.println (" pos2 " + x2 + " " + y2);
  356.         myPrintStream.println ("end describe");
  357.     }
  358.  
  359. //----------------------------------------------------------------------------
  360. // Try to place a junction here. A junction is performed by splitting
  361. // the wire in two pieces.
  362. //----------------------------------------------------------------------------
  363.     public boolean TryPlaceJunction(Schematic ActSchematic, Pin PinGrid[][], int x, int y, int gs) {
  364.         // System.out.println ("Wire TryPlaceJunction");
  365.         if (x == x1 && y == y1) return false; // No need for junction here
  366.         if (x == x2 && y == y2) return false; // No need for junction here
  367.         if (CheckIfPointInWire(x1 * gs, y1 * gs, x2 * gs, y2 * gs, x * gs, y * gs)) {
  368.             // System.out.println ("Exact hit wire!");
  369.             // Split this wire in two parts
  370.             ActSchematic.addComponent (new Wire (PinGrid, ActLevel, x, y, x1, y1));
  371.             RemovePinsGrid(PinGrid);
  372.             x1 = x; y1 = y;
  373.             PlacePinsHere(PinGrid);
  374.             return true;
  375.         }
  376.  
  377.         return false;
  378.     }
  379. }