Contol Windwo Update. Velocity Change implemented. FONT and some other Var and method needs to be implemented in the GUI class not in Menu Window
This commit is contained in:
+27
-5
@@ -7,13 +7,26 @@ import java.util.TimerTask;
|
||||
|
||||
public class Clock extends Thread {
|
||||
|
||||
private Control control;
|
||||
private GUI gui;
|
||||
private boolean running = true;
|
||||
private int velocity;
|
||||
private Timer t;
|
||||
private TimerTask tt;
|
||||
|
||||
public void start(Control control, GUI gui, int velocity){
|
||||
public Clock(Control control, GUI gui, int velocity) {
|
||||
this.control = control;
|
||||
this.gui = gui;
|
||||
this.velocity = velocity;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
|
||||
Timer t = new Timer();
|
||||
TimerTask tt = new TimerTask() {
|
||||
if (t == null) {
|
||||
t = new Timer();
|
||||
}
|
||||
|
||||
tt = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (running) {
|
||||
@@ -22,12 +35,11 @@ public class Clock extends Thread{
|
||||
}
|
||||
}
|
||||
};
|
||||
t.scheduleAtFixedRate(tt, 0, velocity);
|
||||
|
||||
t.scheduleAtFixedRate(tt, 0, this.velocity);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
@@ -36,5 +48,15 @@ public class Clock extends Thread{
|
||||
this.running = running;
|
||||
}
|
||||
|
||||
|
||||
public int getVelocity() {
|
||||
return velocity;
|
||||
}
|
||||
|
||||
public void setVelocity(int velocity) {
|
||||
this.velocity = velocity;
|
||||
start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ public class Control {
|
||||
private GUI gui;
|
||||
private Clock clock;
|
||||
|
||||
private int xSize, ySize, cellCount, gen = 0;
|
||||
private int xSize, ySize, cellCount, gen;
|
||||
|
||||
private boolean[][] cells;
|
||||
private boolean[][] newcells;
|
||||
@@ -22,14 +22,14 @@ public class Control {
|
||||
|
||||
public void start() {
|
||||
|
||||
clock = new Clock();
|
||||
clock = new Clock(this, gui, 1000);
|
||||
gui = new GUI(this);
|
||||
gui.createMenuWindow();
|
||||
|
||||
}
|
||||
|
||||
public void startGame() {
|
||||
clock.start(this, gui, 200);
|
||||
clock.start();
|
||||
}
|
||||
|
||||
public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) {
|
||||
@@ -37,6 +37,8 @@ public class Control {
|
||||
this.ySize = ySize;
|
||||
this.cellCount = cellCount;
|
||||
|
||||
gen = 0;
|
||||
|
||||
cells = new boolean[this.xSize][this.ySize];
|
||||
newcells = new boolean[this.xSize][this.ySize];
|
||||
|
||||
@@ -279,6 +281,10 @@ public class Control {
|
||||
public boolean isRunning(){
|
||||
return clock.isRunning();
|
||||
}
|
||||
|
||||
public void setVelocity(int velocity){
|
||||
clock.setVelocity(velocity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package gui;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class ControlWindow {
|
||||
|
||||
@@ -13,6 +14,12 @@ public class ControlWindow {
|
||||
private JFrame controlWindow;
|
||||
private JButton toggleGame;
|
||||
|
||||
private int maxVelocity = 1 , minVelocity = 10000;
|
||||
|
||||
private JSlider velocitySlider;
|
||||
private JLabel velocitySliderName;
|
||||
private JTextField velocitySliderLabel;
|
||||
|
||||
private GUI gui;
|
||||
|
||||
public ControlWindow(GameWindow gameWindow, GUI gui, int offset) {
|
||||
@@ -51,6 +58,7 @@ public class ControlWindow {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
gui.setRunning(!gui.isRunning());
|
||||
updateToggleButton();
|
||||
setVelocity(100);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -59,6 +67,71 @@ public class ControlWindow {
|
||||
|
||||
}
|
||||
|
||||
private void initVelocity(Font text){
|
||||
velocitySlider = new JSlider(minVelocity, maxVelocity);
|
||||
velocitySlider.setBounds(180, 110, 200, 20);
|
||||
velocitySlider.setFont(text);
|
||||
velocitySlider.setForeground(Color.decode("#121212"));
|
||||
velocitySlider.setValue(6);
|
||||
velocitySlider.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
if(!sliderLocked) {
|
||||
xAxisSize = velocitySlider.getValue();
|
||||
updateSliderLabels(0, xAxisSize);
|
||||
updateStartCells();
|
||||
}
|
||||
}
|
||||
});
|
||||
velocitySlider.setVisible(true);
|
||||
|
||||
|
||||
xAxisName = new JLabel("Breite in Zellen:");
|
||||
xAxisName.setBounds(50, 100, 200, 30);
|
||||
xAxisName.setFont(text);
|
||||
xAxisName.setForeground(Color.decode("#121212"));
|
||||
xAxisName.setVisible(true);
|
||||
|
||||
xAxisLabel = new JTextField("6");
|
||||
xAxisLabel.setBackground(null);
|
||||
xAxisLabel.setBorder(null);
|
||||
xAxisLabel.setBounds(180, 80, 200, 30);
|
||||
xAxisLabel.setFont(text);
|
||||
xAxisLabel.setForeground(Color.decode("#121212"));
|
||||
xAxisLabel.setVisible(true);
|
||||
xAxisLabel.addKeyListener(new KeyAdapter() {
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
if (!Character.isDigit(evt.getKeyChar())) {
|
||||
evt.consume();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
xAxisLabel.getDocument().addDocumentListener(new DocumentListener() {
|
||||
@Override
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
|
||||
sliderLocked = true;
|
||||
String s = xAxisLabel.getText();
|
||||
velocitySlider.setValue(Integer.parseInt(s));
|
||||
xAxisSize = velocitySlider.getValue();
|
||||
updateStartCells();
|
||||
sliderLocked = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void setVisibility(boolean value) {
|
||||
controlWindow.setVisible(value);
|
||||
}
|
||||
@@ -75,4 +148,8 @@ public class ControlWindow {
|
||||
controlWindow.dispose();
|
||||
}
|
||||
|
||||
|
||||
public void setVelocity(int velocity){
|
||||
gui.setVelocity(velocity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,9 @@ public class GUI {
|
||||
return menuWindow;
|
||||
}
|
||||
|
||||
public void setVelocity(int velocity){
|
||||
control.setVelocity(velocity);
|
||||
}
|
||||
|
||||
//TODO: Delet this Methods. Those are shit.
|
||||
public void setVisibility(boolean value){
|
||||
|
||||
@@ -41,6 +41,7 @@ public class GameWindow {
|
||||
gui.getControlWindow().dispose();
|
||||
}
|
||||
gameWindow.dispose();
|
||||
gui.setRunning(false);
|
||||
gui.getMenuWindow().setVisibility(true);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user