GUI Class Abstract.

This commit is contained in:
Noah
2021-12-14 18:44:26 +01:00
parent bed59d4ecc
commit 546b538c99
13 changed files with 26646 additions and 323 deletions
Binary file not shown.
File diff suppressed because it is too large Load Diff
-3
View File
@@ -1,7 +1,5 @@
package gamecontrol; package gamecontrol;
import gui.control.GUI;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@@ -59,7 +57,6 @@ public class Clock extends Thread {
this.running = running; this.running = running;
} }
public int getVelocity() { public int getVelocity() {
return velocity; return velocity;
} }
+70 -42
View File
@@ -2,14 +2,19 @@ package gamecontrol;
import database.ReadDAO; import database.ReadDAO;
import database.WriteDAO; import database.WriteDAO;
import gui.control.GUI; import gui.windows.ControlWindow;
import gui.windows.GameWindow;
import gui.windows.MenuWindow;
import java.awt.*;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class Control { public class Control {
private GUI gui; private MenuWindow menuWindow;
private GameWindow gameWindow;
private ControlWindow controlWindow;
private Clock clock; private Clock clock;
private ReadDAO readDAO; private ReadDAO readDAO;
private WriteDAO writeDAO; private WriteDAO writeDAO;
@@ -26,29 +31,30 @@ public class Control {
private int width, height; private int width, height;
private boolean mouseLocked; private boolean mouseLocked;
private boolean manual;
public void start() { public void start() {
clock = new Clock(this); clock = new Clock(this);
gui = new GUI(this); this.readDAO = new ReadDAO();
readDAO = new ReadDAO(); this.writeDAO = new WriteDAO();
writeDAO = new WriteDAO(); menuWindow = new MenuWindow(this);
gui.createMenuWindow();
} }
public void startGame() {
clock.start();
}
public void buildGameWindow(int xSize, int ySize, int cellCount, String startMode) { public void buildGameWindow(int xSize, int ySize, int cellCount, String startMode) {
manual = false;
switch (startMode) { switch (startMode) {
case "Randomized" -> { case "Randomized" -> {
generateRandomized(xSize, ySize, cellCount); generateRandomized(xSize, ySize, cellCount);
} }
case "Manuel" -> { case "Manuel" -> {
setManual(true);
buildManuelGame(xSize, ySize, cellCount); buildManuelGame(xSize, ySize, cellCount);
} }
default -> { default -> {
@@ -60,13 +66,10 @@ public class Control {
calcSize(); calcSize();
gui.createGameWindow(this.xSize, this.ySize, this.width, this.height); gameWindow = new GameWindow(this, this.manual, this.xSize, this.ySize, this.width, this.height);
if (startMode == "Manuel"){
gui.buildControlWindow();
}
startGame(); clock.start();
} }
@@ -99,7 +102,7 @@ public class Control {
} }
private void getFromDatabase(String name){ private void getFromDatabase(String name) {
cells = readDAO.getGrid(name); cells = readDAO.getGrid(name);
gen = Integer.parseInt(readDAO.getGameInfo(name, "GENERATION")); gen = Integer.parseInt(readDAO.getGameInfo(name, "GENERATION"));
@@ -147,7 +150,7 @@ public class Control {
} }
} }
public void saveFirstGrid(){ public void saveFirstGrid() {
for (int x = 0; x < this.xSize; x++) { for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) { for (int y = 0; y < this.ySize; y++) {
startingCells[x][y] = cells[x][y]; startingCells[x][y] = cells[x][y];
@@ -155,7 +158,7 @@ public class Control {
} }
} }
private void resetCells(){ private void resetCells() {
for (int x = 0; x < this.xSize; x++) { for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) { for (int y = 0; y < this.ySize; y++) {
cells[x][y] = startingCells[x][y]; cells[x][y] = startingCells[x][y];
@@ -194,7 +197,7 @@ public class Control {
cells[x][y] = newcells[x][y]; cells[x][y] = newcells[x][y];
} }
} }
gui.updateGameWindowTitle(gen); gameWindow.updateTitle(gen);
} }
public void nextGenLight() { public void nextGenLight() {
@@ -224,15 +227,15 @@ public class Control {
} }
} }
public void previousGen(){ public void previousGen() {
if(gen > 0) { if (gen > 0) {
resetCells(); resetCells();
syncCells(); syncCells();
for (int i = 0; i < gen - 1; i++) { for (int i = 0; i < gen - 1; i++) {
nextGenLight(); nextGenLight();
} }
gen--; gen--;
gui.updateGameWindowTitle(gen); gameWindow.updateTitle(gen);
} }
} }
@@ -257,13 +260,6 @@ public class Control {
return ThreadLocalRandom.current().nextInt(min, max); return ThreadLocalRandom.current().nextInt(min, max);
} }
public void showGrid(boolean[][] grid) {
gui.showGrid(grid);
}
public void setVisibility(boolean value) {
gui.setVisibility(value);
}
public void setCell(int mouseX, int mouseY, boolean value, int offset) { public void setCell(int mouseX, int mouseY, boolean value, int offset) {
@@ -285,7 +281,7 @@ public class Control {
cells[x][y] = value; cells[x][y] = value;
syncCells(); syncCells();
gui.showGrid(cells); gameWindow.showGrid(cells);
} }
private void calcSize() { private void calcSize() {
@@ -325,14 +321,14 @@ public class Control {
clock.setRunning(state); clock.setRunning(state);
} }
public void setVelocity(int velocity) {
clock.setVelocity(velocity);
}
public int getVelocity() { public int getVelocity() {
return clock.getVelocity(); return clock.getVelocity();
} }
public void setVelocity(int velocity) {
clock.setVelocity(velocity);
}
public boolean isMouseLocked() { public boolean isMouseLocked() {
return mouseLocked; return mouseLocked;
} }
@@ -359,43 +355,75 @@ public class Control {
} }
} }
public int getGeneration(){ public int getGeneration() {
return gen; return gen;
} }
public void jump2Gen(int wantedtGen){ public void jump2Gen(int wantedtGen) {
if (wantedtGen <= gen){ if (wantedtGen <= gen) {
resetCells(); resetCells();
syncCells(); syncCells();
int counter = ((gen - wantedtGen) / (wantedtGen + 1) ); int counter = ((gen - wantedtGen) / (wantedtGen + 1));
for (int i = 0; i < wantedtGen; i++) { for (int i = 0; i < wantedtGen; i++) {
nextGenLight(); nextGenLight();
gen = gen - counter; gen = gen - counter;
gui.updateGameWindowTitle(gen); gameWindow.updateTitle(gen);
} }
gen = wantedtGen; gen = wantedtGen;
gui.updateGameWindowTitle(gen); gameWindow.updateTitle(gen);
} else { } else {
multipleGenSkip(wantedtGen - gen); multipleGenSkip(wantedtGen - gen);
} }
} }
public List<String> getAllGrids(){ public List<String> getAllGrids() {
return readDAO.getAllGridNames(); return readDAO.getAllGridNames();
} }
public void saveGridInDB(String gridName){ public void saveGridInDB(String gridName) {
writeDAO.saveGrid(this.cells, gridName, this.gen, this.cellCount); writeDAO.saveGrid(this.cells, gridName, this.gen, this.cellCount);
} }
public boolean checkName(String name){ public boolean checkName(String name) {
return readDAO.checkName(name); return readDAO.checkName(name);
} }
public boolean isManual() {
return manual;
}
public void setManual(boolean manual) {
this.manual = manual;
}
public void buildControlWindow() {
controlWindow = new ControlWindow(this);
}
public Point getGameWindowPos() {
return gameWindow.getPos();
}
public void showGrid(boolean[][] grid) {
gameWindow.showGrid(grid);
}
public ControlWindow getControlWindow() {
return controlWindow;
}
public MenuWindow getMenuWindow() {
return menuWindow;
}
public void setVisibility(boolean value) {
controlWindow.setVisibility(value);
}
} }
-167
View File
@@ -1,167 +0,0 @@
package gui.control;
import gamecontrol.Control;
import gui.windows.ControlWindow;
import gui.windows.GameWindow;
import gui.windows.MenuWindow;
import java.awt.*;
import java.util.List;
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;
private GameWindow gameWindow;
private ControlWindow controlWindow;
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(){
menuWindow = new MenuWindow(title, this);
}
public void createGameWindow(int cellCountX, int cellCountY, int width, int height){
gameWindow = new GameWindow(title, this, cellCountX, cellCountY, width, height, offset);
}
public void createControlWindow(){
controlWindow = new ControlWindow(gameWindow, this, offset);
}
public void showGrid(boolean[][] grid){
gameWindow.showGrid(grid);
}
public void buildGameWindow(int xSize, int ySize, int cellCount, String startMode){
control.buildGameWindow(xSize, ySize, cellCount, startMode);
}
public void buildControlWindow(){
createControlWindow();
}
public void setCell(int mouseX, int mouseY, boolean 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;
}
public void setVelocity(int velocity){
control.setVelocity(velocity);
}
public Font getHead() {
return head;
}
public Font getText() {
return text;
}
public boolean isSliderLocked() {
return sliderLocked;
}
public void setSliderLocked(boolean sliderLocked) {
this.sliderLocked = sliderLocked;
}
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);
}
public void clearGrid(){ control.clearGrid(); }
public void multipleGenSkip(int generations){
control.multipleGenSkip(generations);
}
public int getGeneration(){
return control.getGeneration();
}
public void updateGameWindowTitle(int gen){
gameWindow.updateTitle(gen);
}
public int getVelocity(){
return control.getVelocity();
}
public void saveFirstGrid(){
control.saveFirstGrid();
}
public void previousGen(){
control.previousGen();
}
public void jump2Gen(int wantedtGen){
control.jump2Gen(wantedtGen);
}
public void saveGridInDB(String gridName){
control.saveGridInDB(gridName);
}
public List<String> getAllGrids(){
return control.getAllGrids();
}
public boolean checkName(String name){
return control.checkName(name);
}
//TODO: Delet this Methods. Those are shit.
public void setVisibility(boolean value){
controlWindow.setVisibility(value);
}
public ControlWindow getControlWindow() {
return controlWindow;
}
}
@@ -1,4 +1,4 @@
package gui.basics; package gui.fundamental;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@@ -1,4 +1,4 @@
package gui.basics; package gui.fundamental;
import javax.swing.text.AttributeSet; import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException; import javax.swing.text.BadLocationException;
@@ -1,4 +1,4 @@
package gui.basics; package gui.fundamental;
import javax.swing.border.Border; import javax.swing.border.Border;
import java.awt.*; import java.awt.*;
+46 -58
View File
@@ -1,9 +1,9 @@
package gui.windows; package gui.windows;
import gui.control.GUI; import gamecontrol.Control;
import gui.basics.Hover; import gui.fundamental.Hover;
import gui.basics.JTextFieldLimit; import gui.fundamental.JTextFieldLimit;
import gui.basics.RoundedBorder; import gui.fundamental.RoundedBorder;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
@@ -13,13 +13,10 @@ import javax.swing.event.DocumentListener;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
public class ControlWindow { public class ControlWindow extends GUI {
private final int width = 400, height = 800; private final int width = 400, height = 800;
private final Font head;
private final Font text;
private final JFrame controlWindow; private final JFrame controlWindow;
private JLabel heading; private JLabel heading;
private JButton toggleGame; private JButton toggleGame;
@@ -43,23 +40,19 @@ public class ControlWindow {
private JLabel velocitySliderName; private JLabel velocitySliderName;
private JTextField velocitySliderLabel; private JTextField velocitySliderLabel;
private final GUI gui; public ControlWindow(Control control) {
super(control);
public ControlWindow(GameWindow gameWindow, GUI gui, int offset) {
this.gui = gui;
this.head = gui.getHead();
this.text = gui.getText();
velocity = 500; velocity = 500;
initPublicComponents();
controlWindow = new JFrame(); controlWindow = new JFrame();
controlWindow.getContentPane().setPreferredSize(new Dimension(width, height + (2 * offset))); controlWindow.getContentPane().setPreferredSize(new Dimension(width, height + (2 * offset)));
controlWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_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(control.getGameWindowPos().x - (width + offset), control.getGameWindowPos().y);
controlWindow.setLayout(null); controlWindow.setLayout(null);
ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png"); ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png");
@@ -117,14 +110,14 @@ public class ControlWindow {
velocitySlider.setBounds(120, 110, 230, 20); velocitySlider.setBounds(120, 110, 230, 20);
velocitySlider.setFont(text); velocitySlider.setFont(text);
velocitySlider.setForeground(Color.decode("#121212")); velocitySlider.setForeground(Color.decode("#121212"));
velocitySlider.setValue(gui.getVelocity()); velocitySlider.setValue(getVelocity());
velocitySlider.addChangeListener(new ChangeListener() { velocitySlider.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if (!gui.isSliderLocked()) { if (!isSliderLocked()) {
velocity = velocitySlider.getValue(); velocity = velocitySlider.getValue();
velocitySliderLabel.setText(String.valueOf(velocity)); velocitySliderLabel.setText(String.valueOf(velocity));
gui.setVelocity(velocity); setVelocity(velocity);
} }
} }
}); });
@@ -137,7 +130,7 @@ public class ControlWindow {
velocitySliderName.setForeground(Color.decode("#121212")); velocitySliderName.setForeground(Color.decode("#121212"));
velocitySliderName.setVisible(true); velocitySliderName.setVisible(true);
velocitySliderLabel = new JTextField(String.valueOf(gui.getVelocity())); velocitySliderLabel = new JTextField(String.valueOf(getVelocity()));
velocitySliderLabel.setBackground(null); velocitySliderLabel.setBackground(null);
velocitySliderLabel.setBorder(null); velocitySliderLabel.setBorder(null);
velocitySliderLabel.setBounds(120, 80, 200, 30); velocitySliderLabel.setBounds(120, 80, 200, 30);
@@ -156,12 +149,12 @@ public class ControlWindow {
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
gui.setSliderLocked(true); setSliderLocked(true);
String s = velocitySliderLabel.getText(); String s = velocitySliderLabel.getText();
velocitySlider.setValue(Integer.parseInt(s)); velocitySlider.setValue(Integer.parseInt(s));
velocity = velocitySlider.getValue(); velocity = velocitySlider.getValue();
gui.setVelocity(velocity); setVelocity(velocity);
gui.setSliderLocked(false); setSliderLocked(false);
} }
@Override @Override
@@ -196,19 +189,19 @@ public class ControlWindow {
toggleGame.addActionListener(new ActionListener() { toggleGame.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (gui.getGeneration() == 0) { if (getGeneration() == 0) {
gui.saveFirstGrid(); saveFirstGrid();
} }
if (!gui.isRunning()) { if (!isRunning()) {
gui.setMouseLocked(true); setMouseLocked(true);
saveGrid.setFocusable(false); saveGrid.setFocusable(false);
updateLockedMouse(); updateLockedMouse();
} else { } else {
saveGrid.setFocusable(true); saveGrid.setFocusable(true);
} }
gui.setRunning(!gui.isRunning()); setRunning(!isRunning());
updateToggleButton(); updateToggleButton();
gui.setVelocity(velocity); setVelocity(velocity);
} }
}); });
@@ -230,12 +223,12 @@ public class ControlWindow {
nextGenButton.addActionListener(new ActionListener() { nextGenButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (!gui.isRunning()) { if (!isRunning()) {
if (gui.getGeneration() == 0) { if (getGeneration() == 0) {
gui.saveFirstGrid(); saveFirstGrid();
} }
gui.nextGen(); nextGen();
gui.showGrid(gui.getCells()); control.showGrid(getCells());
} }
} }
}); });
@@ -256,9 +249,9 @@ public class ControlWindow {
previousGenButton.addActionListener(new ActionListener() { previousGenButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (!gui.isRunning()) { if (!isRunning()) {
gui.previousGen(); previousGen();
gui.showGrid(gui.getCells()); control.showGrid(getCells());
} }
} }
}); });
@@ -278,8 +271,8 @@ public class ControlWindow {
lockMouse.addActionListener(new ActionListener() { lockMouse.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (!gui.isRunning()) { if (!isRunning()) {
gui.setMouseLocked(!gui.isMouseLocked()); setMouseLocked(!isMouseLocked());
updateLockedMouse(); updateLockedMouse();
} }
} }
@@ -302,9 +295,9 @@ public class ControlWindow {
clearButton.addActionListener(new ActionListener() { clearButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (!gui.isRunning()) { if (!isRunning()) {
gui.clearGrid(); clearGrid();
gui.showGrid(gui.getCells()); control.showGrid(getCells());
} }
} }
}); });
@@ -326,9 +319,9 @@ public class ControlWindow {
multipleGenSkipButton.addActionListener(new ActionListener() { multipleGenSkipButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (!gui.isRunning()) { if (!isRunning()) {
gui.multipleGenSkip(Integer.parseInt(multipleGenSkipLable.getText())); multipleGenSkip(Integer.parseInt(multipleGenSkipLable.getText()));
gui.showGrid(gui.getCells()); control.showGrid(getCells());
} }
} }
}); });
@@ -366,9 +359,9 @@ public class ControlWindow {
jump2GenButton.addActionListener(new ActionListener() { jump2GenButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (!gui.isRunning()) { if (!isRunning()) {
gui.jump2Gen(Integer.parseInt(jump2GenLable.getText())); jump2Gen(Integer.parseInt(jump2GenLable.getText()));
gui.showGrid(gui.getCells()); control.showGrid(getCells());
} }
} }
}); });
@@ -418,8 +411,8 @@ public class ControlWindow {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (gui.checkName(saveGrid.getText())) { if (checkName(saveGrid.getText())) {
gui.saveGridInDB(saveGrid.getText()); saveGridInDB(saveGrid.getText());
} else { } else {
System.out.println("Vergeben"); System.out.println("Vergeben");
} }
@@ -470,7 +463,7 @@ public class ControlWindow {
} }
private void updateToggleButton() { private void updateToggleButton() {
if (!gui.isRunning()) { if (!isRunning()) {
toggleGame.setText("Resume Game"); toggleGame.setText("Resume Game");
} else { } else {
toggleGame.setText("Pause Game"); toggleGame.setText("Pause Game");
@@ -479,7 +472,7 @@ public class ControlWindow {
private void updateLockedMouse() { private void updateLockedMouse() {
if (!gui.isMouseLocked()) { if (!isMouseLocked()) {
lockMouse.setText("Lock Mouse"); lockMouse.setText("Lock Mouse");
} else { } else {
lockMouse.setText("Unlock Mouse"); lockMouse.setText("Unlock Mouse");
@@ -489,9 +482,4 @@ public class ControlWindow {
public void dispose() { public void dispose() {
controlWindow.dispose(); controlWindow.dispose();
} }
public void setVelocity(int velocity) {
gui.setVelocity(velocity);
}
} }
+115
View File
@@ -0,0 +1,115 @@
package gui.windows;
import gamecontrol.Control;
import java.awt.*;
import java.util.List;
public abstract class GUI {
protected final String title = "Conway's game if Live";
protected final int offset = 20;
protected Control control;
protected Font head;
protected Font text;
protected boolean sliderLocked = false;
public GUI(Control control){
this.control = control;
}
public void initPublicComponents() {
head = new Font("Segoe UI Light", Font.PLAIN, 24);
text = new Font("Segoe UI Light", Font.PLAIN, 16);
}
public void buildGameWindow(int xSize, int ySize, int cellCount, String startMode) {
control.buildGameWindow(xSize, ySize, cellCount, startMode);
}
public void setCell(int mouseX, int mouseY, boolean value) {
control.setCell(mouseX, mouseY, value, offset);
}
public boolean isRunning() {
return control.isRunning();
}
public void setRunning(boolean state) {
control.setRunning(state);
}
public boolean isSliderLocked() {
return sliderLocked;
}
public void setSliderLocked(boolean sliderLocked) {
this.sliderLocked = sliderLocked;
}
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);
}
public void clearGrid() {
control.clearGrid();
}
public void multipleGenSkip(int generations) {
control.multipleGenSkip(generations);
}
public int getGeneration() {
return control.getGeneration();
}
public int getVelocity() {
return control.getVelocity();
}
public void setVelocity(int velocity) {
control.setVelocity(velocity);
}
public void saveFirstGrid() {
control.saveFirstGrid();
}
public void previousGen() {
control.previousGen();
}
public void jump2Gen(int wantedtGen) {
control.jump2Gen(wantedtGen);
}
public void saveGridInDB(String gridName) {
control.saveGridInDB(gridName);
}
public List<String> getAllGrids() {
return control.getAllGrids();
}
public boolean checkName(String name) {
return control.checkName(name);
}
}
+22 -17
View File
@@ -1,13 +1,13 @@
package gui.windows; package gui.windows;
import gui.control.GUI; import gamecontrol.Control;
import gui.draw.DrawGrid; import gui.draw.DrawGrid;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
public class GameWindow { public class GameWindow extends GUI {
private final JFrame gameWindow; private final JFrame gameWindow;
private final DrawGrid drawGrid; private final DrawGrid drawGrid;
@@ -22,13 +22,14 @@ 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, int offset) { public GameWindow(Control control, boolean manual, int cellCountX, int cellCountY, int width, int height) {
super(control);
this.cellCountX = cellCountX; this.cellCountX = cellCountX;
this.cellCountY = cellCountY; this.cellCountY = cellCountY;
this.title = title; initPublicComponents();
gameWindow = new JFrame(title + " | Generation: " + gui.getGeneration()); gameWindow = new JFrame(title + " | Generation: " + getGeneration());
gameWindow.getContentPane().setPreferredSize(new Dimension(width + (2 * offset), height + (2 * offset))); gameWindow.getContentPane().setPreferredSize(new Dimension(width + (2 * offset), height + (2 * offset)));
gameWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); gameWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
@@ -36,19 +37,22 @@ public class GameWindow {
ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png"); ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png");
gameWindow.setIconImage(imageIcon.getImage()); gameWindow.setIconImage(imageIcon.getImage());
if (manual){
control.buildControlWindow();
}
gameWindow.addWindowListener(new WindowAdapter() { gameWindow.addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
super.windowClosing(e); super.windowClosing(e);
if (gui.getControlWindow() != null){ if (control.getControlWindow() != null){
gui.getControlWindow().dispose(); control.getControlWindow().dispose();
} }
gameWindow.dispose(); gameWindow.dispose();
gui.setRunning(false); setRunning(false);
gui.getMenuWindow().setVisibility(true); control.getMenuWindow().setVisibility(true);
gui.getMenuWindow().setFocus(); control.getMenuWindow().setFocus();
} }
}); });
@@ -61,10 +65,10 @@ public class GameWindow {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if (e.isControlDown() && e.isAltDown() && e.getKeyCode() == KeyEvent.VK_C) { if (e.isControlDown() && e.isAltDown() && e.getKeyCode() == KeyEvent.VK_C) {
if (gui.getControlWindow() == null){ if (control.getControlWindow() == null){
gui.buildControlWindow(); control.buildControlWindow();
} else { } else {
gui.setVisibility(true); control.setVisibility(true);
} }
} }
@@ -81,9 +85,9 @@ public class GameWindow {
@Override @Override
public void mouseDragged(MouseEvent e) { public void mouseDragged(MouseEvent e) {
if (mouseLeft) { if (mouseLeft) {
gui.setCell(e.getX(), e.getY(), true); setCell(e.getX(), e.getY(), true);
} else if (mouseRight) { } else if (mouseRight) {
gui.setCell(e.getX(), e.getY(), false); setCell(e.getX(), e.getY(), false);
} }
} }
@@ -104,11 +108,11 @@ public class GameWindow {
switch (e.getButton()) { switch (e.getButton()) {
case 1 -> { case 1 -> {
mouseLeft = true; mouseLeft = true;
gui.setCell(e.getX(), e.getY(), true); setCell(e.getX(), e.getY(), true);
} }
case 3 -> { case 3 -> {
mouseRight = true; mouseRight = true;
gui.setCell(e.getX(), e.getY(), false); setCell(e.getX(), e.getY(), false);
} }
} }
} }
@@ -151,6 +155,7 @@ public class GameWindow {
gameWindow.setVisible(true); gameWindow.setVisible(true);
} }
public void showGrid(boolean[][] grid) { public void showGrid(boolean[][] grid) {
if (drawGrid != null) { if (drawGrid != null) {
drawGrid.showGrid(grid); drawGrid.showGrid(grid);
+21 -28
View File
@@ -1,9 +1,9 @@
package gui.windows; package gui.windows;
import gui.control.GUI; import gamecontrol.Control;
import gui.basics.Hover; import gui.fundamental.Hover;
import gui.basics.RoundedBorder; import gui.fundamental.RoundedBorder;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
@@ -13,15 +13,11 @@ import javax.swing.event.DocumentListener;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
public class MenuWindow { public class MenuWindow extends GUI {
private GUI gui;
private final JFrame menuWindow; private final JFrame menuWindow;
private Font head;
private Font text;
private JLabel heading; private JLabel heading;
private final int xAxisMin = 6, xAxisMax = 2048; private final int xAxisMin = 6, xAxisMax = 2048;
@@ -51,13 +47,13 @@ public class MenuWindow {
private ImageIcon preview; private ImageIcon preview;
private JLabel previewLabel; private JLabel previewLabel;
public MenuWindow(String title, GUI gui){ public MenuWindow(Control control){
super(control);
this.gui = gui; initPublicComponents();
menuWindow = new JFrame(); menuWindow = new JFrame();
menuWindow.setTitle(title); menuWindow.setTitle(this.title);
ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png"); ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png");
menuWindow.setIconImage(imageIcon.getImage()); menuWindow.setIconImage(imageIcon.getImage());
@@ -83,10 +79,6 @@ public class MenuWindow {
private void initComponents() { private void initComponents() {
this.head = gui.getHead();
this.text = gui.getText();
initHeading(); initHeading();
initXAxis(); initXAxis();
initYAxis(); initYAxis();
@@ -114,7 +106,7 @@ public class MenuWindow {
xAxis.addChangeListener(new ChangeListener() { xAxis.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if(!gui.isSliderLocked()) { if(!isSliderLocked()) {
xAxisSize = xAxis.getValue(); xAxisSize = xAxis.getValue();
updateSliderLabels(0, xAxisSize); updateSliderLabels(0, xAxisSize);
updateStartCells(); updateStartCells();
@@ -149,12 +141,12 @@ public class MenuWindow {
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
gui.setSliderLocked(true); setSliderLocked(true);
String s = xAxisLabel.getText(); String s = xAxisLabel.getText();
xAxis.setValue(Integer.parseInt(s)); xAxis.setValue(Integer.parseInt(s));
xAxisSize = xAxis.getValue(); xAxisSize = xAxis.getValue();
updateStartCells(); updateStartCells();
gui.setSliderLocked(false); setSliderLocked(false);
} }
@Override @Override
@@ -178,7 +170,7 @@ public class MenuWindow {
yAxis.addChangeListener(new ChangeListener() { yAxis.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if(!gui.isSliderLocked()) { if(!isSliderLocked()) {
yAxisSize = yAxis.getValue(); yAxisSize = yAxis.getValue();
updateSliderLabels(1, yAxisSize); updateSliderLabels(1, yAxisSize);
updateStartCells(); updateStartCells();
@@ -212,12 +204,12 @@ public class MenuWindow {
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
gui.setSliderLocked(true); setSliderLocked(true);
String s = yAxisLabel.getText(); String s = yAxisLabel.getText();
yAxis.setValue(Integer.parseInt(s)); yAxis.setValue(Integer.parseInt(s));
yAxisSize = yAxis.getValue(); yAxisSize = yAxis.getValue();
updateStartCells(); updateStartCells();
gui.setSliderLocked(false); setSliderLocked(false);
} }
@Override @Override
@@ -241,7 +233,7 @@ public class MenuWindow {
startCells.addChangeListener(new ChangeListener() { startCells.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if(!gui.isSliderLocked()) { if(!isSliderLocked()) {
cellCount = startCells.getValue(); cellCount = startCells.getValue();
updateSliderLabels(2, cellCount); updateSliderLabels(2, cellCount);
updateStartCells(); updateStartCells();
@@ -275,11 +267,11 @@ public class MenuWindow {
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
gui.setSliderLocked(true); setSliderLocked(true);
String s = startCellsLabel.getText(); String s = startCellsLabel.getText();
startCells.setValue(Integer.parseInt(s)); startCells.setValue(Integer.parseInt(s));
cellCount = startCells.getValue(); cellCount = startCells.getValue();
gui.setSliderLocked(false); setSliderLocked(false);
} }
@Override @Override
@@ -303,8 +295,8 @@ public class MenuWindow {
addComboBoxEntry(modeBox, "Randomized"); addComboBoxEntry(modeBox, "Randomized");
addComboBoxEntry(modeBox, "Manuel"); addComboBoxEntry(modeBox, "Manuel");
for (int i = 0; i < gui.getAllGrids().size(); i++){ for (int i = 0; i < getAllGrids().size(); i++){
addComboBoxEntry(modeBox, gui.getAllGrids().get(i)); addComboBoxEntry(modeBox, getAllGrids().get(i));
} }
modeName = new JLabel("Select Grid: "); modeName = new JLabel("Select Grid: ");
@@ -330,7 +322,7 @@ public class MenuWindow {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
gui.buildGameWindow(xAxisSize, yAxisSize, cellCount, modeBox.getSelectedItem().toString()); buildGameWindow(xAxisSize, yAxisSize, cellCount, modeBox.getSelectedItem().toString());
menuWindow.setVisible(false); menuWindow.setVisible(false);
} }
@@ -381,6 +373,7 @@ public class MenuWindow {
menuWindow.setVisible(visibility); menuWindow.setVisible(visibility);
} }
public void setFocus(){ public void setFocus(){
menuWindow.requestFocus(); menuWindow.requestFocus();
} }
+6 -5
View File
@@ -2,6 +2,7 @@ package main;
import database.ReadDAO; import database.ReadDAO;
import gamecontrol.Control;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -29,12 +30,12 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// Control control = new Control(); Control control = new Control();
// control.start(); control.start();
ReadDAO readDAO = new ReadDAO(); // ReadDAO readDAO = new ReadDAO();
String[] x = readDAO.getGameInfo("test", new String[]{"MAX_CELLCOUNT", "GENERATION"}); // String[] x = readDAO.getGameInfo("test", new String[]{"MAX_CELLCOUNT", "GENERATION"});
System.out.println(Arrays.toString(x)); // System.out.println(Arrays.toString(x));
} }