Commits

beholdmyglory committed 0274864

Added status bar (need to work on label placement)

Comments (0)

Files changed (6)

src/se/kth/csc/inda11/remains/gui/CellGrid.java

             new Color(255, 64, 0) // TAIL
     };
     private Color    gridColor = Color.darkGray;
+    
+    private Statusbar statusbar;
 
     private Point offset;
     private int   zoomlevel; // pixels between grid lines
     
     private boolean enableGrid = true;
     
-    public CellGrid(Simulator simulator) {
+    public CellGrid(Simulator simulator, Statusbar statusbar) {
         this.setLayout(new BorderLayout());
         
         this.simulator = simulator;
+        this.statusbar = statusbar;
         
         this.zoomlevel = 10;
         this.offset  = new Point(0, 0);
                         CellGrid.this.repaint();
                     }
                 }
+                
+                CellGrid.this.statusbar.setXY(getCoordinates(e.getX(), e.getY()));
             }
             
             @Override
             public void mouseMoved(MouseEvent e) {
-                //System.out.println(getCoordinates(e.getX(), e.getY()));
+                CellGrid.this.statusbar.setXY(getCoordinates(e.getX(), e.getY()));
             }
         };
         

src/se/kth/csc/inda11/remains/gui/MainWindow.java

     CellChooserButton[] cellChooserButtons = new CellChooserButton[Simulator.Cell.values().length];
     JButton colorChooserButton;
     
+    private Statusbar statusbar;
+    
     class CellChooserButton extends JToggleButton {
         private static final long serialVersionUID = 2591164248760916981L;
         
         this.setSize(800, 600);
         this.setLayout(new BorderLayout());
         
+        this.statusbar = new Statusbar();
+        
         this.simulator = simulator;
-        this.cellGridPanel = new CellGrid(simulator);
+        this.cellGridPanel = new CellGrid(this.simulator, this.statusbar);
         
         this.timer = new SimulatorTimer(this.defaultTickLength) {
             private long diffTime = 0;
             @Override
             public void run(long timeSinceLastCall) {
                 MainWindow.this.simulator.tick();
+                MainWindow.this.statusbar.setGeneration(MainWindow.this.simulator.getGeneration());
                 this.diffTime += timeSinceLastCall;
                 if (diffTime > 15) {
                     MainWindow.this.cellGridPanel.repaint();
                                                   "Clear grid", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                     MainWindow.this.simulator.reset();
                     MainWindow.this.cellGridPanel.repaint();
+                    MainWindow.this.statusbar.setGeneration(MainWindow.this.simulator.getGeneration());
                 }
             }
         });
                 if (!MainWindow.this.timer.isRunning()) {
                     MainWindow.this.simulator.tick();
                     MainWindow.this.cellGridPanel.repaint();
+                    MainWindow.this.statusbar.setGeneration(MainWindow.this.simulator.getGeneration());
                 }
             }
         });
             @Override
             public void stateChanged(ChangeEvent e) {
                 int tickLength = MainWindow.this.maxTickLength -
-                                              speedChanger.getValue() + 1;
-                System.out.println("> " + tickLength);
-                System.out.println(tickLength*tickLength / 500);
-                MainWindow.this.timer.setDelay(tickLength*tickLength / MainWindow.this.maxTickLength);
+                                              speedChanger.getValue();
+                tickLength = tickLength*tickLength / MainWindow.this.maxTickLength;
+                MainWindow.this.timer.setDelay(tickLength);
+                MainWindow.this.statusbar.setSpeed(tickLength);
             }
         });
         speedChanger.setToolTipText("Adjust simulation speed");
         this.add(toolbar, BorderLayout.NORTH);
         
         this.add(this.cellGridPanel, BorderLayout.CENTER);
+        
+        this.add(statusbar, BorderLayout.SOUTH);
     }
     
     private Icon getIcon(String group, String name) {

src/se/kth/csc/inda11/remains/gui/Statusbar.java

+package se.kth.csc.inda11.remains.gui;
+
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.Point;
+
+import javax.swing.BoxLayout;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+
+public class Statusbar extends JPanel {
+    private static final long serialVersionUID = -7675597948687295746L;
+    
+    private JLabel generationLabel;
+    private JLabel xyLabel;
+    private JLabel speedLabel;
+    
+    public Statusbar() {
+        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
+        
+        class LabelPanel extends JPanel {
+            private static final long serialVersionUID = -7227185425047327703L;
+
+            public LabelPanel(JLabel label, int width) {
+                this.setMaximumSize(new Dimension(width, 100));
+                this.setMinimumSize(new Dimension(width, 0));
+                this.setLayout(new FlowLayout(FlowLayout.LEFT));
+                this.add(label);
+            }
+        }
+
+        this.add(new LabelPanel(this.generationLabel = new JLabel("Generation: 0"), 250));
+        this.add(new LabelPanel(this.speedLabel = new JLabel("Speed: 5 gen/s"), 150));
+        this.add(this.xyLabel = new JLabel("x: 0 ; y: 0"));
+    }
+    
+    public void setGeneration(long generation) {
+        this.generationLabel.setText(String.format("Generation: %d", generation));
+    }
+    
+    public void setXY(Point xy) {
+        this.xyLabel.setText(String.format("x: %d ; y: %d", xy.x, xy.y));
+    }
+    
+    public void setSpeed(int speed) {
+        if (speed == 0) {
+            this.speedLabel.setText("Speed: MAX");
+        } else {
+            this.speedLabel.setText(String.format("Speed: %d gen/s", 1000 / speed));
+        }
+    }
+    
+}

src/se/kth/csc/inda11/remains/simulator/Simulator.java

     public abstract void addCell(int x, int y, Cell type);
     public abstract void removeCell(int x, int y);
     public abstract void reset();
+    public abstract long getGeneration();
     public abstract Iterator<Map.Entry<java.awt.Point, Simulator.Cell>> cellIterator();
 
     @Override

src/se/kth/csc/inda11/remains/simulator/Simulator1.java

     private Set<Point> conductors;
     private Set<Point> heads;
     private Set<Point> tails;
+    
+    private long generation = 0;
 
     /**
      * @param tickLength The length of a tick in milliseconds
         conductors.addAll(tails);
         tails = heads;
         heads = newHeads;
-
-
-
+        
+        generation++;
     }
 
     /**
         this.conductors = new HashSet<>();
         this.heads = new HashSet<>();
         this.tails = new HashSet<>();
+        this.generation = 0;
+    }
+    
+    @Override
+    public long getGeneration() {
+        return this.generation;
     }
 
     @Override

src/se/kth/csc/inda11/remains/simulator/Simulator2.java

     private HashMap<Point, Simulator.Cell> cells;
     private Set<Point> heads;
     private Set<Point> tails;
+    
+    private long generation;
 
     /**
      * @param tickLength The length of a tick in milliseconds
         // Count
 
 
-
-
-
-
+        this.generation++;
 
     }
 
         this.cells = new HashMap<>();
         this.heads = new HashSet<>();
         this.tails = new HashSet<>();
+        this.generation = 0;
+    }
+    
+    @Override
+    public long getGeneration() {
+        return this.generation;
     }
 
     @Override