Structure Update mit Nicolai
This commit is contained in:
Binary file not shown.
+195219
-1505
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,60 @@
|
|||||||
|
package database;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
public abstract class AbstractDAO {
|
||||||
|
|
||||||
|
|
||||||
|
private Connection connection;
|
||||||
|
private Statement statement;
|
||||||
|
|
||||||
|
protected void simpleStatement(String sqlStatement) {
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
try {
|
||||||
|
statement.executeUpdate(sqlStatement);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ResultSet resultSetStatement(String sqlStatement) {
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
try {
|
||||||
|
return statement.executeQuery(sqlStatement);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect();
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void connect() {
|
||||||
|
try {
|
||||||
|
connection = DriverManager.getConnection("jdbc:h2:./lib/Conway-Game-Of-Live", "sa", "");
|
||||||
|
statement = connection.createStatement();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void disconnect() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
statement.close();
|
||||||
|
connection.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
package database;
|
|
||||||
|
|
||||||
import java.sql.*;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class DataBase {
|
|
||||||
|
|
||||||
private Connection connection;
|
|
||||||
private Statement statement;
|
|
||||||
|
|
||||||
private void connect(){
|
|
||||||
try {
|
|
||||||
connection = DriverManager.getConnection("jdbc:h2:./lib/Conway-Game-Of-Live", "sa", "");
|
|
||||||
statement = connection.createStatement();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void disconnect(){
|
|
||||||
|
|
||||||
try {
|
|
||||||
statement.close();
|
|
||||||
connection.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void simpleSQL(String sqlStatement){
|
|
||||||
try {
|
|
||||||
statement.executeUpdate(sqlStatement);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResultSet resultSetSQL(String sqlStatement){
|
|
||||||
try {
|
|
||||||
return statement.executeQuery(sqlStatement);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void createTables(){
|
|
||||||
connect();
|
|
||||||
|
|
||||||
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)");
|
|
||||||
|
|
||||||
disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteTables(){
|
|
||||||
connect();
|
|
||||||
|
|
||||||
simpleSQL("drop table gameInfo");
|
|
||||||
simpleSQL("drop table gameData");
|
|
||||||
|
|
||||||
disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getAllGrids(){
|
|
||||||
|
|
||||||
List<String> allGrids = new ArrayList<>();
|
|
||||||
|
|
||||||
connect();
|
|
||||||
|
|
||||||
ResultSet resultSet = resultSetSQL("select name from gameInfo");
|
|
||||||
|
|
||||||
while (true){
|
|
||||||
try {
|
|
||||||
if (!resultSet.next()) break;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
allGrids.add(resultSet.getString(1));
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
disconnect();
|
|
||||||
|
|
||||||
return allGrids;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveGrid(boolean[][] grid, String gridName, int generation, int cellCount){
|
|
||||||
|
|
||||||
connect();
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
resultSet.next();
|
|
||||||
int id = resultSet.getInt("id");
|
|
||||||
|
|
||||||
for (int x = 0; x < grid.length; x++) {
|
|
||||||
for (int y = 0; y < grid[0].length; y++) {
|
|
||||||
if (grid[x][y]){
|
|
||||||
simpleSQL("insert into gameData (id, x, y) values ("+id+", "+x+", "+y+")");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
disconnect();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean[][] getGrid(String gridName){
|
|
||||||
|
|
||||||
connect();
|
|
||||||
|
|
||||||
ResultSet resultInfo = resultSetSQL("select * from gameInfo where name = '"+gridName+"'");
|
|
||||||
|
|
||||||
try {
|
|
||||||
resultInfo.next();
|
|
||||||
int id = resultInfo.getInt("id");
|
|
||||||
int width = resultInfo.getInt("width");
|
|
||||||
int height = resultInfo.getInt("height");
|
|
||||||
|
|
||||||
resultInfo.close();
|
|
||||||
|
|
||||||
boolean[][] cells = new boolean[width][height];
|
|
||||||
|
|
||||||
ResultSet resultData = resultSetSQL("select * from gameData where id ="+id);
|
|
||||||
|
|
||||||
while (resultData.next()){
|
|
||||||
cells[resultData.getInt("x")][resultData.getInt("y")] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return cells;
|
|
||||||
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
disconnect();
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getGen(String gridName){
|
|
||||||
connect();
|
|
||||||
ResultSet resultSet = resultSetSQL("select generation from gameInfo where name = '"+gridName+"'");
|
|
||||||
try {
|
|
||||||
resultSet.next();
|
|
||||||
int gen = resultSet.getInt("generation");
|
|
||||||
disconnect();
|
|
||||||
return gen;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
disconnect();
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package database;
|
||||||
|
|
||||||
|
import org.h2.tools.DeleteDbFiles;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ReadDAO extends AbstractDAO {
|
||||||
|
|
||||||
|
public List<String> getAllGridNames(){
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
|
||||||
|
ResultSet resultSet = resultSetStatement("select name from gameInfo");
|
||||||
|
|
||||||
|
while (true){
|
||||||
|
try {
|
||||||
|
if (!resultSet.next()){
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.add(resultSet.getString(1));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean[][] getGrid(String gridName){
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
ResultSet resultInfo = resultSetStatement("select * from gameInfo where name = '"+gridName+"'");
|
||||||
|
|
||||||
|
try {
|
||||||
|
resultInfo.next();
|
||||||
|
int id = resultInfo.getInt("id");
|
||||||
|
int width = resultInfo.getInt("width");
|
||||||
|
int height = resultInfo.getInt("height");
|
||||||
|
|
||||||
|
resultInfo.close();
|
||||||
|
|
||||||
|
boolean[][] cells = new boolean[width][height];
|
||||||
|
|
||||||
|
ResultSet resultData = resultSetStatement("select * from gameData where id ="+id);
|
||||||
|
|
||||||
|
while (resultData.next()){
|
||||||
|
cells[resultData.getInt("x")][resultData.getInt("y")] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect();
|
||||||
|
|
||||||
|
return cells;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect();
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getGen(String gridName){
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
ResultSet resultSet = resultSetStatement("select generation from gameInfo where name = '"+gridName+"'");
|
||||||
|
try {
|
||||||
|
|
||||||
|
resultSet.next();
|
||||||
|
int gen = resultSet.getInt("generation");
|
||||||
|
|
||||||
|
return gen;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCellCount(String gridName){
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
ResultSet resultSet = resultSetStatement("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){
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
List<String> allNames = getAllGridNames();
|
||||||
|
|
||||||
|
if (allNames.contains(name)){
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package database;
|
||||||
|
|
||||||
|
import org.h2.tools.DeleteDbFiles;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class WriteDAO extends AbstractDAO{
|
||||||
|
|
||||||
|
public void createTables(){
|
||||||
|
connect();
|
||||||
|
simpleStatement("create table IF NOT EXISTS gameInfo(id int not null auto_increment, name varchar, width int, height int, generation int, cellcount int)");
|
||||||
|
simpleStatement("create table IF NOT EXISTS gameData(id int, x int, y int)");
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteTable(String table){
|
||||||
|
connect();
|
||||||
|
simpleStatement("drop table "+table);
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteDatabase(String dbDir, String dbName, boolean quiet){
|
||||||
|
DeleteDbFiles.execute(dbDir, dbName, quiet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveGrid(boolean[][] grid, String gridName, int generation, int cellCount){
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
simpleStatement("insert into GAMEINFO(NAME, WIDTH, HEIGHT, GENERATION, CELLCOUNT) values ('"+gridName+"', "+grid.length+", "+grid[0].length+", "+generation+","+cellCount+")");
|
||||||
|
ResultSet resultSet = resultSetStatement("select * from gameInfo order by id desc limit 1");
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
resultSet.next();
|
||||||
|
int id = resultSet.getInt("id");
|
||||||
|
|
||||||
|
for (int x = 0; x < grid.length; x++) {
|
||||||
|
for (int y = 0; y < grid[0].length; y++) {
|
||||||
|
if (grid[x][y]){
|
||||||
|
simpleStatement("insert into gameData (id, x, y) values ("+id+", "+x+", "+y+")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package gamecontrol;
|
package gamecontrol;
|
||||||
|
|
||||||
import database.DataBase;
|
import database.ReadDAO;
|
||||||
|
import database.WriteDAO;
|
||||||
import gui.control.GUI;
|
import gui.control.GUI;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -10,7 +11,8 @@ public class Control {
|
|||||||
|
|
||||||
private GUI gui;
|
private GUI gui;
|
||||||
private Clock clock;
|
private Clock clock;
|
||||||
private DataBase dataBase;
|
private ReadDAO readDAO;
|
||||||
|
private WriteDAO writeDAO;
|
||||||
|
|
||||||
private int xSize, ySize, cellCount, gen;
|
private int xSize, ySize, cellCount, gen;
|
||||||
|
|
||||||
@@ -29,7 +31,8 @@ public class Control {
|
|||||||
|
|
||||||
clock = new Clock(this, gui);
|
clock = new Clock(this, gui);
|
||||||
gui = new GUI(this);
|
gui = new GUI(this);
|
||||||
dataBase = new DataBase();
|
readDAO = new ReadDAO();
|
||||||
|
writeDAO = new WriteDAO();
|
||||||
gui.createMenuWindow();
|
gui.createMenuWindow();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -98,8 +101,8 @@ public class Control {
|
|||||||
|
|
||||||
private void getFromDatabase(String name){
|
private void getFromDatabase(String name){
|
||||||
|
|
||||||
cells = dataBase.getGrid(name);
|
cells = readDAO.getGrid(name);
|
||||||
gen = dataBase.getGen(name);
|
gen = readDAO.getGen(name);
|
||||||
this.xSize = cells.length;
|
this.xSize = cells.length;
|
||||||
this.ySize = cells[0].length;
|
this.ySize = cells[0].length;
|
||||||
this.cellCount = this.xSize * this.ySize;
|
this.cellCount = this.xSize * this.ySize;
|
||||||
@@ -383,15 +386,15 @@ public class Control {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getAllGrids(){
|
public List<String> getAllGrids(){
|
||||||
return dataBase.getAllGrids();
|
return readDAO.getAllGridNames();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveGridInDB(String gridName){
|
public void saveGridInDB(String gridName){
|
||||||
dataBase.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 dataBase.checkName(name);
|
return readDAO.checkName(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class ControlWindow {
|
|||||||
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/data/icons/Icon.png");
|
ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png");
|
||||||
controlWindow.setIconImage(imageIcon.getImage());
|
controlWindow.setIconImage(imageIcon.getImage());
|
||||||
|
|
||||||
controlWindow.addWindowListener(new WindowAdapter() {
|
controlWindow.addWindowListener(new WindowAdapter() {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class GameWindow {
|
|||||||
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);
|
||||||
|
|
||||||
ImageIcon imageIcon = new ImageIcon("src/data/icons/Icon.png");
|
ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png");
|
||||||
gameWindow.setIconImage(imageIcon.getImage());
|
gameWindow.setIconImage(imageIcon.getImage());
|
||||||
|
|
||||||
gameWindow.addWindowListener(new WindowAdapter() {
|
gameWindow.addWindowListener(new WindowAdapter() {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ public class MenuWindow {
|
|||||||
|
|
||||||
menuWindow.setTitle(title);
|
menuWindow.setTitle(title);
|
||||||
|
|
||||||
ImageIcon imageIcon = new ImageIcon("src/data/icons/Icon.png");
|
ImageIcon imageIcon = new ImageIcon("src/assets/icons/Icon.png");
|
||||||
menuWindow.setIconImage(imageIcon.getImage());
|
menuWindow.setIconImage(imageIcon.getImage());
|
||||||
|
|
||||||
|
|
||||||
@@ -339,7 +339,7 @@ public class MenuWindow {
|
|||||||
|
|
||||||
|
|
||||||
private void initIMG(){
|
private void initIMG(){
|
||||||
preview = new ImageIcon("src/data/pictures/Preview.png");
|
preview = new ImageIcon("src/assets/pictures/Preview.png");
|
||||||
previewLabel = new JLabel(preview);
|
previewLabel = new JLabel(preview);
|
||||||
previewLabel.setBounds(450, 45, 203, 302);
|
previewLabel.setBounds(450, 45, 203, 302);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -28,12 +28,10 @@ public class Main {
|
|||||||
|
|
||||||
Control control = new Control();
|
Control control = new Control();
|
||||||
control.start();
|
control.start();
|
||||||
// DataBase dataBase = new DataBase();
|
// ReadDAO dataBase = new ReadDAO();
|
||||||
// dataBase.deleteTables();
|
// dataBase.deleteTable("gameData");
|
||||||
// dataBase.createTables();
|
// dataBase.createTables();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user