First Velocity Control Implemented

This commit is contained in:
Noah
2021-10-06 18:47:50 +02:00
parent 23c50de11b
commit 6a8070b9ef
5 changed files with 101 additions and 49 deletions
+7 -2
View File
@@ -14,10 +14,10 @@ public class Clock extends Thread {
private Timer t; private Timer t;
private TimerTask tt; private TimerTask tt;
public Clock(Control control, GUI gui, int velocity) { public Clock(Control control, GUI gui) {
this.control = control; this.control = control;
this.gui = gui; this.gui = gui;
this.velocity = velocity; this.velocity = 500;
} }
public void start() { public void start() {
@@ -26,6 +26,10 @@ public class Clock extends Thread {
t = new Timer(); t = new Timer();
} }
if (tt != null){
tt.cancel();
}
tt = new TimerTask() { tt = new TimerTask() {
@Override @Override
public void run() { public void run() {
@@ -36,6 +40,7 @@ public class Clock extends Thread {
} }
}; };
System.out.println(this.velocity);
t.scheduleAtFixedRate(tt, 0, this.velocity); t.scheduleAtFixedRate(tt, 0, this.velocity);
} }
+1 -1
View File
@@ -22,7 +22,7 @@ public class Control {
public void start() { public void start() {
clock = new Clock(this, gui, 1000); clock = new Clock(this, gui);
gui = new GUI(this); gui = new GUI(this);
gui.createMenuWindow(); gui.createMenuWindow();
+40 -26
View File
@@ -11,10 +11,15 @@ import java.awt.event.*;
public class ControlWindow { public class ControlWindow {
private final int width = 400, height = 800; private final int width = 400, height = 800;
private Font head;
private Font text;
private JFrame controlWindow; private JFrame controlWindow;
private JButton toggleGame; private JButton toggleGame;
private int maxVelocity = 1 , minVelocity = 10000; private int velocity, minVelocity = 1 , maxVelocity = 10000;
private JSlider velocitySlider; private JSlider velocitySlider;
private JLabel velocitySliderName; private JLabel velocitySliderName;
@@ -26,6 +31,11 @@ public class ControlWindow {
this.gui = gui; this.gui = gui;
this.head = gui.getHead();
this.text = gui.getText();
velocity = 500;
controlWindow = new JFrame(); controlWindow = new JFrame();
controlWindow.getContentPane().setPreferredSize(new Dimension(width, height + (2 * offset))); controlWindow.getContentPane().setPreferredSize(new Dimension(width, height + (2 * offset)));
@@ -58,11 +68,15 @@ 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); gui.setVelocity(velocity);
} }
}); });
initVelocity();
controlWindow.add(velocitySlider);
controlWindow.add(velocitySliderName);
controlWindow.add(velocitySliderLabel);
controlWindow.add(toggleGame); controlWindow.add(toggleGame);
} }
@@ -70,7 +84,7 @@ public class ControlWindow {
/* Contol Windwo Update. Velocity Change implemented. /* 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 */ FONT and some other Var and method needs to be implemented in the GUI class not in Menu Window */
private void initVelocity(Font text){ private void initVelocity(){
velocitySlider = new JSlider(minVelocity, maxVelocity); velocitySlider = new JSlider(minVelocity, maxVelocity);
velocitySlider.setBounds(180, 110, 200, 20); velocitySlider.setBounds(180, 110, 200, 20);
velocitySlider.setFont(text); velocitySlider.setFont(text);
@@ -79,30 +93,30 @@ public class ControlWindow {
velocitySlider.addChangeListener(new ChangeListener() { velocitySlider.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if(!sliderLocked) { if(!gui.isSliderLocked()) {
xAxisSize = velocitySlider.getValue(); velocity = velocitySlider.getValue();
updateSliderLabels(0, xAxisSize); velocitySliderLabel.setText(String.valueOf(velocity));
updateStartCells(); gui.setVelocity(velocity);
} }
} }
}); });
velocitySlider.setVisible(true); velocitySlider.setVisible(true);
xAxisName = new JLabel("Breite in Zellen:"); velocitySliderName = new JLabel("Breite in Zellen:");
xAxisName.setBounds(50, 100, 200, 30); velocitySliderName.setBounds(50, 100, 200, 30);
xAxisName.setFont(text); velocitySliderName.setFont(text);
xAxisName.setForeground(Color.decode("#121212")); velocitySliderName.setForeground(Color.decode("#121212"));
xAxisName.setVisible(true); velocitySliderName.setVisible(true);
xAxisLabel = new JTextField("6"); velocitySliderLabel = new JTextField("6");
xAxisLabel.setBackground(null); velocitySliderLabel.setBackground(null);
xAxisLabel.setBorder(null); velocitySliderLabel.setBorder(null);
xAxisLabel.setBounds(180, 80, 200, 30); velocitySliderLabel.setBounds(180, 80, 200, 30);
xAxisLabel.setFont(text); velocitySliderLabel.setFont(text);
xAxisLabel.setForeground(Color.decode("#121212")); velocitySliderLabel.setForeground(Color.decode("#121212"));
xAxisLabel.setVisible(true); velocitySliderLabel.setVisible(true);
xAxisLabel.addKeyListener(new KeyAdapter() { velocitySliderLabel.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) { public void keyTyped(KeyEvent evt) {
if (!Character.isDigit(evt.getKeyChar())) { if (!Character.isDigit(evt.getKeyChar())) {
evt.consume(); evt.consume();
@@ -110,16 +124,16 @@ public class ControlWindow {
} }
}); });
xAxisLabel.getDocument().addDocumentListener(new DocumentListener() { velocitySliderLabel.getDocument().addDocumentListener(new DocumentListener() {
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
sliderLocked = true; gui.setSliderLocked(true);
String s = xAxisLabel.getText(); String s = velocitySliderLabel.getText();
velocitySlider.setValue(Integer.parseInt(s)); velocitySlider.setValue(Integer.parseInt(s));
xAxisSize = velocitySlider.getValue(); velocity = velocitySlider.getValue();
updateStartCells(); gui.setVelocity(velocity);
sliderLocked = false; gui.setSliderLocked(false);
} }
@Override @Override
+31 -1
View File
@@ -3,12 +3,18 @@ package gui;
import game.Control; import game.Control;
import game.StartMode; import game.StartMode;
import java.awt.*;
public class GUI { public class GUI {
private final String title = "Conway's game if Live"; private final String title = "Conway's game if Live";
private final Control control; private final Control control;
private Font head;
private Font text;
private boolean sliderLocked = false;
private final int offset = 20; private final int offset = 20;
private MenuWindow menuWindow; private MenuWindow menuWindow;
@@ -17,6 +23,17 @@ public class GUI {
public GUI(Control control){ public GUI(Control control){
this.control = control; this.control = control;
initPublicComponents();
}
public void initPublicComponents(){
head = new Font("Segoe UI Light", Font.PLAIN, 24);
text = new Font("Segoe UI Light", Font.PLAIN, 16);
} }
public void createMenuWindow(){ public void createMenuWindow(){
@@ -63,6 +80,14 @@ public class GUI {
control.setVelocity(velocity); control.setVelocity(velocity);
} }
public Font getHead() {
return head;
}
public Font getText() {
return text;
}
//TODO: Delet this Methods. Those are shit. //TODO: Delet this Methods. Those are shit.
public void setVisibility(boolean value){ public void setVisibility(boolean value){
controlWindow.setVisibility(value); controlWindow.setVisibility(value);
@@ -72,6 +97,11 @@ public class GUI {
return controlWindow; return controlWindow;
} }
public boolean isSliderLocked() {
return sliderLocked;
}
public void setSliderLocked(boolean sliderLocked) {
this.sliderLocked = sliderLocked;
}
} }
+22 -19
View File
@@ -13,6 +13,8 @@ import java.awt.event.*;
public class MenuWindow { public class MenuWindow {
private GUI gui;
private final JFrame menuWindow; private final JFrame menuWindow;
private Font head; private Font head;
@@ -47,9 +49,10 @@ public class MenuWindow {
private ImageIcon preview; private ImageIcon preview;
private JLabel previewLabel; private JLabel previewLabel;
private boolean sliderLocked = false;
public MenuWindow(String title, GUI gui){ public MenuWindow(String title, GUI gui){
this.gui = gui;
menuWindow = new JFrame(); menuWindow = new JFrame();
menuWindow.setTitle(title); menuWindow.setTitle(title);
@@ -68,7 +71,7 @@ public class MenuWindow {
menuWindow.setResizable(false); menuWindow.setResizable(false);
initComponents(gui); initComponents();
addComponents(); addComponents();
@@ -76,10 +79,10 @@ public class MenuWindow {
menuWindow.setVisible(true); menuWindow.setVisible(true);
} }
private void initComponents(GUI gui) { private void initComponents() {
head = new Font("Segoe UI Light", Font.PLAIN, 24); head = gui.getHead();
text = new Font("Segoe UI Light", Font.PLAIN, 16); text = gui.getText();
hover = new MouseAdapter() { hover = new MouseAdapter() {
@Override @Override
@@ -105,8 +108,8 @@ public class MenuWindow {
initYAxis(); initYAxis();
initStartCells(); initStartCells();
initModeBox(); initModeBox();
initStartButton(gui); initStartButton();
initManuelStart(gui); initManuelStart();
initIMG(); initIMG();
} }
@@ -128,7 +131,7 @@ public class MenuWindow {
xAxis.addChangeListener(new ChangeListener() { xAxis.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if(!sliderLocked) { if(!gui.isSliderLocked()) {
xAxisSize = xAxis.getValue(); xAxisSize = xAxis.getValue();
updateSliderLabels(0, xAxisSize); updateSliderLabels(0, xAxisSize);
updateStartCells(); updateStartCells();
@@ -163,12 +166,12 @@ public class MenuWindow {
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
sliderLocked = true; gui.setSliderLocked(true);
String s = xAxisLabel.getText(); String s = xAxisLabel.getText();
xAxis.setValue(Integer.parseInt(s)); xAxis.setValue(Integer.parseInt(s));
xAxisSize = xAxis.getValue(); xAxisSize = xAxis.getValue();
updateStartCells(); updateStartCells();
sliderLocked = false; gui.setSliderLocked(false);
} }
@Override @Override
@@ -192,7 +195,7 @@ public class MenuWindow {
yAxis.addChangeListener(new ChangeListener() { yAxis.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if(!sliderLocked) { if(!gui.isSliderLocked()) {
yAxisSize = yAxis.getValue(); yAxisSize = yAxis.getValue();
updateSliderLabels(1, yAxisSize); updateSliderLabels(1, yAxisSize);
updateStartCells(); updateStartCells();
@@ -226,12 +229,12 @@ public class MenuWindow {
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
sliderLocked = true; gui.setSliderLocked(true);
String s = yAxisLabel.getText(); String s = yAxisLabel.getText();
yAxis.setValue(Integer.parseInt(s)); yAxis.setValue(Integer.parseInt(s));
yAxisSize = yAxis.getValue(); yAxisSize = yAxis.getValue();
updateStartCells(); updateStartCells();
sliderLocked = false; gui.setSliderLocked(false);
} }
@Override @Override
@@ -255,7 +258,7 @@ public class MenuWindow {
startCells.addChangeListener(new ChangeListener() { startCells.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if(!sliderLocked) { if(!gui.isSliderLocked()) {
cellCount = startCells.getValue(); cellCount = startCells.getValue();
updateSliderLabels(2, cellCount); updateSliderLabels(2, cellCount);
updateStartCells(); updateStartCells();
@@ -289,11 +292,11 @@ public class MenuWindow {
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
sliderLocked = true; gui.setSliderLocked(true);
String s = startCellsLabel.getText(); String s = startCellsLabel.getText();
startCells.setValue(Integer.parseInt(s)); startCells.setValue(Integer.parseInt(s));
cellCount = startCells.getValue(); cellCount = startCells.getValue();
sliderLocked = false; gui.setSliderLocked(false);
} }
@Override @Override
@@ -326,7 +329,7 @@ public class MenuWindow {
modeName.setVisible(true); modeName.setVisible(true);
} }
private void initStartButton(GUI gui){ private void initStartButton(){
startButton = new JButton(); startButton = new JButton();
startButton.setText("Start Game"); startButton.setText("Start Game");
startButton.setFont(text); startButton.setFont(text);
@@ -353,7 +356,7 @@ public class MenuWindow {
}); });
} }
private void initManuelStart(GUI gui){ private void initManuelStart(){
manuelStart = new JButton(); manuelStart = new JButton();
manuelStart.setText("Manuel Start"); manuelStart.setText("Manuel Start");
manuelStart.setFont(text); manuelStart.setFont(text);