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:
Noah
2021-10-06 10:32:16 +02:00
parent 546e5cbfc0
commit 96404f1d26
5 changed files with 123 additions and 14 deletions
+29 -7
View File
@@ -5,29 +5,41 @@ import gui.GUI;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
public class Clock extends Thread{ public class Clock extends Thread {
private Control control;
private GUI gui;
private boolean running = true; 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(); if (t == null) {
TimerTask tt = new TimerTask() { t = new Timer();
}
tt = new TimerTask() {
@Override @Override
public void run() { public void run() {
if (running){ if (running) {
control.showGrid(control.getCells()); control.showGrid(control.getCells());
control.nextGen(); control.nextGen();
} }
} }
}; };
t.scheduleAtFixedRate(tt, 0, velocity);
t.scheduleAtFixedRate(tt, 0, this.velocity);
} }
public boolean isRunning() { public boolean isRunning() {
return running; return running;
} }
@@ -36,5 +48,15 @@ public class Clock extends Thread{
this.running = running; this.running = running;
} }
public int getVelocity() {
return velocity;
}
public void setVelocity(int velocity) {
this.velocity = velocity;
start();
}
} }
+9 -3
View File
@@ -9,7 +9,7 @@ public class Control {
private GUI gui; private GUI gui;
private Clock clock; private Clock clock;
private int xSize, ySize, cellCount, gen = 0; private int xSize, ySize, cellCount, gen;
private boolean[][] cells; private boolean[][] cells;
private boolean[][] newcells; private boolean[][] newcells;
@@ -22,14 +22,14 @@ public class Control {
public void start() { public void start() {
clock = new Clock(); clock = new Clock(this, gui, 1000);
gui = new GUI(this); gui = new GUI(this);
gui.createMenuWindow(); gui.createMenuWindow();
} }
public void startGame() { public void startGame() {
clock.start(this, gui, 200); clock.start();
} }
public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) { public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) {
@@ -37,6 +37,8 @@ public class Control {
this.ySize = ySize; this.ySize = ySize;
this.cellCount = cellCount; this.cellCount = cellCount;
gen = 0;
cells = new boolean[this.xSize][this.ySize]; cells = new boolean[this.xSize][this.ySize];
newcells = new boolean[this.xSize][this.ySize]; newcells = new boolean[this.xSize][this.ySize];
@@ -279,6 +281,10 @@ public class Control {
public boolean isRunning(){ public boolean isRunning(){
return clock.isRunning(); return clock.isRunning();
} }
public void setVelocity(int velocity){
clock.setVelocity(velocity);
}
} }
+81 -4
View File
@@ -1,11 +1,12 @@
package gui; package gui;
import javax.swing.*; 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.*;
import java.awt.event.ActionEvent; import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ControlWindow { public class ControlWindow {
@@ -13,6 +14,12 @@ public class ControlWindow {
private JFrame controlWindow; private JFrame controlWindow;
private JButton toggleGame; private JButton toggleGame;
private int maxVelocity = 1 , minVelocity = 10000;
private JSlider velocitySlider;
private JLabel velocitySliderName;
private JTextField velocitySliderLabel;
private GUI gui; private GUI gui;
public ControlWindow(GameWindow gameWindow, GUI gui, int offset) { public ControlWindow(GameWindow gameWindow, GUI gui, int offset) {
@@ -51,6 +58,7 @@ public class ControlWindow {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
gui.setRunning(!gui.isRunning()); gui.setRunning(!gui.isRunning());
updateToggleButton(); 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) { public void setVisibility(boolean value) {
controlWindow.setVisible(value); controlWindow.setVisible(value);
} }
@@ -75,4 +148,8 @@ public class ControlWindow {
controlWindow.dispose(); controlWindow.dispose();
} }
public void setVelocity(int velocity){
gui.setVelocity(velocity);
}
} }
+3
View File
@@ -59,6 +59,9 @@ public class GUI {
return menuWindow; return menuWindow;
} }
public void setVelocity(int velocity){
control.setVelocity(velocity);
}
//TODO: Delet this Methods. Those are shit. //TODO: Delet this Methods. Those are shit.
public void setVisibility(boolean value){ public void setVisibility(boolean value){
+1
View File
@@ -41,6 +41,7 @@ public class GameWindow {
gui.getControlWindow().dispose(); gui.getControlWindow().dispose();
} }
gameWindow.dispose(); gameWindow.dispose();
gui.setRunning(false);
gui.getMenuWindow().setVisibility(true); gui.getMenuWindow().setVisibility(true);
} }
}); });