home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / DIGSIM.EXE / ElectronicComponent.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  19.0 KB  |  436 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // ElectronicComponent.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 creating, drawing,
  12. // maintaining and simulating an abstract Electronic Component
  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.applet.Applet;
  30. import java.awt.*;
  31. import java.util.Vector;
  32. import java.io.PrintStream;
  33.  
  34. class ElectronicComponent extends Object {
  35.     protected Point Pos;
  36.     protected Dimension Dim;
  37.     protected String ComponentName;
  38.     protected String ClassName;
  39.     protected Point HitBox;
  40.     protected Dimension HitBoxSize;
  41.     protected InputPin IPin[];
  42.     protected OutputPin OPin[];
  43.     protected int Inputs, Outputs;
  44.     public boolean Selected = false;
  45.     protected static final Color ComponentColor = Color.yellow;
  46.     static final int MAXLOOPS = 100;
  47.  
  48. //----------------------------------------------------------------------------
  49. // The constructor of a new electronic component
  50. //----------------------------------------------------------------------------
  51.     public ElectronicComponent() {
  52.     }
  53.  
  54. //----------------------------------------------------------------------------
  55. // The constructor of a new electronic component, with the specified values
  56. //----------------------------------------------------------------------------
  57.     public ElectronicComponent(int x, int y, int w, int h,
  58.                                int xh, int yh, int wh, int hh,
  59.                                int i, int o ) {
  60.         Pos = new Point (x, y);
  61.         Dim = new Dimension (w, h);
  62.         HitBox = new Point (xh, yh);
  63.         HitBoxSize = new Dimension(wh, hh);
  64.         Inputs = i; Outputs = o;
  65.         IPin = new InputPin[i];
  66.         OPin = new OutputPin[o];
  67.      }
  68.  
  69. //----------------------------------------------------------------------------
  70. // The constructor of a new component, which is a copy of CompToCopy
  71. //----------------------------------------------------------------------------
  72.     public ElectronicComponent (ElectronicComponent CompToCopy, int xo, int yo) {
  73.         int ix;
  74.         Pos = new Point (CompToCopy.Pos.x - xo, CompToCopy.Pos.y - yo);
  75.         Dim = new Dimension (CompToCopy.Dim.width, CompToCopy.Dim.height);
  76.         HitBox = new Point (CompToCopy.HitBox.x, CompToCopy.HitBox.y);
  77.         HitBoxSize = new Dimension(CompToCopy.HitBoxSize.width, CompToCopy.HitBoxSize.height);
  78.         Inputs = CompToCopy.Inputs; Outputs = CompToCopy.Outputs;
  79.         ComponentName = CompToCopy.ComponentName;
  80.         ClassName = CompToCopy.ClassName;
  81.         IPin = new InputPin[Inputs];
  82.         OPin = new OutputPin[Outputs];
  83.         for (ix = 0; ix < Inputs; ix++) {
  84.             IPin[ix] = new InputPin(CompToCopy.IPin[ix]);
  85.         }
  86.         for (ix = 0; ix < Outputs; ix++) {
  87.             OPin[ix] = new OutputPin(CompToCopy.OPin[ix]);
  88.         }
  89.     }
  90.  
  91. //----------------------------------------------------------------------------
  92. // Method for copying this component.
  93. //----------------------------------------------------------------------------
  94.     public ElectronicComponent Copy(int xo, int yo) {
  95.         return this; // Should not occure, But return <this> to avoid problems
  96.     }
  97.  
  98. //----------------------------------------------------------------------------
  99. // get the name of this component
  100. //----------------------------------------------------------------------------
  101.     public String getName() {
  102.         return ComponentName;
  103.     }
  104.  
  105. //----------------------------------------------------------------------------
  106. // Init this component before simulating
  107. //----------------------------------------------------------------------------
  108.     public void InitBeforeSimulate() {
  109.         for (int i = 0; i < Inputs; i++) {
  110.             IPin[i].InitBeforeSimulate();
  111.         }
  112.  
  113.         for (int o = 0; o < Outputs; o++) {
  114.             OPin[o].InitBeforeSimulate();
  115.         }
  116.     }
  117.  
  118. //----------------------------------------------------------------------------
  119. // The component receives a potential at the input pin at coord xr, yr
  120. //----------------------------------------------------------------------------
  121.     public void ReceivePotential(int id, int v, int xr, int yr) {
  122.         int i, o, ix;
  123. //      System.out.println (ComponentName + " ReceivePotential id = " + id + " Voltage = " + v);
  124.  
  125.         // Check for short-circuit
  126.         for (o = 0; o < Outputs; o++) {
  127.             // System.out.println ("Check OPin" + o);
  128.             if (xr == OPin[o].PinPos.x + Pos.x && yr == OPin[o].PinPos.y + Pos.y) {
  129.                 if (OPin[o].getLevel() != v) {
  130.                     // System.out.println ("Short circuit!");
  131.                     // System.out.println ("pin level = " + OPin[o].Level + " Received = " + v);
  132.                     OPin[o].ShortCircuit = true;
  133.                 }
  134.             }
  135.         }
  136.  
  137.         for (i = 0; i < Inputs; i++) {
  138.             // System.out.println ("Check IPin" + i);
  139.             if (xr == IPin[i].PinPos.x + Pos.x && yr == IPin[i].PinPos.y + Pos.y) {
  140.                 if (id == IPin[i].ReceivedSimulationCycleID ) {
  141.                     if (IPin[i].Level == v) {
  142.                        // System.out.println ("*!*!*!*!* Input pin " + i + " ALREADY RECEIVED !*!*!*!*!*");
  143.                         return;
  144.                     } else {
  145.                         IPin[i].LevelChanged++;
  146.                         if (IPin[i].LevelChanged > MAXLOOPS) {
  147.                             // System.out.println ("LOOPING");
  148.                                 IPin[1].Looping = true;
  149.                             return;
  150.                         }
  151.                     }
  152.                 }
  153.                 // System.out.println (ComponentName + " setLevel " + v + " at Input pin #" + i);
  154.                 IPin[i].setLevel(v);
  155.                 IPin[i].ReceivedSimulationCycleID = id;
  156.                 if ( (IPin[i].Flags & ComponentPin.PIN_EDGETRIGGERED) == ComponentPin.PIN_EDGETRIGGERED) {
  157.                     SimulateLogic();
  158.                     InformConnectedComponentsOldLevel(id);
  159.                 } else if ( (IPin[i].Flags & ComponentPin.PIN_NOACTION) != ComponentPin.PIN_NOACTION) {
  160.                     SimulateLogic();
  161.                     InformConnectedComponents(id);
  162.                 }
  163.                 return;
  164.             }
  165.         }
  166.     }
  167.  
  168. //----------------------------------------------------------------------------
  169. // This code is simulating the component
  170. //----------------------------------------------------------------------------
  171.     public void SimulateLogic() {
  172.     }
  173.  
  174. //----------------------------------------------------------------------------
  175. // This code is called if the component must perform an action
  176. // every simulation cycle.
  177. //----------------------------------------------------------------------------
  178.     public void Simulate(int id) {
  179.     }
  180.  
  181. //----------------------------------------------------------------------------
  182. // Inform all components connected to all outputs.
  183. //----------------------------------------------------------------------------
  184.     public void InformConnectedComponents(int id) {
  185.         ElectronicComponent ConnectedComponent;
  186.         int o, ix;
  187.  
  188. //      System.out.println ("InformConnectedComponents()");
  189.         for (o = 0; o < Outputs; o++) {
  190.             for (ix = 0; ix < OPin[o].ConnComps.size(); ix++) {
  191.                 ConnectedComponent = (ElectronicComponent) OPin[o].ConnComps.elementAt(ix);
  192.                 if (ConnectedComponent != this) {
  193. //                  System.out.println ("-----");
  194. //                  System.out.println (ComponentName +" Connected component to Output " + o + " found");
  195. //                  System.out.println ("This component will receive: " + OPin[o].getLevel());
  196. //                  System.out.println ("-----");
  197.                     ConnectedComponent.ReceivePotential(id, OPin[o].getLevel(), OPin[o].PinPos.x + Pos.x, OPin[o].PinPos.y + Pos.y);
  198.                 }
  199.             }
  200.         }
  201.     }
  202.  
  203. //----------------------------------------------------------------------------
  204. // Inform all components connected to all outputs with the OLD level
  205. //----------------------------------------------------------------------------
  206.     public void InformConnectedComponentsOldLevel(int id) {
  207.         ElectronicComponent ConnectedComponent;
  208.         int o, ix;
  209.  
  210. //        System.out.println ("InformConnectedComponentsOldLevel()");
  211.         for (o = 0; o < Outputs; o++) {
  212.             for (ix = 0; ix < OPin[o].ConnComps.size(); ix++) {
  213.                 ConnectedComponent = (ElectronicComponent) OPin[o].ConnComps.elementAt(ix);
  214.                 if (ConnectedComponent != this) {
  215.  
  216. //                    System.out.println ("-----");
  217. //                    System.out.println (ComponentName +" Connected component to Output " + o + " found");
  218. //                    System.out.println ("This component will receive: " + OPin[o].getOldLevel());
  219. //                    System.out.println ("-----");
  220.                     ConnectedComponent.ReceivePotential(id, OPin[o].getOldLevel(), OPin[o].PinPos.x + Pos.x, OPin[o].PinPos.y + Pos.y);
  221.                 }
  222.             }
  223.         }
  224.     }
  225.  
  226. //----------------------------------------------------------------------------
  227. // Set up the component before simulating
  228. //----------------------------------------------------------------------------
  229.     public void SimulateSetUp(int x, int y, Vector ActComps) {
  230.         int ix;
  231.         // System.out.println ("SimulateSetUp pos x, y: " + x + ", " + y);
  232.         // Check if this pin is an input
  233.         for (ix = 0; ix < Inputs; ix++) {
  234.             if (Pos.x + IPin[ix].PinPos.x == x &&
  235.                 Pos.y + IPin[ix].PinPos.y == y    ) {
  236.                 // System.out.println ("Equals IPin " + ix);
  237.                 IPin[ix].ConnComps = ActComps;
  238.             }
  239.         }
  240.         // Check if this pin is an output
  241.         for (ix = 0; ix < Outputs; ix++) {
  242.             if (Pos.x + OPin[ix].PinPos.x == x &&
  243.                 Pos.y + OPin[ix].PinPos.y == y    ) {
  244.                 // System.out.println ("Equals OPin " + ix);
  245.                 OPin[ix].ConnComps = ActComps;
  246.             }
  247.         }
  248.     }
  249.  
  250. //----------------------------------------------------------------------------
  251. // Draw this component
  252. //----------------------------------------------------------------------------
  253.     public void draw(Graphics g, int xp, int yp, int gs) {
  254.         if (Selected) {
  255.             int x = Pos.x - xp;
  256.             int y = Pos.y - yp;
  257.             g.setColor (Color.white);
  258.             g.drawRect ((int)((x + HitBox.x - 0.25) * gs), (int)((y + HitBox.y - 0.25) * gs), gs / 2, gs / 2);
  259.             g.drawRect ((int)((x + HitBox.x + HitBoxSize.width - 0.25) * gs), (int)((y + HitBox.y - 0.25) * gs), gs / 2, gs / 2);
  260.             g.drawRect ((int)((x + HitBox.x - 0.25) * gs), (int)((y + HitBox.y + HitBoxSize.height - 0.25) * gs), gs / 2, gs / 2);
  261.             g.drawRect ((int)((x + HitBox.x + HitBoxSize.width - 0.25) * gs), (int)((y + HitBox.y + HitBoxSize.height - 0.25) * gs), gs / 2, gs / 2);
  262.         }
  263.     }
  264.  
  265. //----------------------------------------------------------------------------
  266. // Draw the hit box, only used for debugging
  267. //----------------------------------------------------------------------------
  268.     public void DrawHitBox(Graphics g, int x, int y, int gs) {
  269.         // Draw hitbox
  270.         g.setColor (Color.gray);
  271.         g.drawRect ((x + HitBox.x) * gs, (y + HitBox.y) * gs,
  272.                     HitBoxSize.width * gs, HitBoxSize.height * gs) ;
  273.         // Draw Dimension
  274.         g.setColor (Color.pink);
  275.         g.drawRect (x * gs, (y + 1)  * gs,
  276.                     Dim.width * gs, Dim.height * gs);
  277.     }
  278.  
  279. //----------------------------------------------------------------------------
  280. // Adjust the position of this component
  281. //----------------------------------------------------------------------------
  282.     public boolean AdjustPosition (Pin PinGrid[][], int x, int y) {
  283.         Pos.x += x; Pos.y += y;
  284.         return true;
  285.     }
  286.  
  287. //----------------------------------------------------------------------------
  288. // Check and modify the location of this component if needed
  289. //----------------------------------------------------------------------------
  290.     public void CheckPosition() {
  291.         if (Pos.x < 0) Pos.x = 0;
  292.         if (Pos.y < 0) Pos.y = 0;
  293.         if (Pos.x > DigSim.MaxXPoints - Dim.width) Pos.x = DigSim.MaxXPoints - Dim.width;
  294.         if (Pos.y > DigSim.MaxYPoints - Dim.height) Pos.y = DigSim.MaxYPoints - Dim.height;
  295.     }
  296.  
  297. //----------------------------------------------------------------------------
  298. // Check if this component is in the select box
  299. //----------------------------------------------------------------------------
  300.     public boolean CheckIfComponentInSelectBox(int x1, int y1, int x2, int y2) {
  301.         int cx1 = Pos.x + HitBox.x;
  302.         int cy1 = Pos.y + HitBox.y;
  303.         int cx2 = Pos.x + HitBox.x + HitBoxSize.width;
  304.         int cy2 = Pos.y + HitBox.y + HitBoxSize.height;
  305.         // System.out.println ("--------------");
  306.         // System.out.println ("Check xy1 " + x1 + ", " + y1);
  307.         // System.out.println ("Check xy2 " + x2 + ", " + y2);
  308.         // System.out.println ("Check cxy1 " + cx1 + ", " + cy1);
  309.         // System.out.println ("Check cxy2 " + cx2 + ", " + cy2);
  310.  
  311.         if (x1 <= cx1 && cx2 <= x2 && y1 <= cy1 && cy2 <= y2) {
  312.             Selected = true;
  313.         } else {
  314.             Selected = false;
  315.         }
  316.         return Selected;
  317.     }
  318.  
  319. //----------------------------------------------------------------------------
  320. // Check if this component is clicked at position x,y
  321. //----------------------------------------------------------------------------
  322.     public boolean CheckIfComponentClicked(int x, int y) {
  323.         if (Pos.x + HitBox.x <= x && x <= Pos.x + HitBox.x + HitBoxSize.width &&
  324.             Pos.y + HitBox.y <= y && y <= Pos.y + HitBox.y + HitBoxSize.height  ) {
  325.                 return true;
  326.         }
  327.         return false;
  328.     }
  329.  
  330. //----------------------------------------------------------------------------
  331. // Register Pin ActPin
  332. //----------------------------------------------------------------------------
  333.     protected void RegisterPin (Pin ActPin) {
  334.         ActPin.AddComponent(this);
  335.     }
  336.  
  337. //----------------------------------------------------------------------------
  338. // Register all Input- and Output pins
  339. //----------------------------------------------------------------------------
  340.     public void RegisterPins(Pin PinGrid[][], int x, int y) {
  341.         int ix, xp, yp;
  342.         // Register all inputs
  343.  
  344.         if (PinGrid == null) return;
  345.         for (ix = 0; ix < Inputs; ix++) {
  346.             xp = x + IPin[ix].PinPos.x;
  347.             yp = y + IPin[ix].PinPos.y;
  348.             // System.out.println ("Register input pin pos " + xp + ", " + yp);
  349.             RegisterPin(PinGrid [xp] [yp] );
  350.         }
  351.         // Register all outputs
  352.         for (ix = 0; ix < Outputs; ix++) {
  353.             xp = x + OPin[ix].PinPos.x;
  354.             yp = y + OPin[ix].PinPos.y;
  355.             // System.out.println ("Register output pin pos " + xp + ", " + yp);
  356.             RegisterPin(PinGrid [xp] [yp] );
  357.         }
  358.     }
  359.  
  360. //----------------------------------------------------------------------------
  361. // remove ActPin
  362. //----------------------------------------------------------------------------
  363.     protected void RemovePin(Pin ActPin) {
  364.         // System.out.println ("Remove pin ");
  365.         ActPin.RemoveComponent(this);
  366.     }
  367.  
  368. //----------------------------------------------------------------------------
  369. // Remove all pins from the grid
  370. //----------------------------------------------------------------------------
  371.     public void RemovePinsGrid(Pin PinGrid[][]) {
  372.         int ix, xp, yp;
  373.         // Remove all registered inputs
  374.         for (ix = 0; ix < Inputs; ix++) {
  375.             xp = Pos.x + IPin[ix].PinPos.x;
  376.             yp = Pos.y + IPin[ix].PinPos.y;
  377.             // System.out.println ("Remove input pin pos " + xp + ", " + yp);
  378.             RemovePin(PinGrid [xp] [yp] );
  379.         }
  380.         // Remove all registered outputs
  381.         for (ix = 0; ix < Outputs; ix++) {
  382.             xp = Pos.x + OPin[ix].PinPos.x;
  383.             yp = Pos.y + OPin[ix].PinPos.y;
  384.             // System.out.println ("Remove output pin pos " + xp + ", " + yp);
  385.             RemovePin(PinGrid [xp] [yp] );
  386.         }
  387.     }
  388.  
  389. //----------------------------------------------------------------------------
  390. // Place all pins in the PinGrid
  391. //----------------------------------------------------------------------------
  392.     public void PlacePinsHere(Pin PinGrid[][]) {
  393.         RegisterPins(PinGrid, Pos.x, Pos.y);
  394.     }
  395.  
  396. //----------------------------------------------------------------------------
  397. // User clicked in this component while simulating
  398. //----------------------------------------------------------------------------
  399.     public boolean SimMouseDown() {
  400.         return false;
  401.     }
  402.  
  403. //----------------------------------------------------------------------------
  404. // User released mouse button in this component while simulating
  405. //----------------------------------------------------------------------------
  406.     public boolean SimMouseUp() {
  407.         return false;
  408.     }
  409.  
  410. //----------------------------------------------------------------------------
  411. // Draw all input pins
  412. //----------------------------------------------------------------------------
  413.     public void DrawInputPins(Graphics g, int x, int y, int gs) {
  414.         for (int ix = 0; ix < Inputs; ix++) {
  415.             IPin[ix].draw(g, x, y, gs);
  416.         }
  417.     }
  418.  
  419. //----------------------------------------------------------------------------
  420. // Draw all output pins
  421. //----------------------------------------------------------------------------
  422.     public void DrawOutputPins(Graphics g, int x, int y, int gs) {
  423.         for (int ix = 0; ix < Outputs; ix++) {
  424.             OPin[ix].draw(g, x, y, gs);
  425.         }
  426.     }
  427.  
  428. //----------------------------------------------------------------------------
  429. // Save this component
  430. //----------------------------------------------------------------------------
  431.     public void Save (PrintStream myPrintStream) {
  432.         myPrintStream.println ("describe component " + ClassName);
  433.         myPrintStream.println (" pos " + Pos.x + " " + Pos.y);
  434.         myPrintStream.println ("end describe");
  435.     }
  436. }