Controls.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import java.io.*;
  2. import java.util.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6. import java.awt.event.*;
  7. import javax.swing.ImageIcon;
  8. public class Controls extends JPanel implements Observer
  9. {
  10. private Model model;
  11. private playSlider playback;
  12. private JButton play;
  13. private JButton rewind;
  14. private JButton reset;
  15. private JButton clear;
  16. // Bob the Builder this shit
  17. public Controls(Model model, Handler handler)
  18. {
  19. // Hook up this observer so that it will be notified when the model
  20. // changes.
  21. this.model = model;
  22. this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  23. this.setPreferredSize(new Dimension(1280,75));
  24. this.setMinimumSize(new Dimension(320, 75));
  25. ImageIcon icon1 = new ImageIcon("/src/main/resources/playbutton.jpg");
  26. this.play = new JButton(icon1);
  27. play.addActionListener(new ActionListener()
  28. {
  29. public void actionPerformed(ActionEvent e)
  30. {
  31. }
  32. });
  33. this.rewind = new JButton("Rewind");
  34. rewind.addActionListener(new ActionListener()
  35. {
  36. public void actionPerformed(ActionEvent e)
  37. {
  38. }
  39. });
  40. this.playback = new playSlider(0, 0, 0);
  41. playback.addChangeListener(new ChangeListener()
  42. {
  43. public void stateChanged(ChangeEvent e)
  44. {
  45. if (!playback.getSettingLen() && playback.getValue() != model.getTotalVisibleLen())
  46. {
  47. model.changeThingy(playback.getValue());
  48. }
  49. }
  50. });
  51. this.playback.setPreferredSize(new Dimension(280, 75));
  52. this.reset = new JButton("End");
  53. reset.addActionListener(new ActionListener()
  54. {
  55. public void actionPerformed(ActionEvent e)
  56. {
  57. model.ffwd();
  58. //playback.setValue(playback.getMaximum());
  59. }
  60. });
  61. this.clear = new JButton("Start");
  62. clear.addActionListener(new ActionListener()
  63. {
  64. public void actionPerformed(ActionEvent e)
  65. {
  66. model.rewind();
  67. //playback.setValue(playback.getMinimum());
  68. }
  69. });
  70. this.add(play);
  71. this.add(rewind);
  72. this.add(playback);
  73. this.add(clear);
  74. this.add(reset);
  75. model.addObserver(this);
  76. }
  77. public void update(Object observable)
  78. {
  79. int len = model.getTotalLen();
  80. if (len > 0)
  81. {
  82. playback.toggleSettingLen();
  83. playback.setMaximum(len);
  84. playback.toggleSettingLen();
  85. playback.setValue(model.getTotalVisibleLen());
  86. }
  87. this.setBackground(model.controlColour);
  88. revalidate();
  89. repaint();
  90. }
  91. }
  92. class playSlider extends JSlider
  93. {
  94. private boolean settingLen;
  95. public playSlider(int min, int max, int val)
  96. {
  97. super(min, max, val);
  98. this.settingLen = false;
  99. }
  100. public void toggleSettingLen()
  101. {
  102. this.settingLen = !settingLen;
  103. }
  104. public boolean getSettingLen()
  105. {
  106. return settingLen;
  107. }
  108. }