Working Prototype

This commit is contained in:
Noah
2021-09-29 20:22:37 +02:00
parent c7fa4c8e91
commit d416fa834a
5 changed files with 204 additions and 59 deletions
+64
View File
@@ -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
View File
@@ -1,34 +1,39 @@
package game;
import gui.GUI;
import java.util.Timer;
import java.util.TimerTask;
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();
TimerTask tt = new TimerTask() {
@Override
public void run() {
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;
}
public static void setRunning(boolean running) {
Clock.running = running;
public void setRunning(boolean running) {
this.running = running;
}
}
+42 -21
View File
@@ -15,20 +15,19 @@ public class Control {
private boolean[][] newcells;
public void start(){
public void start() {
gui = new GUI(this);
gui.controlWindow();
}
public void startGame(){
public void startGame() {
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.ySize = ySize;
this.cellCount = cellCount;
@@ -37,7 +36,7 @@ public class Control {
newcells = new boolean[this.xSize][this.ySize];
switch(startMode){
switch (startMode) {
case Task1 -> {
generateTask1();
@@ -49,16 +48,23 @@ public class Control {
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 y = 0; y < 6; y++) {
cells[x][y] = false;
@@ -75,24 +81,29 @@ public class Control {
syncCells();
}
private void generateRandomized(){
private void generateRandomized() {
for (int i = 0; i < this.cellCount; i++) {
int x = rand(0, this.xSize);
int y = rand(0, this.ySize);
this.cells[x][y] = true;
if (!cells[x][y]) {
this.cells[x][y] = true;
} else {
i--;
}
}
syncCells();
}
private void syncCells(){
private void syncCells() {
for (int x = 0; x < this.xSize; x++) {
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 ++;
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[] xoff = {1, 1, 0, -1, -1, -1, 0, 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 {
if (cells[x + xoff[i]][y + yoff[i]]){
count ++;
if (cells[x + xoff[i]][y + yoff[i]]) {
count++;
}
} 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);
}
public void showGrid() {
}
}
+1 -1
View File
@@ -2,6 +2,6 @@ package game;
public enum StartMode {
Manuel, Task1, Randomized, Hallo
Manuel, Randomized, Task1,
}
+85 -30
View File
@@ -1,5 +1,6 @@
package gui;
import draw.Draw;
import game.Control;
import game.StartMode;
@@ -14,7 +15,14 @@ public class GUI {
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 int xAxisSize = 6;
@@ -41,6 +49,17 @@ public class GUI {
private ImageIcon preview;
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) {
@@ -49,31 +68,31 @@ public class GUI {
}
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");
jf.setIconImage(imageIcon.getImage());
jfMenue.setIconImage(imageIcon.getImage());
//getContentPane().setBackground(Color.decode("#121212"));
jf.setUndecorated(false);
jfMenue.setUndecorated(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setAutoRequestFocus(false);
jf.setLayout(null);
jf.setSize(752, 424);
jfMenue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfMenue.setAutoRequestFocus(false);
jfMenue.setLayout(null);
jfMenue.setSize(752, 424);
jf.setResizable(false);
jfMenue.setResizable(false);
initControlComponents();
addControlComponents();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jfMenue.setLocationRelativeTo(null);
jfMenue.setVisible(true);
}
private void initControlComponents() {
@@ -145,7 +164,7 @@ public class GUI {
yAxisLabel.setFont(text);
yAxisLabel.setForeground(Color.decode("#121212"));
yAxisLabel.setVisible(true);
//TODO: Inputfield witch Changelistner to cange Slider
startCells = new JSlider(6, 6);
startCells.setBounds(180, 210, 200, 20);
@@ -203,8 +222,9 @@ public class GUI {
modeBox = new JComboBox<String>();
modeBox.setBounds(180, 257, 100, 20);
addComboBoxEntry(modeBox, "CTTask1");
addComboBoxEntry(modeBox, "Randomized");
addComboBoxEntry(modeBox, "CTTask1");
startButton = new JButton();
startButton.setText("Start Game");
@@ -275,21 +295,21 @@ public class GUI {
}
private void addControlComponents() {
jf.add(heading);
jf.add(xAxis);
jf.add(xAxisName);
jf.add(xAxisLabel);
jf.add(yAxis);
jf.add(yAxisName);
jf.add(yAxisLabel);
jf.add(startCells);
jf.add(startCellsName);
jf.add(startCellsLabel);
jf.add(startButton);
jf.add(manuelStart);
jf.add(previewLabel);
jf.add(modeName);
jf.add(modeBox);
jfMenue.add(heading);
jfMenue.add(xAxis);
jfMenue.add(xAxisName);
jfMenue.add(xAxisLabel);
jfMenue.add(yAxis);
jfMenue.add(yAxisName);
jfMenue.add(yAxisLabel);
jfMenue.add(startCells);
jfMenue.add(startCellsName);
jfMenue.add(startCellsLabel);
jfMenue.add(startButton);
jfMenue.add(manuelStart);
jfMenue.add(previewLabel);
jfMenue.add(modeName);
jfMenue.add(modeBox);
}
@@ -297,9 +317,32 @@ public class GUI {
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() {
@@ -308,5 +351,17 @@ public class GUI {
private void addGameComponents() {
}
public void showGrid(boolean[][] grid)
{
if(draw != null)
{
draw.showGrid(grid);
}
else
{
System.err.println("There is no Window");
}
}
}