From: Daniel Kirschten Date: Mon, 20 May 2019 17:31:07 +0000 (+0200) Subject: Merged logicui into master X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=fb3edf0f514d574d60cc5874b4d53ebaaa34b7e1;p=Mograsim.git Merged logicui into master --- fb3edf0f514d574d60cc5874b4d53ebaaa34b7e1 diff --cc .gitmodules index a128354d,74725734..c6d72056 --- a/.gitmodules +++ b/.gitmodules @@@ -1,8 -1,4 +1,8 @@@ - [submodule "logicui"] - path = logicui - url =
- branch = logicui +[submodule "SampleERCP"] + path = SampleERCP + url =
+ branch = SampleERCP + [submodule "SWTHelper"] + path = SWTHelper + url = https://github.com/Haspamelodica/SWTHelper + branch = master diff --cc LogicUI/.classpath index 00000000,effa05bc..56baf239 mode 000000,100644..100644 --- a/LogicUI/.classpath +++ b/LogicUI/.classpath @@@ -1,0 -1,13 +1,13 @@@ - - - - - - - - - - - - - ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ diff --cc LogicUI/.project index 00000000,b5e0280d..8b40ae19 mode 000000,100644..100644 --- a/LogicUI/.project +++ b/LogicUI/.project @@@ -1,0 -1,17 +1,17 @@@ - - - LogicUI - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - ++ ++ ++ LogicUI ++ ++ ++ ++ ++ ++ org.eclipse.jdt.core.javabuilder ++ ++ ++ ++ ++ ++ org.eclipse.jdt.core.javanature ++ ++ diff --cc era.mi/.settings/org.eclipse.jdt.ui.prefs index 7c7eca30,00000000..07b37c11 mode 100644,000000..100644 --- a/era.mi/.settings/org.eclipse.jdt.ui.prefs +++ b/era.mi/.settings/org.eclipse.jdt.ui.prefs @@@ -1,63 -1,0 +1,63 @@@ - eclipse.preferences.version=1 - editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true - formatter_profile=_ERA-MI - formatter_settings_version=16 - sp_cleanup.add_default_serial_version_id=true - sp_cleanup.add_generated_serial_version_id=false - sp_cleanup.add_missing_annotations=true - sp_cleanup.add_missing_deprecated_annotations=true - sp_cleanup.add_missing_methods=false - sp_cleanup.add_missing_nls_tags=false - sp_cleanup.add_missing_override_annotations=true - sp_cleanup.add_missing_override_annotations_interface_methods=true - sp_cleanup.add_serial_version_id=false - sp_cleanup.always_use_blocks=false - sp_cleanup.always_use_parentheses_in_expressions=false - sp_cleanup.always_use_this_for_non_static_field_access=false - sp_cleanup.always_use_this_for_non_static_method_access=false - sp_cleanup.convert_functional_interfaces=true - sp_cleanup.convert_to_enhanced_for_loop=true - sp_cleanup.correct_indentation=true - sp_cleanup.format_source_code=true - sp_cleanup.format_source_code_changes_only=false - sp_cleanup.insert_inferred_type_arguments=false - sp_cleanup.make_local_variable_final=true - sp_cleanup.make_parameters_final=false - sp_cleanup.make_private_fields_final=true - sp_cleanup.make_type_abstract_if_missing_method=false - sp_cleanup.make_variable_declarations_final=true - sp_cleanup.never_use_blocks=true - sp_cleanup.never_use_parentheses_in_expressions=true - sp_cleanup.on_save_use_additional_actions=false - sp_cleanup.organize_imports=true - sp_cleanup.qualify_static_field_accesses_with_declaring_class=false - sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true - sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true - sp_cleanup.qualify_static_member_accesses_with_declaring_class=true - sp_cleanup.qualify_static_method_accesses_with_declaring_class=false - sp_cleanup.remove_private_constructors=true - sp_cleanup.remove_redundant_modifiers=true - sp_cleanup.remove_redundant_semicolons=true - sp_cleanup.remove_redundant_type_arguments=true - sp_cleanup.remove_trailing_whitespaces=true - sp_cleanup.remove_trailing_whitespaces_all=true - sp_cleanup.remove_trailing_whitespaces_ignore_empty=false - sp_cleanup.remove_unnecessary_casts=true - sp_cleanup.remove_unnecessary_nls_tags=true - sp_cleanup.remove_unused_imports=true - sp_cleanup.remove_unused_local_variables=false - sp_cleanup.remove_unused_private_fields=true - sp_cleanup.remove_unused_private_members=false - sp_cleanup.remove_unused_private_methods=true - sp_cleanup.remove_unused_private_types=true - sp_cleanup.sort_members=false - sp_cleanup.sort_members_all=false - sp_cleanup.use_anonymous_class_creation=false - sp_cleanup.use_blocks=false - sp_cleanup.use_blocks_only_for_return_and_throw=false - sp_cleanup.use_lambda=true - sp_cleanup.use_parentheses_in_expressions=false - sp_cleanup.use_this_for_non_static_field_access=true - sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true - sp_cleanup.use_this_for_non_static_method_access=true - sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true ++eclipse.preferences.version=1 ++editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true ++formatter_profile=_ERA-MI ++formatter_settings_version=16 ++sp_cleanup.add_default_serial_version_id=true ++sp_cleanup.add_generated_serial_version_id=false ++sp_cleanup.add_missing_annotations=true ++sp_cleanup.add_missing_deprecated_annotations=true ++sp_cleanup.add_missing_methods=false ++sp_cleanup.add_missing_nls_tags=false ++sp_cleanup.add_missing_override_annotations=true ++sp_cleanup.add_missing_override_annotations_interface_methods=true ++sp_cleanup.add_serial_version_id=false ++sp_cleanup.always_use_blocks=false ++sp_cleanup.always_use_parentheses_in_expressions=false ++sp_cleanup.always_use_this_for_non_static_field_access=false ++sp_cleanup.always_use_this_for_non_static_method_access=false ++sp_cleanup.convert_functional_interfaces=true ++sp_cleanup.convert_to_enhanced_for_loop=true ++sp_cleanup.correct_indentation=true ++sp_cleanup.format_source_code=true ++sp_cleanup.format_source_code_changes_only=false ++sp_cleanup.insert_inferred_type_arguments=false ++sp_cleanup.make_local_variable_final=true ++sp_cleanup.make_parameters_final=false ++sp_cleanup.make_private_fields_final=true ++sp_cleanup.make_type_abstract_if_missing_method=false ++sp_cleanup.make_variable_declarations_final=true ++sp_cleanup.never_use_blocks=true ++sp_cleanup.never_use_parentheses_in_expressions=true ++sp_cleanup.on_save_use_additional_actions=false ++sp_cleanup.organize_imports=true ++sp_cleanup.qualify_static_field_accesses_with_declaring_class=false ++sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true ++sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true ++sp_cleanup.qualify_static_member_accesses_with_declaring_class=true ++sp_cleanup.qualify_static_method_accesses_with_declaring_class=false ++sp_cleanup.remove_private_constructors=true ++sp_cleanup.remove_redundant_modifiers=true ++sp_cleanup.remove_redundant_semicolons=true ++sp_cleanup.remove_redundant_type_arguments=true ++sp_cleanup.remove_trailing_whitespaces=true ++sp_cleanup.remove_trailing_whitespaces_all=true ++sp_cleanup.remove_trailing_whitespaces_ignore_empty=false ++sp_cleanup.remove_unnecessary_casts=true ++sp_cleanup.remove_unnecessary_nls_tags=true ++sp_cleanup.remove_unused_imports=true ++sp_cleanup.remove_unused_local_variables=false ++sp_cleanup.remove_unused_private_fields=true ++sp_cleanup.remove_unused_private_members=false ++sp_cleanup.remove_unused_private_methods=true ++sp_cleanup.remove_unused_private_types=true ++sp_cleanup.sort_members=false ++sp_cleanup.sort_members_all=false ++sp_cleanup.use_anonymous_class_creation=false ++sp_cleanup.use_blocks=false ++sp_cleanup.use_blocks_only_for_return_and_throw=false ++sp_cleanup.use_lambda=true ++sp_cleanup.use_parentheses_in_expressions=false ++sp_cleanup.use_this_for_non_static_field_access=true ++sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true ++sp_cleanup.use_this_for_non_static_method_access=true ++sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --cc era.mi/src/era/mi/logic/components/ManualSwitch.java index c4b4ffa3,00000000..0ad4a76c mode 100644,000000..100644 --- a/era.mi/src/era/mi/logic/components/ManualSwitch.java +++ b/era.mi/src/era/mi/logic/components/ManualSwitch.java @@@ -1,71 -1,0 +1,71 @@@ - package era.mi.logic.components; - - import java.util.List; - - import era.mi.logic.types.Bit; - import era.mi.logic.wires.Wire.WireEnd; - - /** - * This class models a simple on/off (ONE/ZERO) switch for user interaction. - * - * @author Christian Femers - * - */ - public class ManualSwitch implements Component - { - private WireEnd output; - private boolean isOn; - - public ManualSwitch(WireEnd output) - { - if (output.length() != 1) - throw new IllegalArgumentException("Switch output can be only a single wire"); - this.output = output; - } - - public void switchOn() - { - setState(true); - } - - public void switchOff() - { - setState(false); - } - - public void toggle() - { - setState(!isOn); - } - - public void setState(boolean isOn) - { - if (this.isOn == isOn) - return; - this.isOn = isOn; - output.feedSignals(getValue()); - } - - public boolean isOn() - { - return isOn; - } - - public Bit getValue() - { - return isOn ? Bit.ONE : Bit.ZERO; - } - - @Override - public List getAllInputs() - { - return List.of(); - } - - @Override - public List getAllOutputs() - { - return List.of(output); - } - - } ++package era.mi.logic.components; ++ ++import java.util.List; ++ ++import era.mi.logic.types.Bit; ++import era.mi.logic.wires.Wire.WireEnd; ++ ++/** ++ * This class models a simple on/off (ONE/ZERO) switch for user interaction. ++ * ++ * @author Christian Femers ++ * ++ */ ++public class ManualSwitch implements Component ++{ ++ private WireEnd output; ++ private boolean isOn; ++ ++ public ManualSwitch(WireEnd output) ++ { ++ if (output.length() != 1) ++ throw new IllegalArgumentException("Switch output can be only a single wire"); ++ this.output = output; ++ } ++ ++ public void switchOn() ++ { ++ setState(true); ++ } ++ ++ public void switchOff() ++ { ++ setState(false); ++ } ++ ++ public void toggle() ++ { ++ setState(!isOn); ++ } ++ ++ public void setState(boolean isOn) ++ { ++ if (this.isOn == isOn) ++ return; ++ this.isOn = isOn; ++ output.feedSignals(getValue()); ++ } ++ ++ public boolean isOn() ++ { ++ return isOn; ++ } ++ ++ public Bit getValue() ++ { ++ return isOn ? Bit.ONE : Bit.ZERO; ++ } ++ ++ @Override ++ public List getAllInputs() ++ { ++ return List.of(); ++ } ++ ++ @Override ++ public List getAllOutputs() ++ { ++ return List.of(output); ++ } ++ ++} diff --cc era.mi/src/era/mi/logic/tests/GUITest.java index adc4c32a,00000000..b93d4b6b mode 100644,000000..100644 --- a/era.mi/src/era/mi/logic/tests/GUITest.java +++ b/era.mi/src/era/mi/logic/tests/GUITest.java @@@ -1,294 -1,0 +1,294 @@@ - package era.mi.logic.tests; - - import java.awt.Color; - import java.awt.Graphics; - import java.awt.Graphics2D; - import java.awt.Rectangle; - import java.awt.RenderingHints; - import java.awt.event.MouseEvent; - import java.awt.event.MouseListener; - import java.util.HashMap; - import java.util.Map; - import java.util.Map.Entry; - - import javax.swing.JFrame; - import javax.swing.JPanel; - import javax.swing.WindowConstants; - - import era.mi.logic.Simulation; - import era.mi.logic.components.ManualSwitch; - import era.mi.logic.components.gates.NotGate; - import era.mi.logic.components.gates.OrGate; - import era.mi.logic.timeline.Timeline.ExecutionResult; - import era.mi.logic.wires.Wire; - - public class GUITest extends JPanel - { - - private static final long serialVersionUID = 1L; - - private static final int WIRE_DELAY = 40; - private static final int OR_DELAY = 100; - private static final int NOT_DELAY = 100; - - Wire r = new Wire(1, WIRE_DELAY); - Wire s = new Wire(1, WIRE_DELAY); - Wire t1 = new Wire(1, WIRE_DELAY); - Wire t2 = new Wire(1, WIRE_DELAY); - Wire q = new Wire(1, WIRE_DELAY); - Wire nq = new Wire(1, WIRE_DELAY); - - ManualSwitch rIn = new ManualSwitch(r.createEnd()); - ManualSwitch sIn = new ManualSwitch(s.createEnd()); - - OrGate or1 = new OrGate(OR_DELAY, t2.createEnd(), r.createEnd(), nq.createEnd()); - OrGate or2 = new OrGate(OR_DELAY, t1.createEnd(), s.createEnd(), q.createEnd()); - NotGate not1 = new NotGate(NOT_DELAY, t2.createEnd(), q.createEnd()); - NotGate not2 = new NotGate(NOT_DELAY, t1.createEnd(), nq.createEnd()); - - Map switchMap = new HashMap<>(); - - int height; - int width; - boolean sizeChanged; - - public GUITest() - { - addMouseListener(new MouseListener() - { - - @Override - public void mouseReleased(MouseEvent e) - { - for (Entry dim : switchMap.entrySet()) - { - if (dim.getValue().contains(e.getPoint())) - { - dim.getKey().switchOff(); - repaint(); - } - } - } - - @Override - public void mousePressed(MouseEvent e) - { - for (Entry dim : switchMap.entrySet()) - { - if (dim.getValue().contains(e.getPoint())) - { - dim.getKey().switchOn(); - repaint(); - } - } - } - - @Override - public void mouseExited(MouseEvent e) - { - // none - } - - @Override - public void mouseEntered(MouseEvent e) - { - // none - } - - @Override - public void mouseClicked(MouseEvent e) - { - // If you want toggle buttons, use this code instead - // for (Entry dim : switchMap.entrySet()) { - // if (dim.getValue().contains(e.getPoint())) { - // dim.getKey().toggle(); - // repaint(); - // } - // } - } - }); - } - - @Override - public void paint(Graphics some_g) - { - super.paint(some_g); - Graphics2D g = ((Graphics2D) some_g); - g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); - - checkSizeChange(); - adaptFont(g); - - drawWire(g, r, "r", 2, 9, 4, 9); - - drawWire(g, s, "s", 2, 3, 4, 3); - - drawWire(g, t2, "t2", 5, 8.5, 6, 8.5); - - drawWire(g, t1, "t1", 5, 3.5, 6, 3.5); - - drawWire(g, q, "q", 7, 8.5, 9, 8.5); - - drawWire(g, nq, "nq", 7, 3.5, 9, 3.5); - - drawWire(g, q, "", 7.5, 8.5, 7.5, 7.5); - drawWire(g, q, "", 7.5, 7.5, 3, 4.5); - drawWire(g, q, "", 3, 4.5, 3, 4); - drawWire(g, q, "q", 3, 4, 4, 4); - - drawWire(g, nq, "", 7.5, 3.5, 7.5, 4.5); - drawWire(g, nq, "", 7.5, 4.5, 3, 7.5); - drawWire(g, nq, "", 3, 7.5, 3, 8); - drawWire(g, nq, "nq", 3, 8, 4, 8); - - drawSquare(g, 4, 8, "OR"); - drawSquare(g, 4, 3, "OR"); - - drawSquare(g, 6, 8, "NOT"); - drawSquare(g, 6, 3, "NOT"); - - drawSwitch(g, rIn, "Switch R", 0.5, 8.25, 2, 9.75); - drawSwitch(g, sIn, "Switch S", 0.5, 2.25, 2, 3.75); - - drawString(g, "Hint: drag the cursor out of the pressed switch to keep it's state", 5, 0, 0.0, 1.0); - } - - private void checkSizeChange() - { - sizeChanged = height != getHeight() || width != getWidth(); - if (sizeChanged) - { - height = getHeight(); - width = getWidth(); - } - } - - private void adaptFont(Graphics g) - { - g.setFont(g.getFont().deriveFont(Math.min(height, width) / 40f)); - } - - private void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY) - { - int h = g.getFontMetrics().getAscent(); - int w = g.getFontMetrics().stringWidth(s); - g.drawString(s, x - (int) (w * anchorX), y + (int) (h * anchorY)); - } - - private void drawWire(Graphics g, Wire wa, String name, double x1, double y1, double x2, double y2) - { - setTo(g, wa); - g.drawLine(gX(x1), gY(y1), gX(x2), gY(y2)); - drawString(g, name, (gX(x1) + gX(x2)) / 2, (gY(y1) + gY(y2)) / 2 - 5, 0, 0); - } - - private void drawSquare(Graphics g, int posX, int posY, String text) - { - int x1 = gX(posX) - 5; - int x2 = gX(posX + 1) + 5; - int y1 = gY(posY) - 5; - int y2 = gY(posY + 1) + 5; - - g.setColor(Color.WHITE); - g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); - setBlack(g); - g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); - drawString(g, text, (x1 + x2) / 2, (y1 + y2) / 2, 0.5, 0.5); - - } - - private void drawSwitch(Graphics g, ManualSwitch ms, String text, double posX1, double posY1, double posX2, double posY2) - { - int x1 = gX(posX1) - 5; - int x2 = gX(posX2) + 5; - int y1 = gY(posY1) - 5; - int y2 = gY(posY2) + 5; - - if (sizeChanged) - { - Rectangle r = new Rectangle(x1, y1, x2 - x1, y2 - y1); - switchMap.put(ms, r); - } - - g.setColor(ms.isOn() ? Color.getHSBColor(.3f, .5f, 1f) : Color.WHITE); - g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); - setBlack(g); - g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); - drawString(g, text, (x1 + x2) / 2, (y1 + y2) / 2, 0.5, 0.5); - } - - private static void setBlack(Graphics g) - { - g.setColor(Color.BLACK); - } - - private static void setTo(Graphics g, Wire wa) - { - switch (wa.getValue()) - { - case ONE: - g.setColor(Color.GREEN); - break; - case X: - g.setColor(Color.RED); - break; - case Z: - g.setColor(Color.DARK_GRAY); - break; - case ZERO: - g.setColor(Color.BLACK); - break; - case U: - g.setColor(Color.MAGENTA); - break; - default: - throw new IllegalArgumentException(); - } - } - - private int gY(double pos) - { - return (int) (pos * height / 11); - } - - private int gX(double pos) - { - return (int) (pos * width / 11) + 50; - } - - public static void main(String[] args) - { - JFrame f = new JFrame("Test circuit 1.0.0"); - GUITest gt = new GUITest(); - f.add(gt); - f.setSize(800, 600); - f.setLocation(500, 400); - f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - f.setVisible(true); - - long begin = System.currentTimeMillis(); - - long lastFrame = begin; - long updateT = 16; - - while (f.isVisible()) - { - ExecutionResult er = Simulation.TIMELINE.executeUpTo((lastFrame - begin) * 3, lastFrame + 14); - // if (Simulation.TIMELINE.hasNext()) - // Simulation.TIMELINE.executeNext(); - if (er != ExecutionResult.NOTHING_DONE) - gt.repaint(12); - try - { - Thread.sleep(Math.max(updateT - System.currentTimeMillis() + lastFrame, 0)); - } - catch (Exception e) - { - e.printStackTrace(); - } - lastFrame = System.currentTimeMillis(); - } - } - } ++package era.mi.logic.tests; ++ ++import java.awt.Color; ++import java.awt.Graphics; ++import java.awt.Graphics2D; ++import java.awt.Rectangle; ++import java.awt.RenderingHints; ++import java.awt.event.MouseEvent; ++import java.awt.event.MouseListener; ++import java.util.HashMap; ++import java.util.Map; ++import java.util.Map.Entry; ++ ++import javax.swing.JFrame; ++import javax.swing.JPanel; ++import javax.swing.WindowConstants; ++ ++import era.mi.logic.Simulation; ++import era.mi.logic.components.ManualSwitch; ++import era.mi.logic.components.gates.NotGate; ++import era.mi.logic.components.gates.OrGate; ++import era.mi.logic.timeline.Timeline.ExecutionResult; ++import era.mi.logic.wires.Wire; ++ ++public class GUITest extends JPanel ++{ ++ ++ private static final long serialVersionUID = 1L; ++ ++ private static final int WIRE_DELAY = 40; ++ private static final int OR_DELAY = 100; ++ private static final int NOT_DELAY = 100; ++ ++ Wire r = new Wire(1, WIRE_DELAY); ++ Wire s = new Wire(1, WIRE_DELAY); ++ Wire t1 = new Wire(1, WIRE_DELAY); ++ Wire t2 = new Wire(1, WIRE_DELAY); ++ Wire q = new Wire(1, WIRE_DELAY); ++ Wire nq = new Wire(1, WIRE_DELAY); ++ ++ ManualSwitch rIn = new ManualSwitch(r.createEnd()); ++ ManualSwitch sIn = new ManualSwitch(s.createEnd()); ++ ++ OrGate or1 = new OrGate(OR_DELAY, t2.createEnd(), r.createEnd(), nq.createEnd()); ++ OrGate or2 = new OrGate(OR_DELAY, t1.createEnd(), s.createEnd(), q.createEnd()); ++ NotGate not1 = new NotGate(NOT_DELAY, t2.createEnd(), q.createEnd()); ++ NotGate not2 = new NotGate(NOT_DELAY, t1.createEnd(), nq.createEnd()); ++ ++ Map switchMap = new HashMap<>(); ++ ++ int height; ++ int width; ++ boolean sizeChanged; ++ ++ public GUITest() ++ { ++ addMouseListener(new MouseListener() ++ { ++ ++ @Override ++ public void mouseReleased(MouseEvent e) ++ { ++ for (Entry dim : switchMap.entrySet()) ++ { ++ if (dim.getValue().contains(e.getPoint())) ++ { ++ dim.getKey().switchOff(); ++ repaint(); ++ } ++ } ++ } ++ ++ @Override ++ public void mousePressed(MouseEvent e) ++ { ++ for (Entry dim : switchMap.entrySet()) ++ { ++ if (dim.getValue().contains(e.getPoint())) ++ { ++ dim.getKey().switchOn(); ++ repaint(); ++ } ++ } ++ } ++ ++ @Override ++ public void mouseExited(MouseEvent e) ++ { ++ // none ++ } ++ ++ @Override ++ public void mouseEntered(MouseEvent e) ++ { ++ // none ++ } ++ ++ @Override ++ public void mouseClicked(MouseEvent e) ++ { ++ // If you want toggle buttons, use this code instead ++// for (Entry dim : switchMap.entrySet()) { ++// if (dim.getValue().contains(e.getPoint())) { ++// dim.getKey().toggle(); ++// repaint(); ++// } ++// } ++ } ++ }); ++ } ++ ++ @Override ++ public void paint(Graphics some_g) ++ { ++ super.paint(some_g); ++ Graphics2D g = ((Graphics2D) some_g); ++ g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); ++ g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ++ g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); ++ ++ checkSizeChange(); ++ adaptFont(g); ++ ++ drawWire(g, r, "r", 2, 9, 4, 9); ++ ++ drawWire(g, s, "s", 2, 3, 4, 3); ++ ++ drawWire(g, t2, "t2", 5, 8.5, 6, 8.5); ++ ++ drawWire(g, t1, "t1", 5, 3.5, 6, 3.5); ++ ++ drawWire(g, q, "q", 7, 8.5, 9, 8.5); ++ ++ drawWire(g, nq, "nq", 7, 3.5, 9, 3.5); ++ ++ drawWire(g, q, "", 7.5, 8.5, 7.5, 7.5); ++ drawWire(g, q, "", 7.5, 7.5, 3, 4.5); ++ drawWire(g, q, "", 3, 4.5, 3, 4); ++ drawWire(g, q, "q", 3, 4, 4, 4); ++ ++ drawWire(g, nq, "", 7.5, 3.5, 7.5, 4.5); ++ drawWire(g, nq, "", 7.5, 4.5, 3, 7.5); ++ drawWire(g, nq, "", 3, 7.5, 3, 8); ++ drawWire(g, nq, "nq", 3, 8, 4, 8); ++ ++ drawSquare(g, 4, 8, "OR"); ++ drawSquare(g, 4, 3, "OR"); ++ ++ drawSquare(g, 6, 8, "NOT"); ++ drawSquare(g, 6, 3, "NOT"); ++ ++ drawSwitch(g, rIn, "Switch R", 0.5, 8.25, 2, 9.75); ++ drawSwitch(g, sIn, "Switch S", 0.5, 2.25, 2, 3.75); ++ ++ drawString(g, "Hint: drag the cursor out of the pressed switch to keep it's state", 5, 0, 0.0, 1.0); ++ } ++ ++ private void checkSizeChange() ++ { ++ sizeChanged = height != getHeight() || width != getWidth(); ++ if (sizeChanged) ++ { ++ height = getHeight(); ++ width = getWidth(); ++ } ++ } ++ ++ private void adaptFont(Graphics g) ++ { ++ g.setFont(g.getFont().deriveFont(Math.min(height, width) / 40f)); ++ } ++ ++ private void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY) ++ { ++ int h = g.getFontMetrics().getAscent(); ++ int w = g.getFontMetrics().stringWidth(s); ++ g.drawString(s, x - (int) (w * anchorX), y + (int) (h * anchorY)); ++ } ++ ++ private void drawWire(Graphics g, Wire wa, String name, double x1, double y1, double x2, double y2) ++ { ++ setTo(g, wa); ++ g.drawLine(gX(x1), gY(y1), gX(x2), gY(y2)); ++ drawString(g, name, (gX(x1) + gX(x2)) / 2, (gY(y1) + gY(y2)) / 2 - 5, 0, 0); ++ } ++ ++ private void drawSquare(Graphics g, int posX, int posY, String text) ++ { ++ int x1 = gX(posX) - 5; ++ int x2 = gX(posX + 1) + 5; ++ int y1 = gY(posY) - 5; ++ int y2 = gY(posY + 1) + 5; ++ ++ g.setColor(Color.WHITE); ++ g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); ++ setBlack(g); ++ g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); ++ drawString(g, text, (x1 + x2) / 2, (y1 + y2) / 2, 0.5, 0.5); ++ ++ } ++ ++ private void drawSwitch(Graphics g, ManualSwitch ms, String text, double posX1, double posY1, double posX2, double posY2) ++ { ++ int x1 = gX(posX1) - 5; ++ int x2 = gX(posX2) + 5; ++ int y1 = gY(posY1) - 5; ++ int y2 = gY(posY2) + 5; ++ ++ if (sizeChanged) ++ { ++ Rectangle r = new Rectangle(x1, y1, x2 - x1, y2 - y1); ++ switchMap.put(ms, r); ++ } ++ ++ g.setColor(ms.isOn() ? Color.getHSBColor(.3f, .5f, 1f) : Color.WHITE); ++ g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); ++ setBlack(g); ++ g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); ++ drawString(g, text, (x1 + x2) / 2, (y1 + y2) / 2, 0.5, 0.5); ++ } ++ ++ private static void setBlack(Graphics g) ++ { ++ g.setColor(Color.BLACK); ++ } ++ ++ private static void setTo(Graphics g, Wire wa) ++ { ++ switch (wa.getValue()) ++ { ++ case ONE: ++ g.setColor(Color.GREEN); ++ break; ++ case X: ++ g.setColor(Color.RED); ++ break; ++ case Z: ++ g.setColor(Color.DARK_GRAY); ++ break; ++ case ZERO: ++ g.setColor(Color.BLACK); ++ break; ++ case U: ++ g.setColor(Color.MAGENTA); ++ break; ++ default: ++ throw new IllegalArgumentException(); ++ } ++ } ++ ++ private int gY(double pos) ++ { ++ return (int) (pos * height / 11); ++ } ++ ++ private int gX(double pos) ++ { ++ return (int) (pos * width / 11) + 50; ++ } ++ ++ public static void main(String[] args) ++ { ++ JFrame f = new JFrame("Test circuit 1.0.0"); ++ GUITest gt = new GUITest(); ++ f.add(gt); ++ f.setSize(800, 600); ++ f.setLocation(500, 400); ++ f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); ++ f.setVisible(true); ++ ++ long begin = System.currentTimeMillis(); ++ ++ long lastFrame = begin; ++ long updateT = 16; ++ ++ while (f.isVisible()) ++ { ++ ExecutionResult er = Simulation.TIMELINE.executeUpTo((lastFrame - begin) * 3, lastFrame + 14); ++// if (Simulation.TIMELINE.hasNext()) ++// Simulation.TIMELINE.executeNext(); ++ if (er != ExecutionResult.NOTHING_DONE) ++ gt.repaint(12); ++ try ++ { ++ Thread.sleep(Math.max(updateT - System.currentTimeMillis() + lastFrame, 0)); ++ } ++ catch (Exception e) ++ { ++ e.printStackTrace(); ++ } ++ lastFrame = System.currentTimeMillis(); ++ } ++ } ++}