This commit is contained in:
Noah
2021-12-02 19:13:06 +01:00
parent e1c6b1cffc
commit cb8e4246db
9 changed files with 264 additions and 226 deletions
Binary file not shown.
+60
View File
@@ -1486,3 +1486,63 @@ java.sql.SQLClientInfoException: Client info name 'ApplicationName' not supporte
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829) at java.base/java.lang.Thread.run(Thread.java:829)
2021-12-02 14:22:39 jdbc[3]: exception
java.sql.SQLClientInfoException: Client info name 'ApplicationName' not supported.
at org.h2.jdbc.JdbcConnection.setClientInfo(JdbcConnection.java:1749)
at com.intellij.database.remote.jdbc.impl.RemoteConnectionImpl.setClientInfo(RemoteConnectionImpl.java:470)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
2021-12-02 14:22:43 jdbc[4]: exception
java.sql.SQLClientInfoException: Client info name 'ApplicationName' not supported.
at org.h2.jdbc.JdbcConnection.setClientInfo(JdbcConnection.java:1749)
at com.intellij.database.remote.jdbc.impl.RemoteConnectionImpl.setClientInfo(RemoteConnectionImpl.java:470)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
2021-12-02 15:26:55 jdbc[3]: exception
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
No data is available [2000-200]
2021-12-02 15:26:55 jdbc[3]: exception
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
No data is available [2000-200]
2021-12-02 15:26:58 jdbc[3]: exception
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
No data is available [2000-200]
2021-12-02 15:26:58 jdbc[3]: exception
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
No data is available [2000-200]
2021-12-02 15:26:59 jdbc[3]: exception
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
No data is available [2000-200]
2021-12-02 15:26:59 jdbc[3]: exception
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
No data is available [2000-200]
+35 -9
View File
@@ -9,7 +9,7 @@ public class DataBase {
private Connection connection; private Connection connection;
private Statement statement; private Statement statement;
public void connect(){ private void connect(){
try { try {
connection = DriverManager.getConnection("jdbc:h2:./lib/Conway-Game-Of-Live", "sa", ""); connection = DriverManager.getConnection("jdbc:h2:./lib/Conway-Game-Of-Live", "sa", "");
statement = connection.createStatement(); statement = connection.createStatement();
@@ -18,7 +18,7 @@ public class DataBase {
} }
} }
public void disconnect(){ private void disconnect(){
try { try {
statement.close(); statement.close();
@@ -29,7 +29,7 @@ public class DataBase {
} }
public void simpleSQL(String sqlStatement){ private void simpleSQL(String sqlStatement){
try { try {
statement.executeUpdate(sqlStatement); statement.executeUpdate(sqlStatement);
} catch (SQLException e) { } catch (SQLException e) {
@@ -37,7 +37,7 @@ public class DataBase {
} }
} }
public ResultSet resultSetSQL(String sqlStatement){ private ResultSet resultSetSQL(String sqlStatement){
try { try {
return statement.executeQuery(sqlStatement); return statement.executeQuery(sqlStatement);
} catch (SQLException e) { } catch (SQLException e) {
@@ -51,7 +51,7 @@ public class DataBase {
public void createTables(){ public void createTables(){
connect(); connect();
simpleSQL("create table gameInfo(id int not null auto_increment, name varchar, width int, height int, generation int)"); simpleSQL("create table gameInfo(id int not null auto_increment, name varchar, width int, height int, generation int, cellcount int)");
simpleSQL("create table gameData(id int, x int, y int)"); simpleSQL("create table gameData(id int, x int, y int)");
disconnect(); disconnect();
@@ -93,11 +93,11 @@ public class DataBase {
} }
public void saveGrid(boolean[][] grid, String gridName, int generation){ public void saveGrid(boolean[][] grid, String gridName, int generation, int cellCount){
connect(); connect();
simpleSQL("insert into GAMEINFO(name, width, height, GENERATION) values ('"+gridName+"', "+grid.length+", "+grid[0].length+", "+generation+")"); simpleSQL("insert into GAMEINFO(NAME, WIDTH, HEIGHT, GENERATION, CELLCOUNT) values ('"+gridName+"', "+grid.length+", "+grid[0].length+", "+generation+","+cellCount+")");
ResultSet resultSet = resultSetSQL("select * from gameInfo order by id desc limit 1"); ResultSet resultSet = resultSetSQL("select * from gameInfo order by id desc limit 1");
@@ -125,8 +125,6 @@ public class DataBase {
connect(); connect();
ResultSet resultInfo = resultSetSQL("select * from gameInfo where name = '"+gridName+"'"); ResultSet resultInfo = resultSetSQL("select * from gameInfo where name = '"+gridName+"'");
try { try {
@@ -173,4 +171,32 @@ public class DataBase {
} }
public int getCellCount(String gridName){
connect();
ResultSet resultSet = resultSetSQL("select CELLCOUNT from gameInfo where name = '"+gridName+"'");
try {
resultSet.next();
int cellCount = resultSet.getInt("CELLCOUNT");
disconnect();
return cellCount;
} catch (SQLException e) {
e.printStackTrace();
}
disconnect();
return 0;
}
public boolean checkName(String name){
List<String> allNames = getAllGrids();
if (allNames.contains(name)){
return false;
} else {
return true;
}
}
} }
+60 -122
View File
@@ -3,6 +3,7 @@ package game;
import data.database.DataBase; import data.database.DataBase;
import gui.GUI; import gui.GUI;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@@ -39,6 +40,36 @@ public class Control {
} }
public void buildGameWindow(int xSize, int ySize, int cellCount, String startMode) { public void buildGameWindow(int xSize, int ySize, int cellCount, String startMode) {
switch (startMode) {
case "Randomized" -> {
generateRandomized(xSize, ySize, cellCount);
}
case "Manuel" -> {
buildManuelGame(xSize, ySize, cellCount);
}
default -> {
getFromDatabase(startMode);
}
}
saveFirstGrid();
calcSize();
gui.createGameWindow(this.xSize, this.ySize, this.width, this.height);
if (startMode == "Manuel"){
gui.buildControlWindow();
}
startGame();
}
private void buildManuelGame(int xSize, int ySize, int cellCount) {
this.xSize = xSize; this.xSize = xSize;
this.ySize = ySize; this.ySize = ySize;
this.cellCount = cellCount; this.cellCount = cellCount;
@@ -52,42 +83,6 @@ public class Control {
newcells = new boolean[this.xSize][this.ySize]; newcells = new boolean[this.xSize][this.ySize];
startingCells = new boolean[this.xSize][this.ySize]; startingCells = new boolean[this.xSize][this.ySize];
switch (startMode) {
case "Task1" -> {
generateTask1();
}
case "Randomized" -> {
generateRandomized();
}
case "Manuel" -> {
buildManuelGame();
}
default -> {
getFromDatabase(startMode);
}
}
saveFirstGrid();
calcSize();
gui.createGameWindow(this.xSize, this.ySize, this.width, this.height);
setRunning(true);
if (startMode == "Manuel") {
setRunning(false);
gui.buildControlWindow();
this.mouseLocked = false;
}
startGame();
}
private void buildManuelGame() {
for (int x = 0; x < xSize; x++) { for (int x = 0; x < xSize; x++) {
for (int y = 0; y < ySize; y++) { for (int y = 0; y < ySize; y++) {
cells[x][y] = false; cells[x][y] = false;
@@ -96,36 +91,39 @@ public class Control {
syncCells(); syncCells();
setRunning(false);
this.mouseLocked = false;
} }
private void getFromDatabase(String name){ private void getFromDatabase(String name){
cells = dataBase.getGrid(name); cells = dataBase.getGrid(name);
gen = dataBase.getGen(name); gen = dataBase.getGen(name);
this.xSize = cells.length;
this.ySize = cells[0].length;
this.cellCount = this.xSize * this.ySize;
newcells = new boolean[this.xSize][this.ySize];
startingCells = new boolean[this.xSize][this.ySize];
syncCells(); syncCells();
setRunning(true);
} }
private void generateTask1() { private void generateRandomized(int xSize, int ySize, int cellCount) {
for (int x = 0; x < xSize; x++) { this.xSize = xSize;
for (int y = 0; y < ySize; y++) { this.ySize = ySize;
cells[x][y] = false; this.cellCount = cellCount;
}
}
clock.setFirstGen(true);
cells[1][2] = true; gen = 0;
cells[2][2] = true; mouseLocked = true;
cells[2][4] = true;
cells[3][1] = true;
cells[3][3] = true;
cells[4][3] = true;
cells = new boolean[this.xSize][this.ySize];
newcells = new boolean[this.xSize][this.ySize];
startingCells = new boolean[this.xSize][this.ySize];
syncCells();
}
private void generateRandomized() {
for (int i = 0; i < this.cellCount; i++) { for (int i = 0; i < this.cellCount; i++) {
int x = rand(0, this.xSize); int x = rand(0, this.xSize);
int y = rand(0, this.ySize); int y = rand(0, this.ySize);
@@ -136,7 +134,7 @@ public class Control {
} }
} }
syncCells(); syncCells();
setRunning(true);
} }
private void syncCells() { private void syncCells() {
@@ -236,71 +234,6 @@ public class Control {
} }
} }
// public void checkN(Boolean[][] arr, Boolean[][] futureGen, int columns, int rows) {
// ArrayList<Thread> threads = new ArrayList<>();
// final int NUM_OF_THREADS = 8; //Or can be passed as an argument
// for (int tid = 0; tid < NUM_OF_THREADS; tid++) {
// Integer thread_local_row_start = tid * rows / NUM_OF_THREADS;
// Integer thread_local_row_end = (tid + 1) * rows / NUM_OF_THREADS;
// Thread t = new Thread(() -> {
// for (int row = thread_local_row_start; row < thread_local_row_end; row++) {
// for (int column = 0; column < columns; column++) {
// countNeighbors(arr, row, column, rows, columns);
// evaluateNeighbors(arr[row][column], futureGen[row][column]);
// }
// }
// });
// t.start();
// threads.add(t);
// }
// for (Thread t : threads) {
// try {
// t.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
public boolean[][] nextGenBackup() {
gen++;
// System.out.println("Generation:" + gen);
for (int x = 0; x < xSize; x++) {
for (int y = 0; y < ySize; y++) {
int n = aliveNeigbours(x, y);
if (n == 3 && !cells[x][y]) {
newcells[x][y] = true;
}
if (n < 2) {
newcells[x][y] = false;
}
if (n == 2 || n == 3) {
}
if (n > 3) {
newcells[x][y] = false;
}
}
}
for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) {
cells[x][y] = newcells[x][y];
}
}
return cells;
}
private int aliveNeigbours(int x, int y) { private int aliveNeigbours(int x, int y) {
int count = 0; int count = 0;
@@ -318,17 +251,14 @@ public class Control {
return count; return count;
} }
private int rand(int min, int max) { private int rand(int min, int max) {
return ThreadLocalRandom.current().nextInt(min, max); return ThreadLocalRandom.current().nextInt(min, max);
} }
public void showGrid(boolean[][] grid) { public void showGrid(boolean[][] grid) {
gui.showGrid(grid); gui.showGrid(grid);
} }
public void setVisibility(boolean value) { public void setVisibility(boolean value) {
gui.setVisibility(value); gui.setVisibility(value);
} }
@@ -456,6 +386,14 @@ public class Control {
public List<String> getAllGrids(){ public List<String> getAllGrids(){
return dataBase.getAllGrids(); return dataBase.getAllGrids();
} }
public void saveGridInDB(String gridName){
dataBase.saveGrid(this.cells, gridName, this.gen, this.cellCount);
}
public boolean checkName(String name){
return dataBase.checkName(name);
}
} }
+61 -32
View File
@@ -12,11 +12,10 @@ public class ControlWindow {
private final int width = 400, height = 800; private final int width = 400, height = 800;
private Font head; private final Font head;
private Font text; private final Font text;
private MouseAdapter hover;
private JFrame controlWindow; private final JFrame controlWindow;
private JLabel heading; private JLabel heading;
private JButton toggleGame; private JButton toggleGame;
private JButton nextGenButton; private JButton nextGenButton;
@@ -30,14 +29,16 @@ public class ControlWindow {
private JTextField saveGrid; private JTextField saveGrid;
private JLabel saveGridPlaceholder; private JLabel saveGridPlaceholder;
private int velocity, minVelocity = 1 , maxVelocity = 5000; private int velocity;
private final int minVelocity = 1;
private final int maxVelocity = 5000;
private JSlider velocitySlider; private JSlider velocitySlider;
private JLabel velocitySliderName; private JLabel velocitySliderName;
private JTextField velocitySliderLabel; private JTextField velocitySliderLabel;
private GUI gui; private final GUI gui;
public ControlWindow(GameWindow gameWindow, GUI gui, int offset) { public ControlWindow(GameWindow gameWindow, GUI gui, int offset) {
@@ -45,7 +46,6 @@ public class ControlWindow {
this.head = gui.getHead(); this.head = gui.getHead();
this.text = gui.getText(); this.text = gui.getText();
this.hover = gui.getHover();
velocity = 500; velocity = 500;
@@ -68,18 +68,25 @@ public class ControlWindow {
} }
}); });
controlWindow.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
saveGrid.setFocusable(false);
saveGrid.setFocusable(true);
}
});
controlWindow.pack(); controlWindow.pack();
controlWindow.setVisible(true); controlWindow.setVisible(true);
initComponents(); initComponents();
addComponents(); addComponents();
} }
private void initComponents(){ private void initComponents() {
initHeading(); initHeading();
initVelocity(); initVelocity();
initToggleButton(); initToggleButton();
@@ -92,7 +99,7 @@ public class ControlWindow {
initSaveGrid(); initSaveGrid();
} }
private void initHeading(){ private void initHeading() {
heading = new JLabel("Control Panel"); heading = new JLabel("Control Panel");
heading.setFont(head); heading.setFont(head);
heading.setForeground(Color.decode("#121212")); heading.setForeground(Color.decode("#121212"));
@@ -100,7 +107,7 @@ public class ControlWindow {
heading.setVisible(true); heading.setVisible(true);
} }
private void initVelocity(){ private void initVelocity() {
velocitySlider = new JSlider(minVelocity, maxVelocity); velocitySlider = new JSlider(minVelocity, maxVelocity);
velocitySlider.setBounds(120, 110, 230, 20); velocitySlider.setBounds(120, 110, 230, 20);
velocitySlider.setFont(text); velocitySlider.setFont(text);
@@ -109,7 +116,7 @@ public class ControlWindow {
velocitySlider.addChangeListener(new ChangeListener() { velocitySlider.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
if(!gui.isSliderLocked()) { if (!gui.isSliderLocked()) {
velocity = velocitySlider.getValue(); velocity = velocitySlider.getValue();
velocitySliderLabel.setText(String.valueOf(velocity)); velocitySliderLabel.setText(String.valueOf(velocity));
gui.setVelocity(velocity); gui.setVelocity(velocity);
@@ -166,7 +173,7 @@ public class ControlWindow {
} }
private void initToggleButton(){ private void initToggleButton() {
toggleGame = new JButton(); toggleGame = new JButton();
toggleGame.setText("Start Game"); toggleGame.setText("Start Game");
@@ -177,19 +184,22 @@ public class ControlWindow {
toggleGame.setContentAreaFilled(false); toggleGame.setContentAreaFilled(false);
toggleGame.setBounds(50, 750, 300, 50); toggleGame.setBounds(50, 750, 300, 50);
toggleGame.setVisible(true); toggleGame.setVisible(true);
toggleGame.addMouseListener(hover); toggleGame.addMouseListener(new Hover());
toggleGame.setBorder(new RoundedBorder(25)); toggleGame.setBorder(new RoundedBorder(25));
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 (gui.getGeneration() == 0) {
gui.saveFirstGrid(); gui.saveFirstGrid();
} }
if (!gui.isRunning()){ if (!gui.isRunning()) {
gui.setMouseLocked(true); gui.setMouseLocked(true);
saveGrid.setFocusable(false);
updateLockedMouse(); updateLockedMouse();
} else {
saveGrid.setFocusable(true);
} }
gui.setRunning(!gui.isRunning()); gui.setRunning(!gui.isRunning());
updateToggleButton(); updateToggleButton();
@@ -200,7 +210,7 @@ public class ControlWindow {
updateToggleButton(); updateToggleButton();
} }
private void initNextButton(){ private void initNextButton() {
nextGenButton = new JButton(); nextGenButton = new JButton();
nextGenButton.setText("Next Gen."); nextGenButton.setText("Next Gen.");
nextGenButton.setFont(text); nextGenButton.setFont(text);
@@ -210,7 +220,7 @@ public class ControlWindow {
nextGenButton.setContentAreaFilled(false); nextGenButton.setContentAreaFilled(false);
nextGenButton.setBounds(225, 675, 125, 50); nextGenButton.setBounds(225, 675, 125, 50);
nextGenButton.setVisible(true); nextGenButton.setVisible(true);
nextGenButton.addMouseListener(hover); nextGenButton.addMouseListener(new Hover());
nextGenButton.setBorder(new RoundedBorder(25)); nextGenButton.setBorder(new RoundedBorder(25));
nextGenButton.addActionListener(new ActionListener() { nextGenButton.addActionListener(new ActionListener() {
@Override @Override
@@ -226,7 +236,7 @@ public class ControlWindow {
}); });
} }
private void initPreviousGenButton(){ private void initPreviousGenButton() {
previousGenButton = new JButton(); previousGenButton = new JButton();
previousGenButton.setText("Prior Gen."); previousGenButton.setText("Prior Gen.");
previousGenButton.setFont(text); previousGenButton.setFont(text);
@@ -236,7 +246,7 @@ public class ControlWindow {
previousGenButton.setContentAreaFilled(false); previousGenButton.setContentAreaFilled(false);
previousGenButton.setBounds(50, 675, 125, 50); previousGenButton.setBounds(50, 675, 125, 50);
previousGenButton.setVisible(true); previousGenButton.setVisible(true);
previousGenButton.addMouseListener(hover); previousGenButton.addMouseListener(new Hover());
previousGenButton.setBorder(new RoundedBorder(25)); previousGenButton.setBorder(new RoundedBorder(25));
previousGenButton.addActionListener(new ActionListener() { previousGenButton.addActionListener(new ActionListener() {
@Override @Override
@@ -249,7 +259,7 @@ public class ControlWindow {
}); });
} }
private void initDrawLockButton(){ private void initDrawLockButton() {
lockMouse = new JButton(); lockMouse = new JButton();
lockMouse.setFont(text); lockMouse.setFont(text);
lockMouse.setForeground(Color.decode("#121212")); lockMouse.setForeground(Color.decode("#121212"));
@@ -258,7 +268,7 @@ public class ControlWindow {
lockMouse.setContentAreaFilled(false); lockMouse.setContentAreaFilled(false);
lockMouse.setBounds(50, 450, 300, 50); lockMouse.setBounds(50, 450, 300, 50);
lockMouse.setVisible(true); lockMouse.setVisible(true);
lockMouse.addMouseListener(hover); lockMouse.addMouseListener(new Hover());
lockMouse.setBorder(new RoundedBorder(25)); lockMouse.setBorder(new RoundedBorder(25));
lockMouse.addActionListener(new ActionListener() { lockMouse.addActionListener(new ActionListener() {
@Override @Override
@@ -272,7 +282,7 @@ public class ControlWindow {
updateLockedMouse(); updateLockedMouse();
} }
private void initClearButton(){ private void initClearButton() {
clearButton = new JButton(); clearButton = new JButton();
clearButton.setText("Clear Grid"); clearButton.setText("Clear Grid");
clearButton.setFont(text); clearButton.setFont(text);
@@ -282,7 +292,7 @@ public class ControlWindow {
clearButton.setContentAreaFilled(false); clearButton.setContentAreaFilled(false);
clearButton.setBounds(50, 375, 300, 50); clearButton.setBounds(50, 375, 300, 50);
clearButton.setVisible(true); clearButton.setVisible(true);
clearButton.addMouseListener(hover); clearButton.addMouseListener(new Hover());
clearButton.setBorder(new RoundedBorder(25)); clearButton.setBorder(new RoundedBorder(25));
clearButton.addActionListener(new ActionListener() { clearButton.addActionListener(new ActionListener() {
@Override @Override
@@ -295,7 +305,7 @@ public class ControlWindow {
}); });
} }
private void initMultibleGenSkip(){ private void initMultibleGenSkip() {
multipleGenSkipButton = new JButton(); multipleGenSkipButton = new JButton();
multipleGenSkipButton.setText("Next Generation"); multipleGenSkipButton.setText("Next Generation");
@@ -306,7 +316,7 @@ public class ControlWindow {
multipleGenSkipButton.setContentAreaFilled(false); multipleGenSkipButton.setContentAreaFilled(false);
multipleGenSkipButton.setBounds(50, 600, 300, 50); multipleGenSkipButton.setBounds(50, 600, 300, 50);
multipleGenSkipButton.setVisible(true); multipleGenSkipButton.setVisible(true);
multipleGenSkipButton.addMouseListener(hover); multipleGenSkipButton.addMouseListener(new Hover());
multipleGenSkipButton.setBorder(new RoundedBorder(25)); multipleGenSkipButton.setBorder(new RoundedBorder(25));
multipleGenSkipButton.addActionListener(new ActionListener() { multipleGenSkipButton.addActionListener(new ActionListener() {
@Override @Override
@@ -335,7 +345,7 @@ public class ControlWindow {
}); });
} }
private void initJump2Gen(){ private void initJump2Gen() {
jump2GenButton = new JButton(); jump2GenButton = new JButton();
jump2GenButton.setText("Jump to generation"); jump2GenButton.setText("Jump to generation");
@@ -346,7 +356,7 @@ public class ControlWindow {
jump2GenButton.setContentAreaFilled(false); jump2GenButton.setContentAreaFilled(false);
jump2GenButton.setBounds(50, 525, 300, 50); jump2GenButton.setBounds(50, 525, 300, 50);
jump2GenButton.setVisible(true); jump2GenButton.setVisible(true);
jump2GenButton.addMouseListener(hover); jump2GenButton.addMouseListener(new Hover());
jump2GenButton.setBorder(new RoundedBorder(25)); jump2GenButton.setBorder(new RoundedBorder(25));
jump2GenButton.addActionListener(new ActionListener() { jump2GenButton.addActionListener(new ActionListener() {
@Override @Override
@@ -375,7 +385,7 @@ public class ControlWindow {
}); });
} }
private void initSaveGrid(){ private void initSaveGrid() {
saveGridPlaceholder = new JLabel("Save Grid..."); saveGridPlaceholder = new JLabel("Save Grid...");
saveGridPlaceholder.setBounds(50, 300, 300, 50); saveGridPlaceholder.setBounds(50, 300, 300, 50);
@@ -385,6 +395,7 @@ public class ControlWindow {
saveGrid = new JTextField(); saveGrid = new JTextField();
saveGrid.setDocument(new JTextFieldLimit(10)); saveGrid.setDocument(new JTextFieldLimit(10));
saveGrid.setFocusable(false);
saveGrid.setOpaque(false); saveGrid.setOpaque(false);
saveGrid.setBackground(null); saveGrid.setBackground(null);
saveGrid.setBorder(null); saveGrid.setBorder(null);
@@ -398,6 +409,21 @@ public class ControlWindow {
evt.consume(); evt.consume();
} }
} }
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (gui.checkName(saveGrid.getText())) {
gui.saveGridInDB(saveGrid.getText());
} else {
System.out.println("Vergeben");
}
saveGrid.setText("");
saveGridPlaceholder.setVisible(true);
}
}
}); });
saveGrid.addFocusListener(new FocusListener() { saveGrid.addFocusListener(new FocusListener() {
@Override @Override
@@ -407,13 +433,16 @@ public class ControlWindow {
@Override @Override
public void focusLost(FocusEvent e) { public void focusLost(FocusEvent e) {
if (saveGrid.getText().equals("")) {
saveGridPlaceholder.setVisible(true); saveGridPlaceholder.setVisible(true);
} }
}
}); });
} }
private void addComponents(){ private void addComponents() {
controlWindow.add(heading); controlWindow.add(heading);
controlWindow.add(velocitySlider); controlWindow.add(velocitySlider);
controlWindow.add(velocitySliderName); controlWindow.add(velocitySliderName);
@@ -452,12 +481,12 @@ public class ControlWindow {
} }
} }
public void dispose(){ public void dispose() {
controlWindow.dispose(); controlWindow.dispose();
} }
public void setVelocity(int velocity){ public void setVelocity(int velocity) {
gui.setVelocity(velocity); gui.setVelocity(velocity);
} }
} }
+12 -32
View File
@@ -2,10 +2,7 @@ package gui;
import game.Control; import game.Control;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List; import java.util.List;
public class GUI { public class GUI {
@@ -16,7 +13,6 @@ public class GUI {
private Font head; private Font head;
private Font text; private Font text;
private MouseAdapter hover;
private boolean sliderLocked = false; private boolean sliderLocked = false;
private final int offset = 20; private final int offset = 20;
@@ -36,27 +32,6 @@ public class GUI {
head = new Font("Segoe UI Light", Font.PLAIN, 24); head = new Font("Segoe UI Light", Font.PLAIN, 24);
text = new Font("Segoe UI Light", Font.PLAIN, 16); 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);
}
};
} }
public void createMenuWindow(){ public void createMenuWindow(){
@@ -119,10 +94,6 @@ public class GUI {
this.sliderLocked = sliderLocked; this.sliderLocked = sliderLocked;
} }
public MouseAdapter getHover() {
return hover;
}
public void nextGen(){ public void nextGen(){
control.nextGen(); control.nextGen();
} }
@@ -167,6 +138,18 @@ public class GUI {
control.jump2Gen(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. //TODO: Delet this Methods. Those are shit.
public void setVisibility(boolean value){ public void setVisibility(boolean value){
controlWindow.setVisibility(value); controlWindow.setVisibility(value);
@@ -176,9 +159,6 @@ public class GUI {
return controlWindow; return controlWindow;
} }
public List<String> getAllGrids(){
return control.getAllGrids();
}
} }
+27
View File
@@ -0,0 +1,27 @@
package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Hover extends 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);
}
}
+1 -6
View File
@@ -81,8 +81,6 @@ public class MenuWindow {
this.head = gui.getHead(); this.head = gui.getHead();
this.text = gui.getText(); this.text = gui.getText();
this.hover = gui.getHover();
initHeading(); initHeading();
@@ -300,9 +298,6 @@ public class MenuWindow {
modeBox.setBounds(180, 257, 200, 20); modeBox.setBounds(180, 257, 200, 20);
addComboBoxEntry(modeBox, "Randomized"); addComboBoxEntry(modeBox, "Randomized");
addComboBoxEntry(modeBox, "Manuel"); addComboBoxEntry(modeBox, "Manuel");
addComboBoxEntry(modeBox, "Task1");
addComboBoxEntry(modeBox, "Blinker");
addComboBoxEntry(modeBox, "Toad");
for (int i = 0; i < gui.getAllGrids().size(); i++){ for (int i = 0; i < gui.getAllGrids().size(); i++){
addComboBoxEntry(modeBox, gui.getAllGrids().get(i)); addComboBoxEntry(modeBox, gui.getAllGrids().get(i));
@@ -325,7 +320,7 @@ public class MenuWindow {
startButton.setContentAreaFilled(false); startButton.setContentAreaFilled(false);
startButton.setBounds(50, 310, 330, 50); startButton.setBounds(50, 310, 330, 50);
startButton.setVisible(true); startButton.setVisible(true);
startButton.addMouseListener(hover); startButton.addMouseListener(new Hover());
startButton.setBorder(new RoundedBorder(25)); startButton.setBorder(new RoundedBorder(25));
startButton.addActionListener(new ActionListener() { startButton.addActionListener(new ActionListener() {
@Override @Override
+6 -23
View File
@@ -1,11 +1,13 @@
package main; package main;
import data.database.DataBase;
import game.Control; import game.Control;
public class Main { public class Main {
//TODO: Database Seve Button. Alert when Name is already is taken. //TODO: Database Seve Button.
//TODO: Show grid name.
//TODO: Database window to Delete Entries. //TODO: Database window to Delete Entries.
//TODO: Save Velocity in DB //TODO: Save Velocity in DB
//TODO: Correct generation saving in DB and read out it //TODO: Correct generation saving in DB and read out it
@@ -14,8 +16,10 @@ public class Main {
//TODO: Correct new Initialisation when game starts //TODO: Correct new Initialisation when game starts
//TODO: Change the population Rules //TODO: Change the population Rules
//TODO: Limit input length //TODO: Limit input length
//TODO: DB Reload when Menu windows Opened
//TODO: Implement Corona Virus. Infected, Vaccinated, Recovered and Normal //TODO: Implement Corona Virus. Infected, Vaccinated, Recovered and Normal
//TODO: Implement Picture to grid function
//TODO: Implement a war mode. //TODO: Implement a war mode.
@@ -27,28 +31,7 @@ public class Main {
// DataBase dataBase = new DataBase(); // DataBase dataBase = new DataBase();
// dataBase.deleteTables(); // dataBase.deleteTables();
// dataBase.createTables(); // dataBase.createTables();
//
//
// boolean[][] cells = new boolean[44][66];
//
// for (int x = 0; x < 6; x++) {
// for (int y = 0; y < 6; y++) {
// }
// }
//
//
// for (int x = 0; x < 6; x++) {
// for (int y = 0; y < 6; y++) {
// cells[x][y] = true;
// }
// }
//
// dataBase.saveGrid(cells, "Noah", 1012);
//
// System.out.println(dataBase.getAllGrids());
//
// boolean[][] x = dataBase.getGrid("penis");
//
} }