|
@@ -35,6 +35,8 @@ import javax.swing.plaf.basic.BasicSliderUI;
|
|
|
|
|
|
import ui.controller.Control;
|
|
|
import utility.ImageImport;
|
|
|
+import utility.listener.LostFocusListener;
|
|
|
+
|
|
|
|
|
|
* This Class represents a Panel where the User can start and stop the
|
|
|
* Simulation. He Can also reset the Simulation and click through every
|
|
@@ -42,29 +44,29 @@ import utility.ImageImport;
|
|
|
*
|
|
|
* @author Gruppe14
|
|
|
*/
|
|
|
-public class TimePanel extends JPanel implements ActionListener{
|
|
|
+public class TimePanel extends JPanel implements ActionListener {
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
- private static final int MAX_ITERATIONS=100000;
|
|
|
+ private static final int MAX_ITERATIONS = 100000;
|
|
|
|
|
|
* variable for calculating the performance
|
|
|
*/
|
|
|
-
|
|
|
+
|
|
|
final JButton playBtn = new JButton();
|
|
|
final JButton timeResetBtn = new JButton();
|
|
|
final JButton timeForwardBtn = new JButton();
|
|
|
-
|
|
|
+
|
|
|
final JButton timeBackwardBtn = new JButton();
|
|
|
JTextField iterationsField;
|
|
|
- final JLabel iterationsLabel=new JLabel("Iterations:" , SwingConstants.CENTER);
|
|
|
+ final JLabel iterationsLabel = new JLabel("Iterations:", SwingConstants.CENTER);
|
|
|
JLabel hint = new JLabel("Invalid", SwingConstants.RIGHT);
|
|
|
private final JPanel btnAndSpeedPanel = new JPanel();
|
|
|
private final JPanel speedPanel = new JPanel();
|
|
|
private final JPanel timeBtnPanel = new JPanel();
|
|
|
- private final JPanel iterationsPanel=new JPanel();
|
|
|
- private final JPanel timePanel=new JPanel();
|
|
|
+ private final JPanel iterationsPanel = new JPanel();
|
|
|
+ private final JPanel timePanel = new JPanel();
|
|
|
private ScheduledFuture<?> futureTask;
|
|
|
-
|
|
|
+
|
|
|
JSlider timeSlider = new JSlider() {
|
|
|
|
|
|
*
|
|
@@ -96,7 +98,7 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
addMouseListener(tl);
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
private Control controller;
|
|
|
private int dragResetIteration = 0;
|
|
|
private JLabel simSpeedLabel = new JLabel("Speed:");
|
|
@@ -107,10 +109,8 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
|
|
|
* Constructor.
|
|
|
*
|
|
|
- * @param mod
|
|
|
- * the Model
|
|
|
- * @param cont
|
|
|
- * the Controller
|
|
|
+ * @param mod the Model
|
|
|
+ * @param cont the Controller
|
|
|
*/
|
|
|
public TimePanel(Control cont) {
|
|
|
super();
|
|
@@ -136,8 +136,8 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
|
|
|
timeSlider.setPaintTicks(true);
|
|
|
timeSlider.setPaintLabels(true);
|
|
|
- timeSlider.setMajorTickSpacing((int)Math.ceil(((double)cont.getModel().getMaxIterations())/20));
|
|
|
- timeSlider.setMinorTickSpacing((int)Math.ceil(((double)cont.getModel().getMaxIterations())/100));
|
|
|
+ timeSlider.setMajorTickSpacing((int) Math.ceil(((double) cont.getModel().getMaxIterations()) / 20));
|
|
|
+ timeSlider.setMinorTickSpacing((int) Math.ceil(((double) cont.getModel().getMaxIterations()) / 100));
|
|
|
timeSlider.setToolTipText("Time Slider");
|
|
|
timeSlider.setMaximum(cont.getModel().getMaxIterations() - 1);
|
|
|
timeSlider.setValue(0);
|
|
@@ -171,25 +171,22 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
playBtn.setContentAreaFilled(false);
|
|
|
playBtn.setBorderPainted(false);
|
|
|
playBtn.setBorder(null);
|
|
|
- playBtn.setIcon(new ImageIcon(ImageImport.loadImage("/Button_Images/play.png",30,30)));
|
|
|
+ playBtn.setIcon(new ImageIcon(ImageImport.loadImage("/Button_Images/play.png", 30, 30)));
|
|
|
playBtn.addActionListener(new ActionListener() {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
- if(cont.getModel().getCurrentIteration()==cont.getModel().getMaxIterations()-1)timeSlider.setValue(timeSlider.getMinimum());
|
|
|
+ if (cont.getModel().getCurrentIteration() == cont.getModel().getMaxIterations() - 1)
|
|
|
+ timeSlider.setValue(timeSlider.getMinimum());
|
|
|
running = !running;
|
|
|
if (running) {
|
|
|
timer.setDelay(cont.getModel().getTimerSpeed());
|
|
|
|
|
|
|
|
|
- * start the Performance Test
|
|
|
- * /
|
|
|
- performanceTime = System.nanoTime();
|
|
|
- timeSlider.setValue(0);
|
|
|
- timer.setDelay(0);
|
|
|
- speedSlider.setValue(10);
|
|
|
- timeSlider.setValue(0);
|
|
|
-
|
|
|
-
|
|
|
+ * start the Performance Test / performanceTime = System.nanoTime();
|
|
|
+ * timeSlider.setValue(0); timer.setDelay(0); speedSlider.setValue(10);
|
|
|
+ * timeSlider.setValue(0);
|
|
|
+ */
|
|
|
+
|
|
|
timer.start();
|
|
|
playBtn.setIcon(new ImageIcon(ImageImport.loadImage("/Button_Images/pause.png", 30, 30)));
|
|
|
} else {
|
|
@@ -218,7 +215,7 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
|
|
|
timeForwardBtn.setContentAreaFilled(false);
|
|
|
timeForwardBtn.setBorder(null);
|
|
|
- timeForwardBtn.setIcon(new ImageIcon(ImageImport.loadImage("/Button_Images/forward.png",30,30)));
|
|
|
+ timeForwardBtn.setIcon(new ImageIcon(ImageImport.loadImage("/Button_Images/forward.png", 30, 30)));
|
|
|
timeForwardBtn.addActionListener(new ActionListener() {
|
|
|
public void actionPerformed(ActionEvent ae) {
|
|
|
timeSlider.setValue(timeSlider.getValue() + 1);
|
|
@@ -228,7 +225,7 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
timeBackwardBtn.setToolTipText("Backward");
|
|
|
|
|
|
timeBackwardBtn.setBorder(null);
|
|
|
- timeBackwardBtn.setIcon(new ImageIcon(ImageImport.loadImage("/Button_Images/backward.png", 30,30)));
|
|
|
+ timeBackwardBtn.setIcon(new ImageIcon(ImageImport.loadImage("/Button_Images/backward.png", 30, 30)));
|
|
|
timeBackwardBtn.addActionListener(new ActionListener() {
|
|
|
public void actionPerformed(ActionEvent ae) {
|
|
|
timeSlider.setValue(timeSlider.getValue() - 1);
|
|
@@ -250,34 +247,32 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
speedSlider.setMaximum(6);
|
|
|
speedSlider.setMinimum(0);
|
|
|
speedSlider.setValue(1);
|
|
|
-
|
|
|
+
|
|
|
speedSlider.setPaintLabels(true);
|
|
|
Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
|
|
|
- table.put (0, new JLabel("1x"));
|
|
|
- table.put (1, new JLabel("2x"));
|
|
|
- table.put (2, new JLabel("4x"));
|
|
|
- table.put (3, new JLabel("8x"));
|
|
|
- table.put (4, new JLabel("16x"));
|
|
|
- table.put (5, new JLabel("32x"));
|
|
|
- table.put (6, new JLabel("64x"));
|
|
|
-
|
|
|
+ table.put(0, new JLabel("1x"));
|
|
|
+ table.put(1, new JLabel("2x"));
|
|
|
+ table.put(2, new JLabel("4x"));
|
|
|
+ table.put(3, new JLabel("8x"));
|
|
|
+ table.put(4, new JLabel("16x"));
|
|
|
+ table.put(5, new JLabel("32x"));
|
|
|
+ table.put(6, new JLabel("64x"));
|
|
|
+
|
|
|
speedSlider.setLabelTable(table);
|
|
|
-
|
|
|
+
|
|
|
speedSlider.addChangeListener(new ChangeListener() {
|
|
|
@Override
|
|
|
public void stateChanged(ChangeEvent e) {
|
|
|
|
|
|
- * Shifting Powers of two:
|
|
|
- * e.g. 1<<0 -> 1 step per Second
|
|
|
- * 1<<3 -> 8 steps per Second
|
|
|
- * and so on,
|
|
|
+ * Shifting Powers of two: e.g. 1<<0 -> 1 step per Second 1<<3 -> 8 steps per
|
|
|
+ * Second and so on,
|
|
|
*/
|
|
|
int calculationsPerSecond = 1 << speedSlider.getValue();
|
|
|
controller.setTimerSpeed(1024 >> speedSlider.getValue());
|
|
|
speedSlider.setToolTipText("Speed: " + calculationsPerSecond + " Calculations per Second.");
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
speedSlider.setToolTipText("Change the Number of Calculations per Secons");
|
|
|
|
|
|
|
|
@@ -292,52 +287,48 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
c.gridx = 0;
|
|
|
c.gridy = 0;
|
|
|
iterationsPanel.add(iterationsLabel, c);
|
|
|
-
|
|
|
+
|
|
|
hint.setForeground(Color.red);
|
|
|
hint.setText(" ");
|
|
|
- iterationsField=new JTextField(6);
|
|
|
- iterationsField.setText(""+cont.getModel().getMaxIterations());
|
|
|
- iterationsField.setToolTipText("0-" + MAX_ITERATIONS );
|
|
|
+ iterationsField = new JTextField(6);
|
|
|
+ iterationsField.setText("" + cont.getModel().getMaxIterations());
|
|
|
+ iterationsField.setToolTipText("0-" + MAX_ITERATIONS);
|
|
|
iterationsField.addActionListener(this);
|
|
|
ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(1);
|
|
|
- iterationsField.addCaretListener((e)->
|
|
|
- {
|
|
|
- try{
|
|
|
- iterationsField.setBackground(Color.WHITE);
|
|
|
- if(futureTask != null){
|
|
|
- futureTask.cancel(true);
|
|
|
- }
|
|
|
- futureTask = s.schedule((Runnable)() -> updateIterationsInput(), 1, TimeUnit.SECONDS);
|
|
|
- hint.setText(" ");
|
|
|
- }catch(NumberFormatException n){
|
|
|
- iterationsField.setBackground(GUI.PALE_RED);
|
|
|
- hint.setText("Invalid");
|
|
|
+ iterationsField.addCaretListener((e) -> {
|
|
|
+ try {
|
|
|
+ iterationsField.setBackground(Color.WHITE);
|
|
|
+ if (futureTask != null) {
|
|
|
+ futureTask.cancel(true);
|
|
|
}
|
|
|
+ futureTask = s.schedule((Runnable) () -> updateIterationsInput(), 1, TimeUnit.SECONDS);
|
|
|
+ hint.setText(" ");
|
|
|
+ } catch (NumberFormatException n) {
|
|
|
+ iterationsField.setBackground(GUI.PALE_RED);
|
|
|
+ hint.setText("Invalid");
|
|
|
}
|
|
|
- );
|
|
|
- iterationsField.addFocusListener((interfaces.FocusListener)(e) ->{
|
|
|
-
|
|
|
- updateIterationsInput();
|
|
|
});
|
|
|
+ iterationsField.addFocusListener((LostFocusListener) (e) -> updateIterationsInput());
|
|
|
c.gridy = 1;
|
|
|
iterationsPanel.add(iterationsField, c);
|
|
|
c.gridy = 2;
|
|
|
iterationsPanel.add(hint, c);
|
|
|
-
|
|
|
- timePanel.setLayout(new BorderLayout());;
|
|
|
+
|
|
|
+ timePanel.setLayout(new BorderLayout());
|
|
|
+ ;
|
|
|
timePanel.add(iterationsPanel, BorderLayout.WEST);
|
|
|
timePanel.add(timeSlider, BorderLayout.CENTER);
|
|
|
this.add(btnAndSpeedPanel, BorderLayout.WEST);
|
|
|
add(timePanel);
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "UP_ARROW");
|
|
|
timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "DOWN_ARROW");
|
|
|
timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0, false), "PAGE_DOWN");
|
|
|
timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0, false), "PAGE_UP");
|
|
|
timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0, false), "END");
|
|
|
timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0, false), "HOME");
|
|
|
-
|
|
|
+
|
|
|
|
|
|
timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "LEFT_ARROW");
|
|
|
timeSlider.getActionMap().put("LEFT_ARROW", new AbstractAction() {
|
|
@@ -349,8 +340,8 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
controller.resetSimulation();
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "Right_ARROW");
|
|
|
timeSlider.getActionMap().put("Right_ARROW", new AbstractAction() {
|
|
|
private static final long serialVersionUID = 1L;
|
|
@@ -375,25 +366,34 @@ public class TimePanel extends JPanel implements ActionListener{
|
|
|
public void actionPerformed(ActionEvent arg0) {
|
|
|
updateIterationsInput();
|
|
|
}
|
|
|
+
|
|
|
|
|
|
- * Update the Text field and apply valid changes to update Iterations from to model.
|
|
|
- * <p>
|
|
|
- * Executed by user input.
|
|
|
+ * Update the Text field and apply valid changes to update Iterations from to
|
|
|
+ * model.
|
|
|
+ * <p>
|
|
|
+ * Executed by user input.
|
|
|
*/
|
|
|
private void updateIterationsInput() {
|
|
|
- try{
|
|
|
- int iterations=Integer.parseInt(iterationsField.getText());
|
|
|
-
|
|
|
- boolean resetField=true;
|
|
|
- if(iterations<1)iterations=1;
|
|
|
- else if(iterations>MAX_ITERATIONS)iterations=MAX_ITERATIONS;
|
|
|
- else resetField=false;
|
|
|
- if(resetField)iterationsField.setText(""+iterations);
|
|
|
+ try {
|
|
|
+ int iterations = Integer.parseInt(iterationsField.getText());
|
|
|
+
|
|
|
+ boolean resetField = true;
|
|
|
+ if (iterations < 1)
|
|
|
+ iterations = 1;
|
|
|
+ else if (iterations > MAX_ITERATIONS)
|
|
|
+ iterations = MAX_ITERATIONS;
|
|
|
+ else
|
|
|
+ resetField = false;
|
|
|
+ if (resetField)
|
|
|
+ iterationsField.setText("" + iterations);
|
|
|
controller.getModel().setIterations(Integer.parseInt(iterationsField.getText()));
|
|
|
- timeSlider.setMaximum(controller.getModel().getMaxIterations()-1);
|
|
|
- timeSlider.setLabelTable(null);
|
|
|
- timeSlider.setMajorTickSpacing((int)Math.ceil(((double)iterations)/20));
|
|
|
- timeSlider.setMinorTickSpacing((int)Math.ceil(((double)iterations)/100));
|
|
|
- }catch(NumberFormatException e){}
|
|
|
+ timeSlider.setMaximum(controller.getModel().getMaxIterations() - 1);
|
|
|
+ timeSlider.setLabelTable(null);
|
|
|
+ timeSlider.setMajorTickSpacing((int) Math.ceil(((double) iterations) / 20));
|
|
|
+ timeSlider.setMinorTickSpacing((int) Math.ceil(((double) iterations) / 100));
|
|
|
+
|
|
|
+
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ }
|
|
|
}
|
|
|
}
|