Skip to content

Commit

Permalink
modified everything
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-algohary committed Jan 2, 2023
1 parent 76ab999 commit dfcebca
Show file tree
Hide file tree
Showing 15 changed files with 988 additions and 134 deletions.
4 changes: 3 additions & 1 deletion src/Capacitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public Capacitor(Point[] points , Double args[]){
@Override
public double getValue(){
// V = Q/C
return q/getCapacitance();
System.out.println("Q = "+ q + " V = " + -q/getCapacitance());
return -q/getCapacitance();
}
@Override
public void setState(double I , double v1 , double v2){
System.out.println("!!!!!!!!!!!!!!!!!!!!! I = "+I+" !!!!!!!!!!!!!!!!");
if(!simulator.isDelayedComputation())
this.q += I*simulator.getTimeStepSecond();
}
Expand Down
21 changes: 12 additions & 9 deletions src/CircuitSimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ public boolean hasDelayedComponents(){
public CircuitSimulator(Component components[])throws Exception{
convertWiresToNodes(components);
this.components = components;
for(Component component : this.components){
component.simulator = this;
}
this.nodes = componentsToNodes(components);
}
public CircuitSimulator(){}
//public CircuitSimulator(){}

private void convertWiresToNodes(Component components[])throws Exception{
for(Component component : components){
if(component.type == Component.Type.V && component.getValue()<0.00000001){
if(component.type == Component.Type.W && component.getValue()<0.00000001){
for(int i=1 ;i<component.points.size();i++){
renameNodeInComponents(components, component.points.get(0), component.points.get(i));
}
Expand All @@ -50,14 +53,14 @@ private void convertWiresToNodes(Component components[])throws Exception{
private void renameNodeInComponents(Component components[] , Point toBeRenamed , Point newName){
for(Component component : components){
for(Point point : component.points){
System.out.println(point.x);
System.out.println(point.y);
System.out.println(toBeRenamed.x);
System.out.println(point.x == toBeRenamed.x);
//System.out.println(point.x);
//System.out.println(point.y);
//System.out.println(toBeRenamed.x);
//System.out.println(point.x == toBeRenamed.x);
if(point.x == toBeRenamed.x && point.y == toBeRenamed.y){
//point.x = newName.x;
//point.y = newName.y;
//point = new Point(toBeRenamed);
point.x = newName.x;
point.y = newName.y;
point = new Point(toBeRenamed);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected boolean isSorted(){
return true;
}
public String imageName = "";
enum Type{IV , V ,I}
enum Type{IV , V ,I , W}
Type type;
boolean isGround = false;
public Component(){}
Expand Down
53 changes: 43 additions & 10 deletions src/ComponentUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,34 @@
import java.util.Scanner;
import java.util.Vector;

public class ComponentUI{
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ComponentUI extends JButton implements ActionListener{
CircularInt orientation = new CircularInt(0, 3);
String name;
Double args[];
String name = new String();
Double args[] = new Double[MAX_ARGS];

ComponentUI(){
updateIcon();
}
private void load(ComponentUI block){
this.orientation = block.orientation;
this.name = block.name;
this.args = block.args;
}
private void updateIcon(){
ImageIcon icon = new ImageIcon(getImage());
RotatedIcon R = new RotatedIcon(icon , RotatedIcon.Rotate.ABOUT_CENTER);
R.setCircularIcon(true);
R.setDegrees(90 * orientation.get());
this.setIcon(R.getIcon());
}

Component getComponent(double x , double y){
Point nodes[] = new Point[]{
new Point(x+1 , y+0.5),
Expand Down Expand Up @@ -54,18 +78,19 @@ String getImage(){
void rotate(){
orientation.inc();
}

static HashMap<String , String[]> componentsArgs = new HashMap<>();
static final int MAX_ARGS = 2;
static{
componentsArgs.put(Constants.R, new String[]{"Ohms"});
componentsArgs.put(Constants.DC_V, new String[]{"Volts"});
componentsArgs.put(Constants.DC_I, new String[]{"Amperes"});
componentsArgs.put(Constants.R, new String[]{"Ohms" , ""});
componentsArgs.put(Constants.DC_V, new String[]{"Volts" , ""});
componentsArgs.put(Constants.DC_I, new String[]{"Amperes" , ""});
componentsArgs.put(Constants.AC_V, new String[]{"Hertz" , "Maximum V"});
componentsArgs.put(Constants.AC_I, new String[]{"Hertz" , "Maximum I"});
componentsArgs.put(Constants.C, new String[]{"Farads"});
componentsArgs.put(Constants.W2, new String[]{});
componentsArgs.put(Constants.W3, new String[]{});
componentsArgs.put(Constants.W4, new String[]{});
componentsArgs.put(Constants.C, new String[]{"Farads" , ""});
componentsArgs.put(Constants.W2, new String[]{"",""});
componentsArgs.put(Constants.W3, new String[]{"",""});
componentsArgs.put(Constants.W4, new String[]{"",""});
}

static HashMap<String , String[]> componentsOrientataions = new HashMap<>();
Expand All @@ -81,6 +106,14 @@ void rotate(){
in.close();
}catch(Exception e){
System.out.println(e);

}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this){
ComponentUI block = Frame_1.run();
this.load(block);
}
}
}
28 changes: 28 additions & 0 deletions src/Diode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Arrays;

public class Diode extends Component {
Diode(Point[] points , Double args[]){
this.points = Arrays.asList(points);
this.args = Arrays.asList(args);
//type = Type.V;
}
@Override
public boolean isDelayed(){
return true;
}

private double getVd(){
return this.args.get(0);
}

@Override
public double getValue(){
if(simulator.isDelayedComputation()){
return getVd();
}else{
return 0;
}

}

}
133 changes: 133 additions & 0 deletions src/Frame_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Frame_1 extends JFrame implements ActionListener{



ComponentUI CUI = new ComponentUI();

private String[] Cs = { Constants.R,
Constants.DC_V,
Constants.DC_I,
Constants.AC_V,
Constants.AC_I,
Constants.W2,
Constants.W3,
Constants.W4,
Constants.C
};

String x;

JComboBox cB ;

JTextField tF1;
JTextField tF2;

JButton b1;

JLabel l1;
JLabel l2;
JLabel l3;
JLabel l4;
JLabel l5;

public Frame_1(){
cB = new JComboBox<String>(Cs);
cB.setSize(150,40);
cB.addActionListener(this);



tF1 = new JTextField();
tF1.setSize(100,40);


tF2 = new JTextField();
tF2.setSize(100,40);



b1 = new JButton("OK");
b1.setFont(new Font("courier",Font.LAYOUT_RIGHT_TO_LEFT,20));
b1.setSize(100,50);
b1.setHorizontalAlignment(JLabel.CENTER);
b1.addActionListener(this);
b1.setFocusPainted(false);
b1.setVisible(true);



l5 = new JLabel("< Choose a component >");
l5.setFont(new Font("Times New Roman",Font.BOLD,30));
l5.setHorizontalAlignment(JLabel.CENTER);


l1 = new JLabel("Component");
l1.add(cB);


l2 = new JLabel("Value 1");
l2.add(tF1);
l2.setHorizontalTextPosition(JLabel.LEFT);
l2.setVerticalTextPosition(JLabel.CENTER);


l3 = new JLabel("Value 2");
l3.add(tF2);
l3.setHorizontalTextPosition(JLabel.LEFT);
l3.setVerticalTextPosition(JLabel.CENTER);


l4 = new JLabel();
l4.add(b1);



this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(450,500);
this.setLayout(new GridLayout(5,1));
this.add(l5);
this.add(l1);
this.add(l2);
this.add(l3);
this.add(l4);

this.setVisible(true);
}

public static ComponentUI run(){
Frame_1 frame1 = new Frame_1();
while(frame1.isVisible()){
System.out.println("hello");
}
return frame1.CUI;
}



@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b1){
CUI.name = (String) cB.getSelectedItem();
CUI.args[0] = Double.parseDouble(tF1.getText());
CUI.args[1] = Double.parseDouble(tF2.getText());
}

if (e.getSource()==cB){

l2.setText(ComponentUI.componentsArgs.get( cB.getSelectedItem())[0]);
l3.setText(ComponentUI.componentsArgs.get( cB.getSelectedItem())[1]);

}
}

}
10 changes: 5 additions & 5 deletions src/LinearSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public LinearSystem(Equation[] equations)throws Exception{
System.out.println(e.toString());
}*/

for(String var : varNames){
/*for(String var : varNames){
System.out.println(var);
}
System.out.println("-----------------");
}*/
/*System.out.println("-----------------");
System.out.println(equations.length);
System.out.println(varNames.length);
System.out.println(constants.length);
System.out.println(coeff.length);
System.out.println(coeff.length);*/
//throw new Exception("equations.length < varNames.length");
//}
for(Equation equation : equations){
System.out.println(equation);
//System.out.println(equation);
}
for(int i=0 ; i<equations.length ; i++){
for(Variable var : equations[i].vars){
Expand Down
Loading

0 comments on commit dfcebca

Please sign in to comment.