1 package era.mi.logic.tests;
4 import java.awt.Graphics;
5 import java.awt.Graphics2D;
6 import java.awt.Rectangle;
7 import java.awt.RenderingHints;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
10 import java.util.HashMap;
12 import java.util.Map.Entry;
14 import javax.swing.JFrame;
15 import javax.swing.JPanel;
16 import javax.swing.WindowConstants;
18 import era.mi.logic.Simulation;
19 import era.mi.logic.components.ManualSwitch;
20 import era.mi.logic.components.gates.NotGate;
21 import era.mi.logic.components.gates.OrGate;
22 import era.mi.logic.timeline.Timeline.ExecutionResult;
23 import era.mi.logic.wires.WireArray;
25 public class GUITest extends JPanel {
27 private static final long serialVersionUID = 1L;
29 private static final int WIRE_DELAY = 40;
30 private static final int OR_DELAY = 100;
31 private static final int NOT_DELAY = 100;
33 WireArray r = new WireArray(1, WIRE_DELAY);
34 WireArray s = new WireArray(1, WIRE_DELAY);
35 WireArray t1 = new WireArray(1, WIRE_DELAY);
36 WireArray t2 = new WireArray(1, WIRE_DELAY);
37 WireArray q = new WireArray(1, WIRE_DELAY);
38 WireArray nq = new WireArray(1, WIRE_DELAY);
40 ManualSwitch rIn = new ManualSwitch(r);
41 ManualSwitch sIn = new ManualSwitch(s);
43 OrGate or1 = new OrGate(OR_DELAY, t2, r, nq);
44 OrGate or2 = new OrGate(OR_DELAY, t1, s, q);
45 NotGate not1 = new NotGate(NOT_DELAY, t2, q);
46 NotGate not2 = new NotGate(NOT_DELAY, t1, nq);
48 Map<ManualSwitch, Rectangle> switchMap = new HashMap<>();
55 addMouseListener(new MouseListener() {
58 public void mouseReleased(MouseEvent e) {
59 for (Entry<ManualSwitch, Rectangle> dim : switchMap.entrySet()) {
60 if (dim.getValue().contains(e.getPoint())) {
61 dim.getKey().switchOff();
68 public void mousePressed(MouseEvent e) {
69 for (Entry<ManualSwitch, Rectangle> dim : switchMap.entrySet()) {
70 if (dim.getValue().contains(e.getPoint())) {
71 dim.getKey().switchOn();
78 public void mouseExited(MouseEvent e) {
83 public void mouseEntered(MouseEvent e) {
88 public void mouseClicked(MouseEvent e) {
89 // If you want toggle buttons, use this code instead
90 // for (Entry<ManualSwitch, Rectangle> dim : switchMap.entrySet()) {
91 // if (dim.getValue().contains(e.getPoint())) {
92 // dim.getKey().toggle();
101 public void paint(Graphics some_g) {
103 Graphics2D g = ((Graphics2D) some_g);
104 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
105 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
106 g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
111 drawWire(g, r, "r", 2, 9, 4, 9);
113 drawWire(g, s, "s", 2, 3, 4, 3);
115 drawWire(g, t2, "t2", 5, 8.5, 6, 8.5);
117 drawWire(g, t1, "t1", 5, 3.5, 6, 3.5);
119 drawWire(g, q, "q", 7, 8.5, 9, 8.5);
121 drawWire(g, nq, "nq", 7, 3.5, 9, 3.5);
123 drawWire(g, q, "", 7.5, 8.5, 7.5, 7.5);
124 drawWire(g, q, "", 7.5, 7.5, 3, 4.5);
125 drawWire(g, q, "", 3, 4.5, 3, 4);
126 drawWire(g, q, "q", 3, 4, 4, 4);
128 drawWire(g, nq, "", 7.5, 3.5, 7.5, 4.5);
129 drawWire(g, nq, "", 7.5, 4.5, 3, 7.5);
130 drawWire(g, nq, "", 3, 7.5, 3, 8);
131 drawWire(g, nq, "nq", 3, 8, 4, 8);
133 drawSquare(g, 4, 8, "OR");
134 drawSquare(g, 4, 3, "OR");
136 drawSquare(g, 6, 8, "NOT");
137 drawSquare(g, 6, 3, "NOT");
139 drawSwitch(g, rIn, "Switch R", 0.5, 8.25, 2, 9.75);
140 drawSwitch(g, sIn, "Switch S", 0.5, 2.25, 2, 3.75);
142 drawString(g, "Hint: drag the cursor out of the pressed switch to keep it's state", 5, 0, 0.0, 1.0);
145 private void checkSizeChange() {
146 sizeChanged = height != getHeight() || width != getWidth();
148 height = getHeight();
153 private void adaptFont(Graphics g) {
154 g.setFont(g.getFont().deriveFont(Math.min(height, width) / 40f));
157 private void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY) {
158 int h = g.getFontMetrics().getAscent();
159 int w = g.getFontMetrics().stringWidth(s);
160 g.drawString(s, x - (int) (w * anchorX), y + (int) (h * anchorY));
163 private void drawWire(Graphics g, WireArray wa, String name, double x1, double y1, double x2, double y2) {
165 g.drawLine(gX(x1), gY(y1), gX(x2), gY(y2));
166 drawString(g, name, (gX(x1) + gX(x2)) / 2, (gY(y1) + gY(y2)) / 2 - 5, 0, 0);
169 private void drawSquare(Graphics g, int posX, int posY, String text) {
170 int x1 = gX(posX) - 5;
171 int x2 = gX(posX + 1) + 5;
172 int y1 = gY(posY) - 5;
173 int y2 = gY(posY + 1) + 5;
175 g.setColor(Color.WHITE);
176 g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
178 g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
179 drawString(g, text, (x1 + x2) / 2, (y1 + y2) / 2, 0.5, 0.5);
183 private void drawSwitch(Graphics g, ManualSwitch ms, String text, double posX1, double posY1, double posX2, double posY2) {
184 int x1 = gX(posX1) - 5;
185 int x2 = gX(posX2) + 5;
186 int y1 = gY(posY1) - 5;
187 int y2 = gY(posY2) + 5;
190 Rectangle r = new Rectangle(x1, y1, x2 - x1, y2 - y1);
191 switchMap.put(ms, r);
194 g.setColor(ms.isOn() ? Color.getHSBColor(.3f, .5f, 1f) : Color.WHITE);
195 g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
197 g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
198 drawString(g, text, (x1 + x2) / 2, (y1 + y2) / 2, 0.5, 0.5);
201 private static void setBlack(Graphics g) {
202 g.setColor(Color.BLACK);
205 private static void setTo(Graphics g, WireArray wa) {
206 switch (wa.getValue()) {
208 g.setColor(Color.GREEN);
211 g.setColor(Color.RED);
214 g.setColor(Color.DARK_GRAY);
217 g.setColor(Color.BLACK);
220 throw new IllegalArgumentException();
224 private int gY(double pos) {
225 return (int) (pos * height / 11);
228 private int gX(double pos) {
229 return (int) (pos * width / 11) + 50;
232 public static void main(String[] args) {
233 JFrame f = new JFrame("Test circuit 1.0.0");
234 GUITest gt = new GUITest();
237 f.setLocation(500, 400);
238 f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
241 long begin = System.currentTimeMillis();
243 long lastFrame = begin;
246 while (f.isVisible()) {
247 ExecutionResult er = Simulation.TIMELINE.executeUpTo((lastFrame - begin) * 3, lastFrame + 14);
248 // if (Simulation.TIMELINE.hasNext())
249 // Simulation.TIMELINE.executeNext();
250 if (er != ExecutionResult.NOTHING_DONE)
253 Thread.sleep(Math.max(16 - System.currentTimeMillis() + lastFrame, 0));
254 } catch (Exception e) {
257 lastFrame = System.currentTimeMillis();