Nice stuff is Done. Some GUI and Code Structure.

This commit is contained in:
Noah
2021-10-05 22:35:14 +02:00
parent 808083a770
commit 546e5cbfc0
8 changed files with 139 additions and 45 deletions
+15 -2
View File
@@ -7,18 +7,20 @@ public class Draw extends JPanel {
private int xSize, ySize; private int xSize, ySize;
private int width, height; private int width, height;
private float ratio; private float ratio;
private int offset;
private Graphics2D g2d; private Graphics2D g2d;
private boolean[][] currentGrid; private boolean[][] currentGrid;
private boolean drawLocked = false; 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.xSize = gridSizeX;
this.ySize = gridSizeY; this.ySize = gridSizeY;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.ratio = ratio; this.ratio = ratio;
this.offset = offset;
currentGrid = new boolean[gridSizeX][gridSizeY]; currentGrid = new boolean[gridSizeX][gridSizeY];
@@ -34,11 +36,21 @@ public class Draw extends JPanel {
} }
} }
@Override @Override
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
g2d = (Graphics2D) 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 cellSizeX = 800f / xSize;
float cellSizeY = 800f / ySize; float cellSizeY = 800f / ySize;
@@ -57,7 +69,8 @@ public class Draw extends JPanel {
{ {
if (currentGrid[x][y]) 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));
} }
} }
} }
+2 -2
View File
@@ -18,11 +18,11 @@ public class Clock extends Thread{
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, 1000, velocity); t.scheduleAtFixedRate(tt, 0, velocity);
} }
+22 -20
View File
@@ -22,14 +22,14 @@ public class Control {
public void start() { public void start() {
clock = new Clock();
gui = new GUI(this); gui = new GUI(this);
gui.createMenuWindow(); gui.createMenuWindow();
} }
public void startGame() { public void startGame() {
clock = new Clock(); clock.start(this, gui, 200);
clock.start(this, gui, 2000);
} }
public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) { public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) {
@@ -51,7 +51,6 @@ public class Control {
} }
case Manuel -> { case Manuel -> {
buildManuelGame(); buildManuelGame();
} }
} }
@@ -60,12 +59,13 @@ public class Control {
calcSize(); calcSize();
gui.createGameWindow(this.xSize, this.ySize, this.width, this.height); gui.createGameWindow(this.xSize, this.ySize, this.width, this.height);
setRunning(true);
if (startMode == StartMode.Manuel){ if (startMode == StartMode.Manuel){
setRunning(false);
gui.buildControlWindow(); gui.buildControlWindow();
} }
startGame(); 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[1][2] = true;
// cells[2][2] = true; // cells[2][2] = true;
@@ -101,9 +95,9 @@ public class Control {
// cells[3][3] = true; // cells[3][3] = true;
// cells[4][3] = true; // cells[4][3] = true;
// cells[1][0] = true; cells[1][0] = true;
// cells[1][1] = true; cells[1][1] = true;
// cells[1][2] = true; cells[1][2] = true;
syncCells(); syncCells();
@@ -136,6 +130,7 @@ public class Control {
} }
public boolean[][] nextGen() { public boolean[][] nextGen() {
checkCellCount();
gen++; gen++;
System.out.println("Generation:" + gen); System.out.println("Generation:" + gen);
@@ -225,27 +220,26 @@ public class Control {
gui.setVisibility(value); gui.setVisibility(value);
} }
public void setCell(int mouseX, int mouseY, boolean value) { public void setCell(int mouseX, int mouseY, boolean value, int offset) {
int x = 0;
int y = 0;
float ratio = (float) (xSize) / (float) (ySize); float ratio = (float) (xSize) / (float) (ySize);
if (ySize > xSize) { if (ySize > xSize) {
x = (int) ((float) mouseX / (800f / ((float) xSize / ratio))); x = (int) ((float) (mouseX - offset) / (800f / ((float) xSize / ratio)));
y = (int) ((float) mouseY / (800f / ((float) ySize))); y = (int) ((float) (mouseY - offset) / (800f / ((float) ySize)));
} else { } else {
x = (int) ((float) mouseX / (800f / ((float) xSize))); x = (int) ((float) (mouseX - offset) / (800f / ((float) xSize)));
y = (int) ((float) mouseY / (800f / ((float) ySize * ratio))); y = (int) ((float) (mouseY - offset) / (800f / ((float) ySize * ratio)));
} }
if (x >= 0 && x < xSize && y >= 0 && y < ySize && (checkCellCount() || !value)) if (x >= 0 && x < xSize && y >= 0 && y < ySize && (checkCellCount() || !value))
cells[x][y] = value; cells[x][y] = value;
syncCells();
gui.showGrid(cells); gui.showGrid(cells);
} }
@@ -277,6 +271,14 @@ public class Control {
return counter < cellCount; return counter < cellCount;
} }
public void setRunning(boolean state){
clock.setRunning(state);
}
public boolean isRunning(){
return clock.isRunning();
}
} }
+53 -6
View File
@@ -2,30 +2,77 @@ package gui;
import javax.swing.*; import javax.swing.*;
import java.awt.*; 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 { public class ControlWindow {
private JFrame controlWindow;
private final int width = 400, height = 800; 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 = new JFrame();
controlWindow.getContentPane().setPreferredSize(new Dimension(width, height)); controlWindow.getContentPane().setPreferredSize(new Dimension(width, height + (2 * offset)));
controlWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); controlWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
controlWindow.setResizable(false); controlWindow.setResizable(false);
controlWindow.setLocation(gameWindow.getPos().x - (width + offset), gameWindow.getPos().y); controlWindow.setLocation(gameWindow.getPos().x - (width + offset), gameWindow.getPos().y);
controlWindow.setLayout(null); 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.pack();
controlWindow.setVisible(true); 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); controlWindow.setVisible(value);
} }
private void updateToggleButton() {
if (!gui.isRunning()) {
toggleGame.setText("Resume Game");
} else {
toggleGame.setText("Pause Game");
}
}
public void dispose(){
controlWindow.dispose();
}
} }
+16 -7
View File
@@ -3,16 +3,13 @@ package gui;
import game.Control; import game.Control;
import game.StartMode; import game.StartMode;
import javax.swing.*;
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 final int offset = 10; private final int offset = 20;
private MenuWindow menuWindow; private MenuWindow menuWindow;
private GameWindow gameWindow; private GameWindow gameWindow;
@@ -27,11 +24,11 @@ public class GUI {
} }
public void createGameWindow(int cellCountX, int cellCountY, int width, int height){ 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(){ public void createControlWindow(){
controlWindow = new ControlWindow(gameWindow, offset); controlWindow = new ControlWindow(gameWindow, this, offset);
} }
public void showGrid(boolean[][] grid){ public void showGrid(boolean[][] grid){
@@ -47,7 +44,19 @@ public class GUI {
} }
public void setCell(int mouseX, int mouseY, boolean value){ 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;
} }
+23 -5
View File
@@ -19,15 +19,31 @@ public class GameWindow {
private boolean mouseLeft = false, mouseRight = false; 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.cellCountX = cellCountX;
this.cellCountY = cellCountY; this.cellCountY = cellCountY;
gameWindow = new JFrame(title); gameWindow = new JFrame(title);
gameWindow.getContentPane().setPreferredSize(new Dimension(width, height)); gameWindow.getContentPane().setPreferredSize(new Dimension(width + (2 * offset), height + (2 * offset)));
gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 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() { gameWindow.addKeyListener(new KeyListener() {
@Override @Override
@@ -109,12 +125,15 @@ public class GameWindow {
} }
}); });
gameWindow.setResizable(false); gameWindow.setResizable(false);
ratio = (float) (cellCountY) / (float) (cellCountX); 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.setBounds(0, 0, width, height);
draw.setVisible(true); draw.setVisible(true);
gameWindow.add(draw); gameWindow.add(draw);
@@ -138,5 +157,4 @@ public class GameWindow {
} }
} }
+7
View File
@@ -347,6 +347,8 @@ public class MenuWindow {
StartMode startMode = StartMode.values()[index + 1]; // + 1 BECAUSE MANUEL HAS THE FIRST INDEX IN ENUM StartMode startMode = StartMode.values()[index + 1]; // + 1 BECAUSE MANUEL HAS THE FIRST INDEX IN ENUM
gui.buildGameWindow(xAxisSize, yAxisSize, cellCount, startMode); gui.buildGameWindow(xAxisSize, yAxisSize, cellCount, startMode);
menuWindow.setVisible(false);
} }
}); });
} }
@@ -367,6 +369,7 @@ public class MenuWindow {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
gui.buildGameWindow(xAxisSize, yAxisSize, cellCount, StartMode.Manuel); gui.buildGameWindow(xAxisSize, yAxisSize, cellCount, StartMode.Manuel);
menuWindow.setVisible(false);
} }
}); });
} }
@@ -411,4 +414,8 @@ public class MenuWindow {
comboBox.addItem(entry); comboBox.addItem(entry);
} }
public void setVisibility(boolean visibility){
menuWindow.setVisible(visibility);
}
} }
+1 -3
View File
@@ -6,9 +6,7 @@ import game.Control;
public class Main { public class Main {
//TODO: Inputfield witch Changelistner to cange Slider, Generic ActionListener. //TODO: Inputfield witch Changelistner to cange Slider, Generic ActionListener.
//TODO: Offset in each Window. //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: GUI Structure.
//TODO: Window visibility, Control window create only one time. Visibility = false when game started, Visibility = true when game quited.
//TODO: Database //TODO: Database