From d9d0918cc6d8f7f2ad099715029dc1cceb3b53cc Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 8 Oct 2021 22:55:43 +0200 Subject: [PATCH] Control Update --- src/game/Clock.java | 14 +++- src/game/Control.java | 43 +++++------ src/gui/ControlWindow.java | 145 +++++++++++++++++++++++++++++++------ src/gui/GUI.java | 55 ++++++++++++-- src/gui/MenuWindow.java | 22 +----- 5 files changed, 206 insertions(+), 73 deletions(-) diff --git a/src/game/Clock.java b/src/game/Clock.java index 7cd0ebf..f15561e 100644 --- a/src/game/Clock.java +++ b/src/game/Clock.java @@ -11,6 +11,7 @@ public class Clock extends Thread { private GUI gui; private boolean running = true; private int velocity, delay; + private boolean isFirstGen; private Timer t; private TimerTask tt; @@ -19,15 +20,18 @@ public class Clock extends Thread { this.gui = gui; this.velocity = 500; this.delay = 0; + + this.isFirstGen = true; } public void start() { this.delay = this.velocity; - if (t == null) { + if (isFirstGen) { t = new Timer(); this.delay = 0; + this.isFirstGen = false; } if (tt != null){ @@ -44,7 +48,6 @@ public class Clock extends Thread { } }; - System.out.println(this.velocity); t.scheduleAtFixedRate(tt, delay, this.velocity); } @@ -68,5 +71,12 @@ public class Clock extends Thread { start(); } + public boolean isFirstGen() { + return isFirstGen; + } + + public void setFirstGen(boolean firstGen) { + isFirstGen = firstGen; + } } diff --git a/src/game/Control.java b/src/game/Control.java index 125950a..5eda724 100644 --- a/src/game/Control.java +++ b/src/game/Control.java @@ -19,6 +19,7 @@ public class Control { private int width, height; + private boolean mouseLocked; public void start() { @@ -37,7 +38,10 @@ public class Control { this.ySize = ySize; this.cellCount = cellCount; + clock.setFirstGen(true); + gen = 0; + mouseLocked = false; cells = new boolean[this.xSize][this.ySize]; newcells = new boolean[this.xSize][this.ySize]; @@ -132,7 +136,7 @@ public class Control { } public boolean[][] nextGen() { - checkCellCount(); + gen++; System.out.println("Generation:" + gen); @@ -169,27 +173,6 @@ public class Control { } - public boolean[][] nextGen_GridTest() { - for (int x = 0; x < xSize; x++) { - for (int y = 0; y < ySize; y++) { - cells[x][y] = false; - } - } - - - if (x == xSize) { - x = 0; - y++; - } - if (y == ySize) y = 0; - - - cells[x][y] = true; - x++; - - return cells; - } - private int aliveNeigbours(int x, int y) { int count = 0; @@ -238,7 +221,7 @@ public class Control { } - if (x >= 0 && x < xSize && y >= 0 && y < ySize && (checkCellCount() || !value)) + if ((x >= 0 && x < xSize && y >= 0 && y < ySize && (checkCellCount() || !value)) && !mouseLocked) cells[x][y] = value; syncCells(); @@ -285,6 +268,20 @@ public class Control { public void setVelocity(int velocity){ clock.setVelocity(velocity); } + + public boolean isMouseLocked() { + return mouseLocked; + } + + public void setMouseLocked(boolean mouseLocked) { + this.mouseLocked = mouseLocked; + } + + public void multipleGenSkip(int generations){ + for (int i = 0; i < generations; i++){ + nextGen(); + } + } } diff --git a/src/gui/ControlWindow.java b/src/gui/ControlWindow.java index 3871ac6..8c22966 100644 --- a/src/gui/ControlWindow.java +++ b/src/gui/ControlWindow.java @@ -14,11 +14,18 @@ public class ControlWindow { private Font head; private Font text; + private MouseAdapter hover; private JFrame controlWindow; + private JLabel heading; private JButton toggleGame; + private JButton nextGenButton; + private JButton lockMouse; + private JButton clearButton; + private JButton multipleGenSkipButton; + private JTextField multipleGenSkipLable; - private int velocity, minVelocity = 1 , maxVelocity = 10000; + private int velocity, minVelocity = 1 , maxVelocity = 5000; private JSlider velocitySlider; @@ -33,6 +40,7 @@ public class ControlWindow { this.head = gui.getHead(); this.text = gui.getText(); + this.hover = gui.getHover(); velocity = 500; @@ -58,35 +66,33 @@ public class ControlWindow { controlWindow.pack(); controlWindow.setVisible(true); - toggleGame = new JButton(); - updateToggleButton(); - toggleGame.setBounds(50, 50, 100, 50); - toggleGame.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - gui.setRunning(!gui.isRunning()); - updateToggleButton(); - gui.setVelocity(velocity); - } - }); + initComponents(); + addComponents(); - initVelocity(); - - controlWindow.add(velocitySlider); - controlWindow.add(velocitySliderName); - controlWindow.add(velocitySliderLabel); - controlWindow.add(toggleGame); } - /* 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 initComponents(){ + initHeading(); + initVelocity(); + initToggleButton(); + initNextButton(); + initDrawLockButton(); + } + + private void initHeading(){ + heading = new JLabel("Control Panel"); + heading.setFont(head); + heading.setForeground(Color.decode("#121212")); + heading.setBounds(50, 25, 400, 50); + heading.setVisible(true); + } private void initVelocity(){ velocitySlider = new JSlider(minVelocity, maxVelocity); - velocitySlider.setBounds(180, 110, 200, 20); + velocitySlider.setBounds(120, 110, 230, 20); velocitySlider.setFont(text); velocitySlider.setForeground(Color.decode("#121212")); velocitySlider.setValue(6); @@ -103,7 +109,7 @@ public class ControlWindow { velocitySlider.setVisible(true); - velocitySliderName = new JLabel("Breite in Zellen:"); + velocitySliderName = new JLabel("Velocity:"); velocitySliderName.setBounds(50, 100, 200, 30); velocitySliderName.setFont(text); velocitySliderName.setForeground(Color.decode("#121212")); @@ -112,7 +118,7 @@ public class ControlWindow { velocitySliderLabel = new JTextField("6"); velocitySliderLabel.setBackground(null); velocitySliderLabel.setBorder(null); - velocitySliderLabel.setBounds(180, 80, 200, 30); + velocitySliderLabel.setBounds(120, 80, 200, 30); velocitySliderLabel.setFont(text); velocitySliderLabel.setForeground(Color.decode("#121212")); velocitySliderLabel.setVisible(true); @@ -146,8 +152,92 @@ public class ControlWindow { } }); + + } + private void initToggleButton(){ + + toggleGame = new JButton(); + toggleGame.setText("Start Game"); + toggleGame.setFont(text); + toggleGame.setForeground(Color.decode("#121212")); + toggleGame.setBorderPainted(true); + toggleGame.setFocusPainted(false); + toggleGame.setContentAreaFilled(false); + toggleGame.setBounds(50, 750, 300, 50); + toggleGame.setVisible(true); + toggleGame.addMouseListener(hover); + toggleGame.setBorder(new RoundedBorder(25)); + + + toggleGame.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + gui.setRunning(!gui.isRunning()); + updateToggleButton(); + gui.setVelocity(velocity); + } + }); + + updateToggleButton(); + } + + private void initNextButton(){ + nextGenButton = new JButton(); + nextGenButton.setText("Next Generation"); + nextGenButton.setFont(text); + nextGenButton.setForeground(Color.decode("#121212")); + nextGenButton.setBorderPainted(true); + nextGenButton.setFocusPainted(false); + nextGenButton.setContentAreaFilled(false); + nextGenButton.setBounds(50, 675, 300, 50); + nextGenButton.setVisible(true); + nextGenButton.addMouseListener(hover); + nextGenButton.setBorder(new RoundedBorder(25)); + nextGenButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + gui.nextGen(); + gui.showGrid(gui.getCells()); + } + }); + } + + private void initDrawLockButton(){ + lockMouse = new JButton(); + lockMouse.setFont(text); + lockMouse.setForeground(Color.decode("#121212")); + lockMouse.setBorderPainted(true); + lockMouse.setFocusPainted(false); + lockMouse.setContentAreaFilled(false); + lockMouse.setBounds(50, 600, 300, 50); + lockMouse.setVisible(true); + lockMouse.addMouseListener(hover); + lockMouse.setBorder(new RoundedBorder(25)); + lockMouse.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + gui.setMouseLocked(!gui.isMouseLocked()); + updateLockedMouse(); + } + }); + updateLockedMouse(); + } + + private void initSaveButton(){} + + private void initSaveNameField(){} + + private void addComponents(){ + controlWindow.add(heading); + controlWindow.add(velocitySlider); + controlWindow.add(velocitySliderName); + controlWindow.add(velocitySliderLabel); + controlWindow.add(toggleGame); + controlWindow.add(nextGenButton); + controlWindow.add(lockMouse); + } public void setVisibility(boolean value) { controlWindow.setVisible(value); @@ -161,6 +251,15 @@ public class ControlWindow { } } + + private void updateLockedMouse() { + if (!gui.isMouseLocked()) { + lockMouse.setText("Lock Mouse"); + } else { + lockMouse.setText("Unlock Mouse"); + } + } + public void dispose(){ controlWindow.dispose(); } diff --git a/src/gui/GUI.java b/src/gui/GUI.java index c8a62fb..14895ef 100644 --- a/src/gui/GUI.java +++ b/src/gui/GUI.java @@ -3,7 +3,10 @@ package gui; import game.Control; import game.StartMode; +import javax.swing.*; import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; public class GUI { @@ -13,6 +16,7 @@ public class GUI { private Font head; private Font text; + private MouseAdapter hover; private boolean sliderLocked = false; private final int offset = 20; @@ -32,6 +36,25 @@ public class GUI { head = new Font("Segoe UI Light", Font.PLAIN, 24); text = new Font("Segoe UI Light", Font.PLAIN, 16); + hover = new MouseAdapter() { + @Override + public void mouseEntered(MouseEvent e) { + super.mouseEntered(e); + JButton b = (JButton) e.getSource(); + b.setForeground(Color.decode("#C40233")); + b.setBounds(b.getX() - 10, b.getY() - 10, b.getWidth() + 20, b.getHeight() + 20); + } + + @Override + public void mouseExited(MouseEvent e) { + super.mouseExited(e); + JButton b = (JButton) e.getSource(); + b.setForeground(Color.decode("#121212")); + b.setBounds(b.getX() + 10, b.getY() + 10, b.getWidth() - 20, b.getHeight() - 20); + + } + }; + } @@ -88,6 +111,32 @@ public class GUI { return text; } + public boolean isSliderLocked() { + return sliderLocked; + } + + public void setSliderLocked(boolean sliderLocked) { + this.sliderLocked = sliderLocked; + } + + public MouseAdapter getHover() { + return hover; + } + + public void nextGen(){ + control.nextGen(); + } + + public boolean[][] getCells(){ return control.getCells(); } + + public boolean isMouseLocked(){ + return control.isMouseLocked(); + } + + public void setMouseLocked(boolean value){ + control.setMouseLocked(value); + } + //TODO: Delet this Methods. Those are shit. public void setVisibility(boolean value){ controlWindow.setVisibility(value); @@ -97,11 +146,5 @@ 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 004580a..3e401e7 100644 --- a/src/gui/MenuWindow.java +++ b/src/gui/MenuWindow.java @@ -81,27 +81,11 @@ public class MenuWindow { private void initComponents() { - head = gui.getHead(); - text = gui.getText(); + this.head = gui.getHead(); + this.text = gui.getText(); + this.hover = gui.getHover(); - hover = new MouseAdapter() { - @Override - public void mouseEntered(MouseEvent e) { - super.mouseEntered(e); - JButton b = (JButton) e.getSource(); - b.setForeground(Color.decode("#C40233")); - b.setBounds(b.getX() - 10, b.getY() - 10, b.getWidth() + 20, b.getHeight() + 20); - } - @Override - public void mouseExited(MouseEvent e) { - super.mouseExited(e); - JButton b = (JButton) e.getSource(); - b.setForeground(Color.decode("#121212")); - b.setBounds(b.getX() + 10, b.getY() + 10, b.getWidth() - 20, b.getHeight() - 20); - - } - }; initHeading(); initXAxis();