RandomSwitch.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package holeg.addon;
  2. import holeg.api.AddOn;
  3. import holeg.model.HolonSwitch.SwitchMode;
  4. import holeg.ui.controller.Control;
  5. import java.awt.BorderLayout;
  6. import java.awt.Dimension;
  7. import java.util.Hashtable;
  8. import javax.swing.BorderFactory;
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JButton;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.JSlider;
  14. public class RandomSwitch implements AddOn {
  15. private double randomChance = 0.5;
  16. private Control control;
  17. private JPanel content = new JPanel();
  18. // To Test the Layout Faster
  19. // public static void main(String[] args)
  20. // {
  21. // JFrame newFrame = new JFrame("exampleWindow");
  22. // RandomSwitch instance = new RandomSwitch();
  23. // newFrame.setContentPane(instance.getAlgorithmPanel());
  24. // newFrame.pack();
  25. // newFrame.setVisible(true);
  26. // newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. // }
  28. public RandomSwitch() {
  29. content.setLayout(new BorderLayout());
  30. content.add(createParameterPanel(), BorderLayout.CENTER);
  31. JButton buttonRun = new JButton("Run");
  32. buttonRun.addActionListener(actionEvent -> run());
  33. content.add(buttonRun, BorderLayout.PAGE_END);
  34. content.setPreferredSize(new Dimension(300, 300));
  35. }
  36. private JPanel createParameterPanel() {
  37. JPanel parameterPanel = new JPanel();
  38. parameterPanel.setLayout(new BoxLayout(parameterPanel, BoxLayout.PAGE_AXIS));
  39. parameterPanel.add(createFlipChanceSlider());
  40. return parameterPanel;
  41. }
  42. private JSlider createFlipChanceSlider() {
  43. JSlider flipChance = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
  44. flipChance.setBorder(BorderFactory.createTitledBorder("FlipChance"));
  45. flipChance.setMajorTickSpacing(50);
  46. flipChance.setMinorTickSpacing(5);
  47. flipChance.setPaintTicks(true);
  48. Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
  49. labelTable.put(Integer.valueOf(0), new JLabel("0.0"));
  50. labelTable.put(Integer.valueOf(50), new JLabel("0.5"));
  51. labelTable.put(Integer.valueOf(100), new JLabel("1.0"));
  52. flipChance.addChangeListener(
  53. actionEvent -> randomChance = (double) flipChance.getValue() / 100.0);
  54. flipChance.setLabelTable(labelTable);
  55. flipChance.setPaintLabels(true);
  56. return flipChance;
  57. }
  58. private void run() {
  59. control.getModel().getCanvas().getAllSwitchObjectsRecursive().forEach(s -> {
  60. // Set to Manual Mode
  61. s.setMode(SwitchMode.Manual);
  62. // Generate a random number between 0 and 1
  63. double randomDouble = Math.random();
  64. if (randomDouble < randomChance) {
  65. s.flipManualState();
  66. }
  67. });
  68. control.calculateStateForCurrentIteration();
  69. }
  70. @Override
  71. public JPanel getPanel() {
  72. return content;
  73. }
  74. @Override
  75. public void setController(Control control) {
  76. this.control = control;
  77. }
  78. }