From 546e5cbfc0cc1f2d8efe0457226f6863536e7eed Mon Sep 17 00:00:00 2001 From: Noah Date: Tue, 5 Oct 2021 22:35:14 +0200 Subject: [PATCH] Nice stuff is Done. Some GUI and Code Structure. --- src/draw/Draw.java | 17 +++++++++-- src/game/Clock.java | 4 +-- src/game/Control.java | 42 ++++++++++++++------------- src/gui/ControlWindow.java | 59 ++++++++++++++++++++++++++++++++++---- src/gui/GUI.java | 23 ++++++++++----- src/gui/GameWindow.java | 28 ++++++++++++++---- src/gui/MenuWindow.java | 7 +++++ src/main/Main.java | 4 +-- 8 files changed, 139 insertions(+), 45 deletions(-) diff --git a/src/draw/Draw.java b/src/draw/Draw.java index f72f47a..87b97bd 100644 --- a/src/draw/Draw.java +++ b/src/draw/Draw.java @@ -7,18 +7,20 @@ public class Draw extends JPanel { private int xSize, ySize; private int width, height; private float ratio; + private int offset; private Graphics2D g2d; private boolean[][] currentGrid; private boolean drawLocked = false; - public Draw(int gridSizeX, int gridSizeY, int width, int height, float ratio) { + public Draw(int gridSizeX, int gridSizeY, int width, int height, float ratio, int offset) { this.xSize = gridSizeX; this.ySize = gridSizeY; this.width = width; this.height = height; this.ratio = ratio; + this.offset = offset; currentGrid = new boolean[gridSizeX][gridSizeY]; @@ -34,11 +36,21 @@ public class Draw extends JPanel { } } + + @Override public void paintComponent(Graphics g) { super.paintComponent(g); g2d = (Graphics2D) g; + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); + + g2d.setColor(Color.white); + g2d.setStroke(new BasicStroke(offset)); + g2d.drawRect(offset / 2, offset / 2, width + offset, height + offset); + + g2d.setColor(Color.DARK_GRAY); + float cellSizeX = 800f / xSize; float cellSizeY = 800f / ySize; @@ -57,7 +69,8 @@ public class Draw extends JPanel { { if (currentGrid[x][y]) { - g2d.fillRect((int) (x * cellSizeX), (int) (y * cellSizeY), (int) ((cellSizeX + 1)), (int) (cellSizeY + 1)); + g2d.fillRect((int) ((x * cellSizeX) + offset), (int) ((y * cellSizeY) + offset), (int) ((cellSizeX + 1)), (int) (cellSizeY + 1)); + } } } diff --git a/src/game/Clock.java b/src/game/Clock.java index 2ecdf83..8e12838 100644 --- a/src/game/Clock.java +++ b/src/game/Clock.java @@ -18,11 +18,11 @@ public class Clock extends Thread{ public void run() { if (running){ control.showGrid(control.getCells()); - //control.nextGen(); + control.nextGen(); } } }; - t.scheduleAtFixedRate(tt, 1000, velocity); + t.scheduleAtFixedRate(tt, 0, velocity); } diff --git a/src/game/Control.java b/src/game/Control.java index 96d386d..7f35f81 100644 --- a/src/game/Control.java +++ b/src/game/Control.java @@ -22,14 +22,14 @@ public class Control { public void start() { + clock = new Clock(); gui = new GUI(this); gui.createMenuWindow(); } public void startGame() { - clock = new Clock(); - clock.start(this, gui, 2000); + clock.start(this, gui, 200); } public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) { @@ -51,7 +51,6 @@ public class Control { } case Manuel -> { buildManuelGame(); - } } @@ -60,12 +59,13 @@ public class Control { calcSize(); gui.createGameWindow(this.xSize, this.ySize, this.width, this.height); + setRunning(true); if (startMode == StartMode.Manuel){ + setRunning(false); gui.buildControlWindow(); } - startGame(); } @@ -87,12 +87,6 @@ public class Control { } } - for (int x = 1; x < 7; x++) { - for (int y = 1; y < 7; y++) { - cells[x][y] = true; - } - } - // cells[1][2] = true; // cells[2][2] = true; @@ -101,9 +95,9 @@ public class Control { // cells[3][3] = true; // cells[4][3] = true; -// cells[1][0] = true; -// cells[1][1] = true; -// cells[1][2] = true; + cells[1][0] = true; + cells[1][1] = true; + cells[1][2] = true; syncCells(); @@ -136,6 +130,7 @@ public class Control { } public boolean[][] nextGen() { + checkCellCount(); gen++; System.out.println("Generation:" + gen); @@ -225,27 +220,26 @@ public class Control { gui.setVisibility(value); } - public void setCell(int mouseX, int mouseY, boolean value) { - int x = 0; - int y = 0; + public void setCell(int mouseX, int mouseY, boolean value, int offset) { float ratio = (float) (xSize) / (float) (ySize); if (ySize > xSize) { - x = (int) ((float) mouseX / (800f / ((float) xSize / ratio))); - y = (int) ((float) mouseY / (800f / ((float) ySize))); + x = (int) ((float) (mouseX - offset) / (800f / ((float) xSize / ratio))); + y = (int) ((float) (mouseY - offset) / (800f / ((float) ySize))); } else { - x = (int) ((float) mouseX / (800f / ((float) xSize))); - y = (int) ((float) mouseY / (800f / ((float) ySize * ratio))); + x = (int) ((float) (mouseX - offset) / (800f / ((float) xSize))); + y = (int) ((float) (mouseY - offset) / (800f / ((float) ySize * ratio))); } if (x >= 0 && x < xSize && y >= 0 && y < ySize && (checkCellCount() || !value)) cells[x][y] = value; + syncCells(); gui.showGrid(cells); } @@ -277,6 +271,14 @@ public class Control { return counter < cellCount; } + + public void setRunning(boolean state){ + clock.setRunning(state); + } + + public boolean isRunning(){ + return clock.isRunning(); + } } diff --git a/src/gui/ControlWindow.java b/src/gui/ControlWindow.java index c56be6d..ff04a27 100644 --- a/src/gui/ControlWindow.java +++ b/src/gui/ControlWindow.java @@ -2,30 +2,77 @@ package gui; import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; public class ControlWindow { - private JFrame controlWindow; - private final int width = 400, height = 800; + private JFrame controlWindow; + private JButton toggleGame; - public ControlWindow(GameWindow gameWindow, int offset){ + private GUI gui; + + public ControlWindow(GameWindow gameWindow, GUI gui, int offset) { + + this.gui = gui; controlWindow = new JFrame(); - controlWindow.getContentPane().setPreferredSize(new Dimension(width, height)); - controlWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + controlWindow.getContentPane().setPreferredSize(new Dimension(width, height + (2 * offset))); + controlWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); controlWindow.setResizable(false); controlWindow.setLocation(gameWindow.getPos().x - (width + offset), gameWindow.getPos().y); controlWindow.setLayout(null); + ImageIcon imageIcon = new ImageIcon("src/files/PNG/Icon.png"); + controlWindow.setIconImage(imageIcon.getImage()); + + controlWindow.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + super.windowClosing(e); + setVisibility(false); + } + }); + 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(); + } + }); + + + controlWindow.add(toggleGame); + } - public void setVisibility(boolean value){ + public void setVisibility(boolean value) { controlWindow.setVisible(value); } + private void updateToggleButton() { + if (!gui.isRunning()) { + toggleGame.setText("Resume Game"); + } else { + toggleGame.setText("Pause Game"); + } + } + + public void dispose(){ + controlWindow.dispose(); + } + } diff --git a/src/gui/GUI.java b/src/gui/GUI.java index 3ab9a05..5a08560 100644 --- a/src/gui/GUI.java +++ b/src/gui/GUI.java @@ -3,16 +3,13 @@ package gui; import game.Control; import game.StartMode; -import javax.swing.*; -import java.awt.*; - public class GUI { private final String title = "Conway's game if Live"; private final Control control; - private final int offset = 10; + private final int offset = 20; private MenuWindow menuWindow; private GameWindow gameWindow; @@ -27,11 +24,11 @@ public class GUI { } public void createGameWindow(int cellCountX, int cellCountY, int width, int height){ - gameWindow = new GameWindow(title, this, cellCountX, cellCountY, width, height); + gameWindow = new GameWindow(title, this, cellCountX, cellCountY, width, height, offset); } public void createControlWindow(){ - controlWindow = new ControlWindow(gameWindow, offset); + controlWindow = new ControlWindow(gameWindow, this, offset); } public void showGrid(boolean[][] grid){ @@ -47,7 +44,19 @@ public class GUI { } public void setCell(int mouseX, int mouseY, boolean value){ - control.setCell(mouseX, mouseY, value); + control.setCell(mouseX, mouseY, value, offset); + } + + public void setRunning(boolean state){ + control.setRunning(state); + } + + public boolean isRunning(){ + return control.isRunning(); + } + + public MenuWindow getMenuWindow(){ + return menuWindow; } diff --git a/src/gui/GameWindow.java b/src/gui/GameWindow.java index fac0c86..e7c128f 100644 --- a/src/gui/GameWindow.java +++ b/src/gui/GameWindow.java @@ -19,15 +19,31 @@ public class GameWindow { private boolean mouseLeft = false, mouseRight = false; - public GameWindow(String title, GUI gui, int cellCountX, int cellCountY, int width, int height) { + public GameWindow(String title, GUI gui, int cellCountX, int cellCountY, int width, int height, int offset) { this.cellCountX = cellCountX; this.cellCountY = cellCountY; gameWindow = new JFrame(title); - gameWindow.getContentPane().setPreferredSize(new Dimension(width, height)); - gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + gameWindow.getContentPane().setPreferredSize(new Dimension(width + (2 * offset), height + (2 * offset))); + gameWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + + ImageIcon imageIcon = new ImageIcon("src/files/PNG/Icon.png"); + gameWindow.setIconImage(imageIcon.getImage()); + + gameWindow.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + super.windowClosing(e); + + if (gui.getControlWindow() != null){ + gui.getControlWindow().dispose(); + } + gameWindow.dispose(); + gui.getMenuWindow().setVisibility(true); + } + }); gameWindow.addKeyListener(new KeyListener() { @Override @@ -109,12 +125,15 @@ public class GameWindow { } }); + gameWindow.setResizable(false); ratio = (float) (cellCountY) / (float) (cellCountX); - draw = new Draw(this.cellCountX, this.cellCountY, width, height, ratio); + draw = new Draw(this.cellCountX, this.cellCountY, width, height, ratio, offset); draw.setBounds(0, 0, width, height); + + draw.setVisible(true); gameWindow.add(draw); @@ -138,5 +157,4 @@ public class GameWindow { } - } diff --git a/src/gui/MenuWindow.java b/src/gui/MenuWindow.java index 7586f85..bcc8a41 100644 --- a/src/gui/MenuWindow.java +++ b/src/gui/MenuWindow.java @@ -347,6 +347,8 @@ public class MenuWindow { StartMode startMode = StartMode.values()[index + 1]; // + 1 BECAUSE MANUEL HAS THE FIRST INDEX IN ENUM gui.buildGameWindow(xAxisSize, yAxisSize, cellCount, startMode); + + menuWindow.setVisible(false); } }); } @@ -367,6 +369,7 @@ public class MenuWindow { @Override public void actionPerformed(ActionEvent e) { gui.buildGameWindow(xAxisSize, yAxisSize, cellCount, StartMode.Manuel); + menuWindow.setVisible(false); } }); } @@ -411,4 +414,8 @@ public class MenuWindow { comboBox.addItem(entry); } + public void setVisibility(boolean visibility){ + menuWindow.setVisible(visibility); + } + } diff --git a/src/main/Main.java b/src/main/Main.java index c91a74d..76d7522 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -6,9 +6,7 @@ import game.Control; public class Main { //TODO: Inputfield witch Changelistner to cange Slider, Generic ActionListener. - //TODO: Offset in each Window. - //TODO: GUI Structure. - //TODO: Window visibility, Control window create only one time. Visibility = false when game started, Visibility = true when game quited. + //TODO: Window visibility, Control window create only one time. Visibility = false when game started, Visibility = true when game quited. :DONE Need a code Check when Brain is awake. //TODO: Database