home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / DIGSIM.EXE / ElectronicComponent.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-05-21  |  6.6 KB  |  304 lines

  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Point;
  5. import java.io.PrintStream;
  6. import java.util.Vector;
  7.  
  8. class ElectronicComponent {
  9.    protected Point Pos;
  10.    protected Dimension Dim;
  11.    protected String ComponentName;
  12.    protected String ClassName;
  13.    protected Point HitBox;
  14.    protected Dimension HitBoxSize;
  15.    protected InputPin[] IPin;
  16.    protected OutputPin[] OPin;
  17.    protected int Inputs;
  18.    protected int Outputs;
  19.    public boolean Selected = false;
  20.    protected static final Color ComponentColor;
  21.    static final int MAXLOOPS = 100;
  22.  
  23.    public ElectronicComponent() {
  24.    }
  25.  
  26.    public ElectronicComponent(int var1, int var2, int var3, int var4, int var5, int var6, int var7, int var8, int var9, int var10) {
  27.       this.Pos = new Point(var1, var2);
  28.       this.Dim = new Dimension(var3, var4);
  29.       this.HitBox = new Point(var5, var6);
  30.       this.HitBoxSize = new Dimension(var7, var8);
  31.       this.Inputs = var9;
  32.       this.Outputs = var10;
  33.       this.IPin = new InputPin[var9];
  34.       this.OPin = new OutputPin[var10];
  35.    }
  36.  
  37.    public ElectronicComponent(ElectronicComponent var1, int var2, int var3) {
  38.       this.Pos = new Point(var1.Pos.x - var2, var1.Pos.y - var3);
  39.       this.Dim = new Dimension(var1.Dim.width, var1.Dim.height);
  40.       this.HitBox = new Point(var1.HitBox.x, var1.HitBox.y);
  41.       this.HitBoxSize = new Dimension(var1.HitBoxSize.width, var1.HitBoxSize.height);
  42.       this.Inputs = var1.Inputs;
  43.       this.Outputs = var1.Outputs;
  44.       this.ComponentName = var1.ComponentName;
  45.       this.ClassName = var1.ClassName;
  46.       this.IPin = new InputPin[this.Inputs];
  47.       this.OPin = new OutputPin[this.Outputs];
  48.  
  49.       for(int var4 = 0; var4 < this.Inputs; ++var4) {
  50.          this.IPin[var4] = new InputPin(var1.IPin[var4]);
  51.       }
  52.  
  53.       for(int var5 = 0; var5 < this.Outputs; ++var5) {
  54.          this.OPin[var5] = new OutputPin(var1.OPin[var5]);
  55.       }
  56.  
  57.    }
  58.  
  59.    public ElectronicComponent Copy(int var1, int var2) {
  60.       return this;
  61.    }
  62.  
  63.    public String getName() {
  64.       return this.ComponentName;
  65.    }
  66.  
  67.    public void InitBeforeSimulate() {
  68.       for(int var1 = 0; var1 < this.Inputs; ++var1) {
  69.          this.IPin[var1].InitBeforeSimulate();
  70.       }
  71.  
  72.       for(int var2 = 0; var2 < this.Outputs; ++var2) {
  73.          this.OPin[var2].InitBeforeSimulate();
  74.       }
  75.  
  76.    }
  77.  
  78.    public void ReceivePotential(int var1, int var2, int var3, int var4) {
  79.       for(int var6 = 0; var6 < this.Outputs; ++var6) {
  80.          if (var3 == this.OPin[var6].PinPos.x + this.Pos.x && var4 == this.OPin[var6].PinPos.y + this.Pos.y && this.OPin[var6].getLevel() != var2) {
  81.             this.OPin[var6].ShortCircuit = true;
  82.          }
  83.       }
  84.  
  85.       for(int var5 = 0; var5 < this.Inputs; ++var5) {
  86.          if (var3 == this.IPin[var5].PinPos.x + this.Pos.x && var4 == this.IPin[var5].PinPos.y + this.Pos.y) {
  87.             if (var1 == this.IPin[var5].ReceivedSimulationCycleID) {
  88.                if (this.IPin[var5].Level == var2) {
  89.                   return;
  90.                }
  91.  
  92.                ++this.IPin[var5].LevelChanged;
  93.                if (this.IPin[var5].LevelChanged > 100) {
  94.                   this.IPin[1].Looping = true;
  95.                   return;
  96.                }
  97.             }
  98.  
  99.             this.IPin[var5].setLevel(var2);
  100.             this.IPin[var5].ReceivedSimulationCycleID = var1;
  101.             if ((this.IPin[var5].Flags & 4) == 4) {
  102.                this.SimulateLogic();
  103.                this.InformConnectedComponentsOldLevel(var1);
  104.                return;
  105.             }
  106.  
  107.             if ((this.IPin[var5].Flags & 8) != 8) {
  108.                this.SimulateLogic();
  109.                this.InformConnectedComponents(var1);
  110.             }
  111.  
  112.             return;
  113.          }
  114.       }
  115.  
  116.    }
  117.  
  118.    public void SimulateLogic() {
  119.    }
  120.  
  121.    public void Simulate(int var1) {
  122.    }
  123.  
  124.    public void InformConnectedComponents(int var1) {
  125.       for(int var3 = 0; var3 < this.Outputs; ++var3) {
  126.          for(int var4 = 0; var4 < this.OPin[var3].ConnComps.size(); ++var4) {
  127.             ElectronicComponent var2 = (ElectronicComponent)this.OPin[var3].ConnComps.elementAt(var4);
  128.             if (var2 != this) {
  129.                var2.ReceivePotential(var1, this.OPin[var3].getLevel(), this.OPin[var3].PinPos.x + this.Pos.x, this.OPin[var3].PinPos.y + this.Pos.y);
  130.             }
  131.          }
  132.       }
  133.  
  134.    }
  135.  
  136.    public void InformConnectedComponentsOldLevel(int var1) {
  137.       for(int var3 = 0; var3 < this.Outputs; ++var3) {
  138.          for(int var4 = 0; var4 < this.OPin[var3].ConnComps.size(); ++var4) {
  139.             ElectronicComponent var2 = (ElectronicComponent)this.OPin[var3].ConnComps.elementAt(var4);
  140.             if (var2 != this) {
  141.                var2.ReceivePotential(var1, this.OPin[var3].getOldLevel(), this.OPin[var3].PinPos.x + this.Pos.x, this.OPin[var3].PinPos.y + this.Pos.y);
  142.             }
  143.          }
  144.       }
  145.  
  146.    }
  147.  
  148.    public void SimulateSetUp(int var1, int var2, Vector var3) {
  149.       for(int var4 = 0; var4 < this.Inputs; ++var4) {
  150.          if (this.Pos.x + this.IPin[var4].PinPos.x == var1 && this.Pos.y + this.IPin[var4].PinPos.y == var2) {
  151.             this.IPin[var4].ConnComps = var3;
  152.          }
  153.       }
  154.  
  155.       for(int var5 = 0; var5 < this.Outputs; ++var5) {
  156.          if (this.Pos.x + this.OPin[var5].PinPos.x == var1 && this.Pos.y + this.OPin[var5].PinPos.y == var2) {
  157.             this.OPin[var5].ConnComps = var3;
  158.          }
  159.       }
  160.  
  161.    }
  162.  
  163.    public void draw(Graphics var1, int var2, int var3, int var4) {
  164.       if (this.Selected) {
  165.          int var5 = this.Pos.x - var2;
  166.          int var6 = this.Pos.y - var3;
  167.          var1.setColor(Color.white);
  168.          var1.drawRect((int)(((double)(var5 + this.HitBox.x) - (double)0.25F) * (double)var4), (int)(((double)(var6 + this.HitBox.y) - (double)0.25F) * (double)var4), var4 / 2, var4 / 2);
  169.          var1.drawRect((int)(((double)(var5 + this.HitBox.x + this.HitBoxSize.width) - (double)0.25F) * (double)var4), (int)(((double)(var6 + this.HitBox.y) - (double)0.25F) * (double)var4), var4 / 2, var4 / 2);
  170.          var1.drawRect((int)(((double)(var5 + this.HitBox.x) - (double)0.25F) * (double)var4), (int)(((double)(var6 + this.HitBox.y + this.HitBoxSize.height) - (double)0.25F) * (double)var4), var4 / 2, var4 / 2);
  171.          var1.drawRect((int)(((double)(var5 + this.HitBox.x + this.HitBoxSize.width) - (double)0.25F) * (double)var4), (int)(((double)(var6 + this.HitBox.y + this.HitBoxSize.height) - (double)0.25F) * (double)var4), var4 / 2, var4 / 2);
  172.       }
  173.  
  174.    }
  175.  
  176.    public void DrawHitBox(Graphics var1, int var2, int var3, int var4) {
  177.       var1.setColor(Color.gray);
  178.       var1.drawRect((var2 + this.HitBox.x) * var4, (var3 + this.HitBox.y) * var4, this.HitBoxSize.width * var4, this.HitBoxSize.height * var4);
  179.       var1.setColor(Color.pink);
  180.       var1.drawRect(var2 * var4, (var3 + 1) * var4, this.Dim.width * var4, this.Dim.height * var4);
  181.    }
  182.  
  183.    public boolean AdjustPosition(Pin[][] var1, int var2, int var3) {
  184.       Point var10000 = this.Pos;
  185.       var10000.x += var2;
  186.       var10000 = this.Pos;
  187.       var10000.y += var3;
  188.       return true;
  189.    }
  190.  
  191.    public void CheckPosition() {
  192.       if (this.Pos.x < 0) {
  193.          this.Pos.x = 0;
  194.       }
  195.  
  196.       if (this.Pos.y < 0) {
  197.          this.Pos.y = 0;
  198.       }
  199.  
  200.       if (this.Pos.x > 100 - this.Dim.width) {
  201.          this.Pos.x = 100 - this.Dim.width;
  202.       }
  203.  
  204.       if (this.Pos.y > 100 - this.Dim.height) {
  205.          this.Pos.y = 100 - this.Dim.height;
  206.       }
  207.  
  208.    }
  209.  
  210.    public boolean CheckIfComponentInSelectBox(int var1, int var2, int var3, int var4) {
  211.       int var5 = this.Pos.x + this.HitBox.x;
  212.       int var6 = this.Pos.y + this.HitBox.y;
  213.       int var7 = this.Pos.x + this.HitBox.x + this.HitBoxSize.width;
  214.       int var8 = this.Pos.y + this.HitBox.y + this.HitBoxSize.height;
  215.       if (var1 <= var5 && var7 <= var3 && var2 <= var6 && var8 <= var4) {
  216.          this.Selected = true;
  217.       } else {
  218.          this.Selected = false;
  219.       }
  220.  
  221.       return this.Selected;
  222.    }
  223.  
  224.    public boolean CheckIfComponentClicked(int var1, int var2) {
  225.       return this.Pos.x + this.HitBox.x <= var1 && var1 <= this.Pos.x + this.HitBox.x + this.HitBoxSize.width && this.Pos.y + this.HitBox.y <= var2 && var2 <= this.Pos.y + this.HitBox.y + this.HitBoxSize.height;
  226.    }
  227.  
  228.    protected void RegisterPin(Pin var1) {
  229.       var1.AddComponent(this);
  230.    }
  231.  
  232.    public void RegisterPins(Pin[][] var1, int var2, int var3) {
  233.       if (var1 != null) {
  234.          for(int var4 = 0; var4 < this.Inputs; ++var4) {
  235.             int var5 = var2 + this.IPin[var4].PinPos.x;
  236.             int var6 = var3 + this.IPin[var4].PinPos.y;
  237.             this.RegisterPin(var1[var5][var6]);
  238.          }
  239.  
  240.          for(int var7 = 0; var7 < this.Outputs; ++var7) {
  241.             int var8 = var2 + this.OPin[var7].PinPos.x;
  242.             int var9 = var3 + this.OPin[var7].PinPos.y;
  243.             this.RegisterPin(var1[var8][var9]);
  244.          }
  245.  
  246.       }
  247.    }
  248.  
  249.    protected void RemovePin(Pin var1) {
  250.       var1.RemoveComponent(this);
  251.    }
  252.  
  253.    public void RemovePinsGrid(Pin[][] var1) {
  254.       for(int var2 = 0; var2 < this.Inputs; ++var2) {
  255.          int var3 = this.Pos.x + this.IPin[var2].PinPos.x;
  256.          int var4 = this.Pos.y + this.IPin[var2].PinPos.y;
  257.          this.RemovePin(var1[var3][var4]);
  258.       }
  259.  
  260.       for(int var5 = 0; var5 < this.Outputs; ++var5) {
  261.          int var6 = this.Pos.x + this.OPin[var5].PinPos.x;
  262.          int var7 = this.Pos.y + this.OPin[var5].PinPos.y;
  263.          this.RemovePin(var1[var6][var7]);
  264.       }
  265.  
  266.    }
  267.  
  268.    public void PlacePinsHere(Pin[][] var1) {
  269.       this.RegisterPins(var1, this.Pos.x, this.Pos.y);
  270.    }
  271.  
  272.    public boolean SimMouseDown() {
  273.       return false;
  274.    }
  275.  
  276.    public boolean SimMouseUp() {
  277.       return false;
  278.    }
  279.  
  280.    public void DrawInputPins(Graphics var1, int var2, int var3, int var4) {
  281.       for(int var5 = 0; var5 < this.Inputs; ++var5) {
  282.          this.IPin[var5].draw(var1, var2, var3, var4);
  283.       }
  284.  
  285.    }
  286.  
  287.    public void DrawOutputPins(Graphics var1, int var2, int var3, int var4) {
  288.       for(int var5 = 0; var5 < this.Outputs; ++var5) {
  289.          this.OPin[var5].draw(var1, var2, var3, var4);
  290.       }
  291.  
  292.    }
  293.  
  294.    public void Save(PrintStream var1) {
  295.       var1.println("describe component " + this.ClassName);
  296.       var1.println(" pos " + this.Pos.x + " " + this.Pos.y);
  297.       var1.println("end describe");
  298.    }
  299.  
  300.    static {
  301.       ComponentColor = Color.yellow;
  302.    }
  303. }
  304.