Nice stuff is Done. Some GUI and Code Structure.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+16
-7
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+23
-5
@@ -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 {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user