School Work
This commit is contained in:
Generated
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
+68
-10
@@ -7,20 +7,28 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||||||
public class Control {
|
public class Control {
|
||||||
|
|
||||||
private GUI gui;
|
private GUI gui;
|
||||||
|
private Clock clock;
|
||||||
|
|
||||||
private int xSize, ySize, cellCount, gen = 0;
|
private int xSize, ySize, cellCount, gen = 0;
|
||||||
|
|
||||||
private boolean[][] cells;
|
private boolean[][] cells;
|
||||||
private boolean[][] newcells;
|
private boolean[][] newcells;
|
||||||
|
|
||||||
public Control(){
|
|
||||||
|
|
||||||
|
public void start(){
|
||||||
|
|
||||||
gui = new GUI(this);
|
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.xSize = xSize;
|
||||||
this.ySize = ySize;
|
this.ySize = ySize;
|
||||||
this.cellCount = cellCount;
|
this.cellCount = cellCount;
|
||||||
@@ -28,10 +36,64 @@ public class Control {
|
|||||||
cells = new boolean[this.xSize][this.ySize];
|
cells = new boolean[this.xSize][this.ySize];
|
||||||
newcells = 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 ++;
|
gen ++;
|
||||||
System.out.println("Generation:" + gen);
|
System.out.println("Generation:" + gen);
|
||||||
|
|
||||||
@@ -58,11 +120,7 @@ public class Control {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int x = 0; x < xSize; x++) {
|
syncCells();
|
||||||
for (int y = 0; y < ySize; y++) {
|
|
||||||
cells[x][y] = newcells[x][y];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
public enum StartMode {
|
||||||
|
|
||||||
|
Task1, Randomized, Manuel
|
||||||
|
|
||||||
|
}
|
||||||
+29
-7
@@ -1,6 +1,7 @@
|
|||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import game.Control;
|
import game.Control;
|
||||||
|
import game.StartMode;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.event.ChangeEvent;
|
import javax.swing.event.ChangeEvent;
|
||||||
@@ -29,6 +30,9 @@ public class GUI {
|
|||||||
private JLabel startCellsName;
|
private JLabel startCellsName;
|
||||||
private JLabel startCellsLabel;
|
private JLabel startCellsLabel;
|
||||||
|
|
||||||
|
private JLabel modeName;
|
||||||
|
private JComboBox<String> modeBox;
|
||||||
|
|
||||||
private MouseListener hover;
|
private MouseListener hover;
|
||||||
|
|
||||||
private JButton randomStart;
|
private JButton randomStart;
|
||||||
@@ -44,7 +48,7 @@ public class GUI {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buildControlWindow() {
|
public void controlWindow() {
|
||||||
jf = new JFrame();
|
jf = new JFrame();
|
||||||
|
|
||||||
jf.setTitle("Conway's Game of Life");
|
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<String>();
|
||||||
|
modeBox.setBounds(180, 270, 100, 20);
|
||||||
|
addComboBoxEntry(modeBox, "CTTask1");
|
||||||
|
addComboBoxEntry(modeBox, "Randomized");
|
||||||
|
|
||||||
randomStart = new JButton();
|
randomStart = new JButton();
|
||||||
randomStart.setText("Random Start");
|
randomStart.setText("Start Game");
|
||||||
randomStart.setFont(text);
|
randomStart.setFont(text);
|
||||||
randomStart.setForeground(Color.decode("#121212"));
|
randomStart.setForeground(Color.decode("#121212"));
|
||||||
randomStart.setBorderPainted(true);
|
randomStart.setBorderPainted(true);
|
||||||
randomStart.setFocusPainted(false);
|
randomStart.setFocusPainted(false);
|
||||||
randomStart.setContentAreaFilled(false);
|
randomStart.setContentAreaFilled(false);
|
||||||
randomStart.setBounds(50, 280, 150, 50);
|
randomStart.setBounds(50, 320, 150, 50);
|
||||||
randomStart.setVisible(true);
|
randomStart.setVisible(true);
|
||||||
randomStart.addMouseListener(hover);
|
randomStart.addMouseListener(hover);
|
||||||
randomStart.setBorder(new RoundedBorder(25));
|
randomStart.setBorder(new RoundedBorder(25));
|
||||||
randomStart.addActionListener(new ActionListener() {
|
randomStart.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
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.setBorderPainted(true);
|
||||||
manuelStart.setFocusPainted(false);
|
manuelStart.setFocusPainted(false);
|
||||||
manuelStart.setContentAreaFilled(false);
|
manuelStart.setContentAreaFilled(false);
|
||||||
manuelStart.setBounds(225, 280, 150, 50);
|
manuelStart.setBounds(225, 320, 150, 50);
|
||||||
manuelStart.setVisible(true);
|
manuelStart.setVisible(true);
|
||||||
manuelStart.addMouseListener(hover);
|
manuelStart.addMouseListener(hover);
|
||||||
manuelStart.setBorder(new RoundedBorder(25));
|
manuelStart.setBorder(new RoundedBorder(25));
|
||||||
manuelStart.addActionListener(new ActionListener() {
|
manuelStart.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
control.buildGameWindow(xAxisSize, yAxisSize, cellCount, StartMode.Manuel);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -265,10 +281,16 @@ public class GUI {
|
|||||||
jf.add(randomStart);
|
jf.add(randomStart);
|
||||||
jf.add(manuelStart);
|
jf.add(manuelStart);
|
||||||
jf.add(previewLabel);
|
jf.add(previewLabel);
|
||||||
|
jf.add(modeName);
|
||||||
|
jf.add(modeBox);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buildGameWindow() {
|
private void addComboBoxEntry(JComboBox<String> comboBox, String entry){
|
||||||
|
comboBox.addItem(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void gameWindow() {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -7,6 +7,7 @@ public class Main {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Control control = new Control();
|
Control control = new Control();
|
||||||
|
control.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user