From 6a8070b9effe8ee3856ec6f54e706bdbecdf6f01 Mon Sep 17 00:00:00 2001 From: Noah Date: Wed, 6 Oct 2021 18:47:50 +0200 Subject: [PATCH] First Velocity Control Implemented --- src/game/Clock.java | 9 ++++-- src/game/Control.java | 2 +- src/gui/ControlWindow.java | 66 +++++++++++++++++++++++--------------- src/gui/GUI.java | 32 +++++++++++++++++- src/gui/MenuWindow.java | 41 ++++++++++++----------- 5 files changed, 101 insertions(+), 49 deletions(-) diff --git a/src/game/Clock.java b/src/game/Clock.java index c4cdf14..f4823e1 100644 --- a/src/game/Clock.java +++ b/src/game/Clock.java @@ -14,10 +14,10 @@ public class Clock extends Thread { private Timer t; private TimerTask tt; - public Clock(Control control, GUI gui, int velocity) { + public Clock(Control control, GUI gui) { this.control = control; this.gui = gui; - this.velocity = velocity; + this.velocity = 500; } public void start() { @@ -26,6 +26,10 @@ public class Clock extends Thread { t = new Timer(); } + if (tt != null){ + tt.cancel(); + } + tt = new TimerTask() { @Override public void run() { @@ -36,6 +40,7 @@ public class Clock extends Thread { } }; + System.out.println(this.velocity); t.scheduleAtFixedRate(tt, 0, this.velocity); } diff --git a/src/game/Control.java b/src/game/Control.java index 204692a..125950a 100644 --- a/src/game/Control.java +++ b/src/game/Control.java @@ -22,7 +22,7 @@ public class Control { public void start() { - clock = new Clock(this, gui, 1000); + clock = new Clock(this, gui); gui = new GUI(this); gui.createMenuWindow(); diff --git a/src/gui/ControlWindow.java b/src/gui/ControlWindow.java index 67278f3..3871ac6 100644 --- a/src/gui/ControlWindow.java +++ b/src/gui/ControlWindow.java @@ -11,10 +11,15 @@ import java.awt.event.*; public class ControlWindow { private final int width = 400, height = 800; + + private Font head; + private Font text; + private JFrame controlWindow; private JButton toggleGame; - private int maxVelocity = 1 , minVelocity = 10000; + private int velocity, minVelocity = 1 , maxVelocity = 10000; + private JSlider velocitySlider; private JLabel velocitySliderName; @@ -26,6 +31,11 @@ public class ControlWindow { this.gui = gui; + this.head = gui.getHead(); + this.text = gui.getText(); + + velocity = 500; + controlWindow = new JFrame(); controlWindow.getContentPane().setPreferredSize(new Dimension(width, height + (2 * offset))); @@ -58,11 +68,15 @@ public class ControlWindow { public void actionPerformed(ActionEvent e) { gui.setRunning(!gui.isRunning()); updateToggleButton(); - setVelocity(100); + gui.setVelocity(velocity); } }); + initVelocity(); + controlWindow.add(velocitySlider); + controlWindow.add(velocitySliderName); + controlWindow.add(velocitySliderLabel); controlWindow.add(toggleGame); } @@ -70,7 +84,7 @@ public class ControlWindow { /* 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 */ - private void initVelocity(Font text){ + private void initVelocity(){ velocitySlider = new JSlider(minVelocity, maxVelocity); velocitySlider.setBounds(180, 110, 200, 20); velocitySlider.setFont(text); @@ -79,30 +93,30 @@ public class ControlWindow { velocitySlider.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - if(!sliderLocked) { - xAxisSize = velocitySlider.getValue(); - updateSliderLabels(0, xAxisSize); - updateStartCells(); + if(!gui.isSliderLocked()) { + velocity = velocitySlider.getValue(); + velocitySliderLabel.setText(String.valueOf(velocity)); + gui.setVelocity(velocity); } } }); 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); + velocitySliderName = new JLabel("Breite in Zellen:"); + velocitySliderName.setBounds(50, 100, 200, 30); + velocitySliderName.setFont(text); + velocitySliderName.setForeground(Color.decode("#121212")); + velocitySliderName.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() { + velocitySliderLabel = new JTextField("6"); + velocitySliderLabel.setBackground(null); + velocitySliderLabel.setBorder(null); + velocitySliderLabel.setBounds(180, 80, 200, 30); + velocitySliderLabel.setFont(text); + velocitySliderLabel.setForeground(Color.decode("#121212")); + velocitySliderLabel.setVisible(true); + velocitySliderLabel.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent evt) { if (!Character.isDigit(evt.getKeyChar())) { evt.consume(); @@ -110,16 +124,16 @@ public class ControlWindow { } }); - xAxisLabel.getDocument().addDocumentListener(new DocumentListener() { + velocitySliderLabel.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { - sliderLocked = true; - String s = xAxisLabel.getText(); + gui.setSliderLocked(true); + String s = velocitySliderLabel.getText(); velocitySlider.setValue(Integer.parseInt(s)); - xAxisSize = velocitySlider.getValue(); - updateStartCells(); - sliderLocked = false; + velocity = velocitySlider.getValue(); + gui.setVelocity(velocity); + gui.setSliderLocked(false); } @Override diff --git a/src/gui/GUI.java b/src/gui/GUI.java index 407565c..c8a62fb 100644 --- a/src/gui/GUI.java +++ b/src/gui/GUI.java @@ -3,12 +3,18 @@ package gui; import game.Control; import game.StartMode; +import java.awt.*; + public class GUI { private final String title = "Conway's game if Live"; private final Control control; + private Font head; + private Font text; + private boolean sliderLocked = false; + private final int offset = 20; private MenuWindow menuWindow; @@ -17,6 +23,17 @@ public class GUI { public GUI(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(){ @@ -63,6 +80,14 @@ public class GUI { control.setVelocity(velocity); } + public Font getHead() { + return head; + } + + public Font getText() { + return text; + } + //TODO: Delet this Methods. Those are shit. public void setVisibility(boolean value){ controlWindow.setVisibility(value); @@ -72,6 +97,11 @@ public class GUI { return controlWindow; } + public boolean isSliderLocked() { + return sliderLocked; + } - + public void setSliderLocked(boolean sliderLocked) { + this.sliderLocked = sliderLocked; + } } diff --git a/src/gui/MenuWindow.java b/src/gui/MenuWindow.java index bcc8a41..b7cb2e5 100644 --- a/src/gui/MenuWindow.java +++ b/src/gui/MenuWindow.java @@ -13,6 +13,8 @@ import java.awt.event.*; public class MenuWindow { + private GUI gui; + private final JFrame menuWindow; private Font head; @@ -47,9 +49,10 @@ public class MenuWindow { private ImageIcon preview; private JLabel previewLabel; - private boolean sliderLocked = false; - public MenuWindow(String title, GUI gui){ + + this.gui = gui; + menuWindow = new JFrame(); menuWindow.setTitle(title); @@ -68,7 +71,7 @@ public class MenuWindow { menuWindow.setResizable(false); - initComponents(gui); + initComponents(); addComponents(); @@ -76,10 +79,10 @@ public class MenuWindow { menuWindow.setVisible(true); } - private void initComponents(GUI gui) { + private void initComponents() { - head = new Font("Segoe UI Light", Font.PLAIN, 24); - text = new Font("Segoe UI Light", Font.PLAIN, 16); + head = gui.getHead(); + text = gui.getText(); hover = new MouseAdapter() { @Override @@ -105,8 +108,8 @@ public class MenuWindow { initYAxis(); initStartCells(); initModeBox(); - initStartButton(gui); - initManuelStart(gui); + initStartButton(); + initManuelStart(); initIMG(); } @@ -128,7 +131,7 @@ public class MenuWindow { xAxis.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - if(!sliderLocked) { + if(!gui.isSliderLocked()) { xAxisSize = xAxis.getValue(); updateSliderLabels(0, xAxisSize); updateStartCells(); @@ -163,12 +166,12 @@ public class MenuWindow { @Override public void insertUpdate(DocumentEvent e) { - sliderLocked = true; + gui.setSliderLocked(true); String s = xAxisLabel.getText(); xAxis.setValue(Integer.parseInt(s)); xAxisSize = xAxis.getValue(); updateStartCells(); - sliderLocked = false; + gui.setSliderLocked(false); } @Override @@ -192,7 +195,7 @@ public class MenuWindow { yAxis.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - if(!sliderLocked) { + if(!gui.isSliderLocked()) { yAxisSize = yAxis.getValue(); updateSliderLabels(1, yAxisSize); updateStartCells(); @@ -226,12 +229,12 @@ public class MenuWindow { @Override public void insertUpdate(DocumentEvent e) { - sliderLocked = true; + gui.setSliderLocked(true); String s = yAxisLabel.getText(); yAxis.setValue(Integer.parseInt(s)); yAxisSize = yAxis.getValue(); updateStartCells(); - sliderLocked = false; + gui.setSliderLocked(false); } @Override @@ -255,7 +258,7 @@ public class MenuWindow { startCells.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - if(!sliderLocked) { + if(!gui.isSliderLocked()) { cellCount = startCells.getValue(); updateSliderLabels(2, cellCount); updateStartCells(); @@ -289,11 +292,11 @@ public class MenuWindow { @Override public void insertUpdate(DocumentEvent e) { - sliderLocked = true; + gui.setSliderLocked(true); String s = startCellsLabel.getText(); startCells.setValue(Integer.parseInt(s)); cellCount = startCells.getValue(); - sliderLocked = false; + gui.setSliderLocked(false); } @Override @@ -326,7 +329,7 @@ public class MenuWindow { modeName.setVisible(true); } - private void initStartButton(GUI gui){ + private void initStartButton(){ startButton = new JButton(); startButton.setText("Start Game"); startButton.setFont(text); @@ -353,7 +356,7 @@ public class MenuWindow { }); } - private void initManuelStart(GUI gui){ + private void initManuelStart(){ manuelStart = new JButton(); manuelStart.setText("Manuel Start"); manuelStart.setFont(text);