-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
158 lines (146 loc) · 6.3 KB
/
Main.java
File metadata and controls
158 lines (146 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
public class Main {
public static ArrayList<Node> nodeList = new ArrayList<Node>();
public static ArrayList<Node> deletedNodeList = new ArrayList<Node>();
public static Translation worldSpaceToScreenSpace;
public static Panel p;
public static Mouse mouseListener = new Mouse();
public static Gate nextGatePlacement;
public static Node lastNodeClicked = null;
public static int nextInputChoice = 1;
private static boolean ticks = false;
public static void main(String args[]) {
createScreens();
while (ticks) {
tick();
}
}
//evaluates the inputs and outputs of each gate each tick and sets the inputs of the next gate to evaluate the following tick
static void tick() {
for (Node workingNode : nodeList) {
workingNode.evaluateGate();
}
for (Node workingNode : nodeList) {
workingNode.nextOuputToCurrentOutput();
}
for (Node workingNode : nodeList) {
for (Node workedNode1 : workingNode.connectionsInput1) {
workingNode.recieveinput1(false);
if (workedNode1.getOutput() == true) {
workingNode.recieveinput1(true);
break;
}
}
for (Node workedNode2 : workingNode.connectionsInput2) {
workingNode.recieveinput2(false);
if (workedNode2.getOutput() == true) {
workingNode.recieveinput2(true);
break;
}
}
}
Main.p.repaint();
}
static void createScreens() {
Screen sc = new Screen();
p = new Panel();
p.addMouseListener(mouseListener);
p.addMouseMotionListener(mouseListener);
p.addMouseWheelListener(mouseListener);
sc.add(p);
setUpKey();
sc.setVisible(true);
worldSpaceToScreenSpace = new Translation(10, 10, p.getWidth(), p.getHeight());
}
static class CtrlZ extends AbstractAction {
public void actionPerformed(ActionEvent e) {
if (nodeList.size() > 1) {
deletedNodeList.add(nodeList.remove(nodeList.size()-1));
p.repaint();
}
}
}
static class CtrlY extends AbstractAction {
public void actionPerformed(ActionEvent e) {
if (deletedNodeList.size() > 1) {
nodeList.add(deletedNodeList.remove(deletedNodeList.size()-1));
p.repaint();
}
}
}
static class tickbutton extends AbstractAction {
public void actionPerformed(ActionEvent e) {
tick();
ticks = true;
}
}
static class connectionNull extends AbstractAction {
public void actionPerformed(ActionEvent e) {
lastNodeClicked = null;
}
}
//implements keyboard input for each gate type and panning
static void setUpKey() {
And.action and = new And.action();
Nand.action nand = new Nand.action();
Nor.action nor = new Nor.action();
Or.action or = new Or.action();
Xor.action xor = new Xor.action();
Xnor.action xnor = new Xnor.action();
Not.action not = new Not.action();
Switch.action swith = new Switch.action();
Light.action light = new Light.action();
Arrow.Up up = new Arrow.Up();
Arrow.Down down = new Arrow.Down();
Arrow.Left left = new Arrow.Left();
Arrow.Right right = new Arrow.Right();
Node.setNextInput1 set1 = new Node.setNextInput1();
Node.setNextInput2 set2 = new Node.setNextInput2();
CtrlZ ctrlZ = new CtrlZ();
CtrlY ctrlY = new CtrlY();
connectionNull nulla = new connectionNull();
tickbutton tickb = new tickbutton();
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "tick");
p.getActionMap().put("tick", tickb);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), "NULL");
p.getActionMap().put("NULL", nulla);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD1, 0), "AND");
p.getActionMap().put("AND", and);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, 0), "NAND");
p.getActionMap().put("NAND", nand);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD3, 0), "NOR");
p.getActionMap().put("NOR", nor);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD4, 0), "OR");
p.getActionMap().put("OR", or);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD5, 0), "XOR");
p.getActionMap().put("XOR", xor);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD6, 0), "XNOR");
p.getActionMap().put("XNOR", xnor);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD7, 0), "NOT");
p.getActionMap().put("NOT", not);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD8, 0), "SWITCH");
p.getActionMap().put("SWITCH", swith);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD9, 0), "LIGHT");
p.getActionMap().put("LIGHT", light);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UP");
p.getActionMap().put("UP", up);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DOWN");
p.getActionMap().put("DOWN", down);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LEFT");
p.getActionMap().put("LEFT", left);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RIGHT");
p.getActionMap().put("RIGHT", right);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0), "CTRLZ");
p.getActionMap().put("CTRLZ", ctrlZ);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 0), "CTRLY");
p.getActionMap().put("CTRLY", ctrlY);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), "IN1");
p.getActionMap().put("IN1", set1);
p.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DECIMAL, 0), "IN2");
p.getActionMap().put("IN2", set2);
}
}