From e06f61f7eb71b08b1b579062e9ac87bb48acfe3e Mon Sep 17 00:00:00 2001 From: Noah Date: Wed, 29 Sep 2021 12:39:35 +0200 Subject: [PATCH] School Work --- .idea/misc.xml | 2 +- src/game/Clock.java | 35 ++++++++++++++++++ src/game/Control.java | 78 +++++++++++++++++++++++++++++++++++------ src/game/StartMode.java | 7 ++++ src/gui/GUI.java | 36 +++++++++++++++---- src/gui/GameGUI.java | 41 ---------------------- src/main/Main.java | 1 + 7 files changed, 141 insertions(+), 59 deletions(-) create mode 100644 src/game/Clock.java create mode 100644 src/game/StartMode.java delete mode 100644 src/gui/GameGUI.java diff --git a/.idea/misc.xml b/.idea/misc.xml index c3dfb30..1029788 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/game/Clock.java b/src/game/Clock.java new file mode 100644 index 0000000..cefa06f --- /dev/null +++ b/src/game/Clock.java @@ -0,0 +1,35 @@ +package game; + +import java.util.Timer; +import java.util.TimerTask; + +public class Clock extends Thread{ + + public static boolean running = true; + + public void start(Control control){ + + Timer t = new Timer(); + TimerTask tt = new TimerTask() { + @Override + public void run() { + if (running){ + control.nextGen(); + } + } + }; + + t.scheduleAtFixedRate(tt, 1, 1); + + } + + public static boolean isRunning() { + return running; + } + + public static void setRunning(boolean running) { + Clock.running = running; + } + +} + diff --git a/src/game/Control.java b/src/game/Control.java index c2acf19..a82b07a 100644 --- a/src/game/Control.java +++ b/src/game/Control.java @@ -7,20 +7,28 @@ import java.util.concurrent.ThreadLocalRandom; public class Control { private GUI gui; + private Clock clock; private int xSize, ySize, cellCount, gen = 0; private boolean[][] cells; private boolean[][] newcells; - public Control(){ + + + public void start(){ gui = new GUI(this); - gui.buildControlWindow(); + gui.controlWindow(); } - public void buildGameWindow(int xSize, int ySize, int cellCount){ + public void startGame(){ + clock = new Clock(); + clock.start(this); + } + + public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode){ this.xSize = xSize; this.ySize = ySize; this.cellCount = cellCount; @@ -28,10 +36,64 @@ public class Control { cells = new boolean[this.xSize][this.ySize]; newcells = new boolean[this.xSize][this.ySize]; - gui.buildGameWindow(); + + switch(startMode){ + + case Task1 -> { + generateTask1(); + System.out.println("Taks1"); + } + case Randomized -> { + generateRandomized(); + System.out.println("Random"); + } + case Manuel -> { + buildManuelGame(); + } + + } + } - private void nextGen(){ + private void buildManuelGame(){ + + } + + private void generateTask1(){ + for (int x = 0; x < 6; x++) { + for (int y = 0; y < 6; y++) { + cells[x][y] = false; + } + } + + cells[1][2] = true; + cells[2][2] = true; + cells[2][4] = true; + cells[3][1] = true; + cells[3][3] = true; + cells[4][3] = true; + + syncCells(); + } + + 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; + } + 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]; + } + } + } + + public void nextGen(){ gen ++; System.out.println("Generation:" + gen); @@ -58,11 +120,7 @@ public class Control { } } - for (int x = 0; x < xSize; x++) { - for (int y = 0; y < ySize; y++) { - cells[x][y] = newcells[x][y]; - } - } + syncCells(); } diff --git a/src/game/StartMode.java b/src/game/StartMode.java new file mode 100644 index 0000000..d566f94 --- /dev/null +++ b/src/game/StartMode.java @@ -0,0 +1,7 @@ +package game; + +public enum StartMode { + + Task1, Randomized, Manuel + +} diff --git a/src/gui/GUI.java b/src/gui/GUI.java index 8461184..348574d 100644 --- a/src/gui/GUI.java +++ b/src/gui/GUI.java @@ -1,6 +1,7 @@ package gui; import game.Control; +import game.StartMode; import javax.swing.*; import javax.swing.event.ChangeEvent; @@ -29,6 +30,9 @@ public class GUI { private JLabel startCellsName; private JLabel startCellsLabel; + private JLabel modeName; + private JComboBox modeBox; + private MouseListener hover; private JButton randomStart; @@ -44,7 +48,7 @@ public class GUI { } - public void buildControlWindow() { + public void controlWindow() { jf = new JFrame(); jf.setTitle("Conway's Game of Life"); @@ -190,21 +194,33 @@ public class GUI { } }; + modeName = new JLabel("Select Figure: "); + modeName.setBounds(50, 270, 200, 30); + modeName.setFont(text); + modeName.setForeground(Color.decode("#121212")); + modeName.setVisible(true); + + + modeBox = new JComboBox(); + modeBox.setBounds(180, 270, 100, 20); + addComboBoxEntry(modeBox, "CTTask1"); + addComboBoxEntry(modeBox, "Randomized"); + randomStart = new JButton(); - randomStart.setText("Random Start"); + randomStart.setText("Start Game"); randomStart.setFont(text); randomStart.setForeground(Color.decode("#121212")); randomStart.setBorderPainted(true); randomStart.setFocusPainted(false); randomStart.setContentAreaFilled(false); - randomStart.setBounds(50, 280, 150, 50); + randomStart.setBounds(50, 320, 150, 50); randomStart.setVisible(true); randomStart.addMouseListener(hover); randomStart.setBorder(new RoundedBorder(25)); randomStart.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - control.buildGameWindow(xAxisSize, yAxisSize, cellCount); + control.buildGameWindow(xAxisSize, yAxisSize, cellCount, StartMode.Randomized); } }); @@ -215,14 +231,14 @@ public class GUI { manuelStart.setBorderPainted(true); manuelStart.setFocusPainted(false); manuelStart.setContentAreaFilled(false); - manuelStart.setBounds(225, 280, 150, 50); + manuelStart.setBounds(225, 320, 150, 50); manuelStart.setVisible(true); manuelStart.addMouseListener(hover); manuelStart.setBorder(new RoundedBorder(25)); manuelStart.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - + control.buildGameWindow(xAxisSize, yAxisSize, cellCount, StartMode.Manuel); } }); @@ -265,10 +281,16 @@ public class GUI { jf.add(randomStart); jf.add(manuelStart); jf.add(previewLabel); + jf.add(modeName); + jf.add(modeBox); } - public void buildGameWindow() { + private void addComboBoxEntry(JComboBox comboBox, String entry){ + comboBox.addItem(entry); + } + + public void gameWindow() { } diff --git a/src/gui/GameGUI.java b/src/gui/GameGUI.java deleted file mode 100644 index 3975227..0000000 --- a/src/gui/GameGUI.java +++ /dev/null @@ -1,41 +0,0 @@ -package gui; - -import javax.swing.*; - -public class GameGUI { - - private JFrame jf; - - public GameGUI(){ - - jf = new JFrame(); - - jf.setTitle("CT-Conway's Game of Life"); - - ImageIcon imageIcon = new ImageIcon("src/data/icon.png"); - jf.setIconImage(imageIcon.getImage()); - - //getContentPane().setBackground(Color.decode("#121212")); - - jf.setUndecorated(false); - - jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - jf.setAutoRequestFocus(false); - jf.setLayout(null); - jf.setSize(752, 424); - //jf.getContentPane().setPreferredSize(new Dimension(752, 423)); - - jf.setResizable(false); - - - //initComponents(); - //addComponents(); - - - jf.setLocationRelativeTo(null); - jf.setVisible(true); - - - } - -} diff --git a/src/main/Main.java b/src/main/Main.java index 72c81d5..f9889c8 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -7,6 +7,7 @@ public class Main { public static void main(String[] args) { Control control = new Control(); + control.start(); }