RandomSwitch.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package exampleAlgorithms;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.util.Hashtable;
  5. import javax.swing.BorderFactory;
  6. import javax.swing.BoxLayout;
  7. import javax.swing.JButton;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import javax.swing.JSlider;
  11. import api.Algorithm;
  12. import classes.HolonSwitch;
  13. import ui.controller.Control;
  14. public class RandomSwitch implements Algorithm {
  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. // }
  27. public RandomSwitch(){
  28. content.setLayout(new BorderLayout());
  29. content.add(createParameterPanel(), BorderLayout.CENTER);
  30. JButton buttonRun = new JButton("Run");
  31. buttonRun.addActionListener(actionEvent -> run());
  32. content.add(buttonRun, BorderLayout.PAGE_END);
  33. content.setPreferredSize(new Dimension(300,300));
  34. }
  35. private JPanel createParameterPanel() {
  36. JPanel parameterPanel = new JPanel();
  37. parameterPanel.setLayout(new BoxLayout(parameterPanel, BoxLayout.PAGE_AXIS));
  38. parameterPanel.add(createFlipChanceSlider());
  39. return parameterPanel;
  40. }
  41. private JSlider createFlipChanceSlider() {
  42. JSlider flipChance =new JSlider(JSlider.HORIZONTAL,0, 100, 50);
  43. flipChance.setBorder(BorderFactory.createTitledBorder("FlipChance"));
  44. flipChance.setMajorTickSpacing(50);
  45. flipChance.setMinorTickSpacing(5);
  46. flipChance.setPaintTicks(true);
  47. Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
  48. labelTable.put( new Integer( 0 ), new JLabel("0.0") );
  49. labelTable.put( new Integer( 50 ), new JLabel("0.5") );
  50. labelTable.put( new Integer( 100 ), new JLabel("1.0") );
  51. flipChance.addChangeListener(actionEvent ->randomChance =(double)flipChance.getValue()/100.0);
  52. flipChance.setLabelTable( labelTable );
  53. flipChance.setPaintLabels(true);
  54. return flipChance;
  55. }
  56. private void run() {
  57. for (HolonSwitch s : control.getModel().getSwitches()) {
  58. // Set to Manual Mode
  59. s.setManualMode(true);
  60. // Generate a random number between 0 and 1
  61. double randomDouble = Math.random();
  62. if (randomDouble < randomChance) {
  63. s.setManualState(!s.getActiveManual());
  64. }
  65. }
  66. control.calculateStateForCurrentTimeStep();
  67. control.updateCanvas();
  68. }
  69. @Override
  70. public JPanel getAlgorithmPanel() {
  71. return content;
  72. }
  73. @Override
  74. public void setController(Control control) {
  75. this.control = control;
  76. }
  77. }