Working Prototype
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
package draw;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class Draw extends JPanel {
|
||||||
|
private int xSize, ySize;
|
||||||
|
private float ratio;
|
||||||
|
|
||||||
|
private Graphics2D g2d;
|
||||||
|
private boolean[][] currentGrid;
|
||||||
|
|
||||||
|
private boolean drawLocked = false;
|
||||||
|
|
||||||
|
public Draw(int gridSizeX, int gridSizeY, float ratio) {
|
||||||
|
this.xSize = gridSizeX;
|
||||||
|
this.ySize = gridSizeY;
|
||||||
|
this.ratio = ratio;
|
||||||
|
currentGrid = new boolean[gridSizeX][gridSizeY];
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showGrid(boolean[][] grid) {
|
||||||
|
currentGrid = new boolean[xSize][ySize];
|
||||||
|
|
||||||
|
for (int x = 0; x < xSize; x++) {
|
||||||
|
for (int y = 0; y < ySize; y++) {
|
||||||
|
currentGrid[x][y] = grid[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
g2d = (Graphics2D) g;
|
||||||
|
|
||||||
|
float cellSizeX = 1024f / xSize;
|
||||||
|
float cellSizeY = 1024f / ySize;
|
||||||
|
|
||||||
|
if(ySize > xSize)
|
||||||
|
{
|
||||||
|
cellSizeX /= ratio;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cellSizeY *= ratio;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int x = 0; x < xSize; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < ySize; y++)
|
||||||
|
{
|
||||||
|
if (currentGrid[x][y])
|
||||||
|
{
|
||||||
|
g2d.fillRect((int) (x * cellSizeX), (int) (y * cellSizeY), (int) ((cellSizeX + 1)), (int) (cellSizeY + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-7
@@ -1,34 +1,39 @@
|
|||||||
package game;
|
package game;
|
||||||
|
|
||||||
|
import gui.GUI;
|
||||||
|
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class Clock extends Thread{
|
public class Clock extends Thread{
|
||||||
|
|
||||||
public static boolean running = true;
|
private boolean running = true;
|
||||||
|
|
||||||
public void start(Control control){
|
public void start(Control control, GUI gui){
|
||||||
|
|
||||||
Timer t = new Timer();
|
Timer t = new Timer();
|
||||||
TimerTask tt = new TimerTask() {
|
TimerTask tt = new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
if (running){
|
if (running){
|
||||||
control.nextGen();
|
boolean[][] booleans = control.nextGen();
|
||||||
|
gui.showGrid(booleans);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
t.scheduleAtFixedRate(tt, 1, 1000);
|
t.scheduleAtFixedRate(tt, 1, 120);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isRunning() {
|
public boolean isRunning() {
|
||||||
return running;
|
return running;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setRunning(boolean running) {
|
public void setRunning(boolean running) {
|
||||||
Clock.running = running;
|
this.running = running;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+42
-21
@@ -15,20 +15,19 @@ public class Control {
|
|||||||
private boolean[][] newcells;
|
private boolean[][] newcells;
|
||||||
|
|
||||||
|
|
||||||
|
public void start() {
|
||||||
public void start(){
|
|
||||||
|
|
||||||
gui = new GUI(this);
|
gui = new GUI(this);
|
||||||
gui.controlWindow();
|
gui.controlWindow();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startGame(){
|
public void startGame() {
|
||||||
clock = new Clock();
|
clock = new Clock();
|
||||||
clock.start(this);
|
clock.start(this, gui);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode){
|
public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) {
|
||||||
this.xSize = xSize;
|
this.xSize = xSize;
|
||||||
this.ySize = ySize;
|
this.ySize = ySize;
|
||||||
this.cellCount = cellCount;
|
this.cellCount = cellCount;
|
||||||
@@ -37,7 +36,7 @@ public class Control {
|
|||||||
newcells = new boolean[this.xSize][this.ySize];
|
newcells = new boolean[this.xSize][this.ySize];
|
||||||
|
|
||||||
|
|
||||||
switch(startMode){
|
switch (startMode) {
|
||||||
|
|
||||||
case Task1 -> {
|
case Task1 -> {
|
||||||
generateTask1();
|
generateTask1();
|
||||||
@@ -49,16 +48,23 @@ public class Control {
|
|||||||
buildManuelGame();
|
buildManuelGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
if (gui == null){
|
||||||
|
gui = new GUI(this);
|
||||||
|
}
|
||||||
|
|
||||||
private void buildManuelGame(){
|
gui.gameWindow(this.xSize, this.ySize);
|
||||||
|
|
||||||
|
startGame();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateTask1(){
|
private void buildManuelGame() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateTask1() {
|
||||||
for (int x = 0; x < 6; x++) {
|
for (int x = 0; x < 6; x++) {
|
||||||
for (int y = 0; y < 6; y++) {
|
for (int y = 0; y < 6; y++) {
|
||||||
cells[x][y] = false;
|
cells[x][y] = false;
|
||||||
@@ -75,24 +81,29 @@ public class Control {
|
|||||||
syncCells();
|
syncCells();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateRandomized(){
|
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);
|
||||||
this.cells[x][y] = true;
|
if (!cells[x][y]) {
|
||||||
|
this.cells[x][y] = true;
|
||||||
|
} else {
|
||||||
|
i--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
syncCells();
|
syncCells();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void syncCells(){
|
private void syncCells() {
|
||||||
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++) {
|
||||||
this.newcells[x][y] = this.cells[x][y];
|
newcells[x][y] = cells[x][y];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void nextGen(){
|
public boolean[][] nextGen() {
|
||||||
gen ++;
|
gen ++;
|
||||||
System.out.println("Generation:" + gen);
|
System.out.println("Generation:" + gen);
|
||||||
|
|
||||||
@@ -119,21 +130,28 @@ public class Control {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syncCells();
|
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;
|
||||||
|
|
||||||
int[] xoff = {1, 1, 0, -1, -1, -1, 0, 1};
|
int[] xoff = {1, 1, 0, -1, -1, -1, 0, 1};
|
||||||
int[] yoff = {0, 1, 1, 1, 0, -1, -1, -1};
|
int[] yoff = {0, 1, 1, 1, 0, -1, -1, -1};
|
||||||
|
|
||||||
for (int i = 0; i < 8; i++){
|
for (int i = 0; i < 8; i++) {
|
||||||
try {
|
try {
|
||||||
if (cells[x + xoff[i]][y + yoff[i]]){
|
if (cells[x + xoff[i]][y + yoff[i]]) {
|
||||||
count ++;
|
count++;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
@@ -142,11 +160,14 @@ public class Control {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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() {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ package game;
|
|||||||
|
|
||||||
public enum StartMode {
|
public enum StartMode {
|
||||||
|
|
||||||
Manuel, Task1, Randomized, Hallo
|
Manuel, Randomized, Task1,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+85
-30
@@ -1,5 +1,6 @@
|
|||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
|
import draw.Draw;
|
||||||
import game.Control;
|
import game.Control;
|
||||||
import game.StartMode;
|
import game.StartMode;
|
||||||
|
|
||||||
@@ -14,7 +15,14 @@ public class GUI {
|
|||||||
|
|
||||||
private final Control control;
|
private final Control control;
|
||||||
|
|
||||||
private JFrame jf;
|
/* Universial Atributes */
|
||||||
|
|
||||||
|
private final String title = "Conway's Game of Life";
|
||||||
|
|
||||||
|
/* End of Universial Atributes */
|
||||||
|
|
||||||
|
/* Components of the menue View */
|
||||||
|
private JFrame jfMenue;
|
||||||
|
|
||||||
private JLabel heading;
|
private JLabel heading;
|
||||||
private int xAxisSize = 6;
|
private int xAxisSize = 6;
|
||||||
@@ -41,6 +49,17 @@ public class GUI {
|
|||||||
private ImageIcon preview;
|
private ImageIcon preview;
|
||||||
private JLabel previewLabel;
|
private JLabel previewLabel;
|
||||||
|
|
||||||
|
/* End of control view Elements */
|
||||||
|
|
||||||
|
/* Game view Elements */
|
||||||
|
|
||||||
|
private JFrame jfGame;
|
||||||
|
private Draw draw;
|
||||||
|
|
||||||
|
private int xSize, ySize;
|
||||||
|
|
||||||
|
/* End of Game view Elements */
|
||||||
|
|
||||||
|
|
||||||
public GUI(Control control) {
|
public GUI(Control control) {
|
||||||
|
|
||||||
@@ -49,31 +68,31 @@ public class GUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void controlWindow() {
|
public void controlWindow() {
|
||||||
jf = new JFrame();
|
jfMenue = new JFrame();
|
||||||
|
|
||||||
jf.setTitle("Conway's Game of Life");
|
jfMenue.setTitle(title);
|
||||||
|
|
||||||
ImageIcon imageIcon = new ImageIcon("src/data/PNG/Icon.png");
|
ImageIcon imageIcon = new ImageIcon("src/data/PNG/Icon.png");
|
||||||
jf.setIconImage(imageIcon.getImage());
|
jfMenue.setIconImage(imageIcon.getImage());
|
||||||
|
|
||||||
//getContentPane().setBackground(Color.decode("#121212"));
|
//getContentPane().setBackground(Color.decode("#121212"));
|
||||||
|
|
||||||
jf.setUndecorated(false);
|
jfMenue.setUndecorated(false);
|
||||||
|
|
||||||
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
jfMenue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
jf.setAutoRequestFocus(false);
|
jfMenue.setAutoRequestFocus(false);
|
||||||
jf.setLayout(null);
|
jfMenue.setLayout(null);
|
||||||
jf.setSize(752, 424);
|
jfMenue.setSize(752, 424);
|
||||||
|
|
||||||
jf.setResizable(false);
|
jfMenue.setResizable(false);
|
||||||
|
|
||||||
|
|
||||||
initControlComponents();
|
initControlComponents();
|
||||||
addControlComponents();
|
addControlComponents();
|
||||||
|
|
||||||
|
|
||||||
jf.setLocationRelativeTo(null);
|
jfMenue.setLocationRelativeTo(null);
|
||||||
jf.setVisible(true);
|
jfMenue.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initControlComponents() {
|
private void initControlComponents() {
|
||||||
@@ -145,7 +164,7 @@ public class GUI {
|
|||||||
yAxisLabel.setFont(text);
|
yAxisLabel.setFont(text);
|
||||||
yAxisLabel.setForeground(Color.decode("#121212"));
|
yAxisLabel.setForeground(Color.decode("#121212"));
|
||||||
yAxisLabel.setVisible(true);
|
yAxisLabel.setVisible(true);
|
||||||
|
//TODO: Inputfield witch Changelistner to cange Slider
|
||||||
|
|
||||||
startCells = new JSlider(6, 6);
|
startCells = new JSlider(6, 6);
|
||||||
startCells.setBounds(180, 210, 200, 20);
|
startCells.setBounds(180, 210, 200, 20);
|
||||||
@@ -203,8 +222,9 @@ public class GUI {
|
|||||||
|
|
||||||
modeBox = new JComboBox<String>();
|
modeBox = new JComboBox<String>();
|
||||||
modeBox.setBounds(180, 257, 100, 20);
|
modeBox.setBounds(180, 257, 100, 20);
|
||||||
addComboBoxEntry(modeBox, "CTTask1");
|
|
||||||
addComboBoxEntry(modeBox, "Randomized");
|
addComboBoxEntry(modeBox, "Randomized");
|
||||||
|
addComboBoxEntry(modeBox, "CTTask1");
|
||||||
|
|
||||||
|
|
||||||
startButton = new JButton();
|
startButton = new JButton();
|
||||||
startButton.setText("Start Game");
|
startButton.setText("Start Game");
|
||||||
@@ -275,21 +295,21 @@ public class GUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addControlComponents() {
|
private void addControlComponents() {
|
||||||
jf.add(heading);
|
jfMenue.add(heading);
|
||||||
jf.add(xAxis);
|
jfMenue.add(xAxis);
|
||||||
jf.add(xAxisName);
|
jfMenue.add(xAxisName);
|
||||||
jf.add(xAxisLabel);
|
jfMenue.add(xAxisLabel);
|
||||||
jf.add(yAxis);
|
jfMenue.add(yAxis);
|
||||||
jf.add(yAxisName);
|
jfMenue.add(yAxisName);
|
||||||
jf.add(yAxisLabel);
|
jfMenue.add(yAxisLabel);
|
||||||
jf.add(startCells);
|
jfMenue.add(startCells);
|
||||||
jf.add(startCellsName);
|
jfMenue.add(startCellsName);
|
||||||
jf.add(startCellsLabel);
|
jfMenue.add(startCellsLabel);
|
||||||
jf.add(startButton);
|
jfMenue.add(startButton);
|
||||||
jf.add(manuelStart);
|
jfMenue.add(manuelStart);
|
||||||
jf.add(previewLabel);
|
jfMenue.add(previewLabel);
|
||||||
jf.add(modeName);
|
jfMenue.add(modeName);
|
||||||
jf.add(modeBox);
|
jfMenue.add(modeBox);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,9 +317,32 @@ public class GUI {
|
|||||||
comboBox.addItem(entry);
|
comboBox.addItem(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void gameWindow() {
|
public void gameWindow(int xSize, int ySize) {
|
||||||
|
this.xSize = xSize;
|
||||||
|
this.ySize = ySize;
|
||||||
|
|
||||||
|
|
||||||
|
jfGame = new JFrame(title);
|
||||||
|
|
||||||
|
jfGame.getContentPane().setPreferredSize(new Dimension(1600, 1600));
|
||||||
|
jfGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
jfGame.setResizable(false);
|
||||||
|
|
||||||
|
float ratio = (float)(ySize) / (float)(xSize);
|
||||||
|
draw = new Draw(this.xSize, this.ySize, ratio);
|
||||||
|
draw.setBounds(0, 0, 1200, 1200);
|
||||||
|
|
||||||
|
draw.setVisible(true);
|
||||||
|
|
||||||
|
|
||||||
|
jfGame.add(draw);
|
||||||
|
|
||||||
|
jfGame.pack();
|
||||||
|
jfGame.setLayout(null);
|
||||||
|
jfGame.setLocationRelativeTo(null);
|
||||||
|
jfGame.setVisible(true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initGameComponents() {
|
private void initGameComponents() {
|
||||||
@@ -308,5 +351,17 @@ public class GUI {
|
|||||||
private void addGameComponents() {
|
private void addGameComponents() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void showGrid(boolean[][] grid)
|
||||||
|
{
|
||||||
|
if(draw != null)
|
||||||
|
{
|
||||||
|
draw.showGrid(grid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.err.println("There is no Window");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user