V2.0
This commit is contained in:
parent
7539cea980
commit
bfe5e45af0
|
|
@ -1,12 +1,12 @@
|
|||
import windowInterface.MyInterface;
|
||||
|
||||
|
||||
import windowInterface.WelcomeScreen;
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
new WelcomeScreen().setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
package backend;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Color;
|
||||
|
||||
public class GameOfLifeSimulation {
|
||||
|
||||
private boolean[][] currentGeneration;
|
||||
private int cellSize;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public GameOfLifeSimulation(int cellSize, int width, int height) {
|
||||
this.cellSize = cellSize;
|
||||
this.width = width / cellSize;
|
||||
this.height = height / cellSize;
|
||||
currentGeneration = new boolean[this.width][this.height];
|
||||
|
||||
for (int i = 0; i < this.width; i++) {
|
||||
for (int j = 0; j < this.height; j++) {
|
||||
currentGeneration[i][j] = Math.random() < 0.2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
boolean[][] newGeneration = new boolean[width][height];
|
||||
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
int aliveNeighbours = 0;
|
||||
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
int neighbourX = x + dx;
|
||||
int neighbourY = y + dy;
|
||||
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
|
||||
if (neighbourX < 0) neighbourX = width - 1;
|
||||
if (neighbourY < 0) neighbourY = height - 1;
|
||||
if (neighbourX >= width) neighbourX = 0;
|
||||
if (neighbourY >= height) neighbourY = 0;
|
||||
|
||||
aliveNeighbours += currentGeneration[neighbourX][neighbourY] ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentGeneration[x][y]) {
|
||||
newGeneration[x][y] = aliveNeighbours >= 2 && aliveNeighbours <= 3;
|
||||
} else {
|
||||
newGeneration[x][y] = aliveNeighbours == 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentGeneration = newGeneration;
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
g.setColor(currentGeneration[x][y] ? Color.WHITE : Color.BLACK);
|
||||
g.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -10,14 +10,14 @@ import java.util.ArrayList;
|
|||
|
||||
public class Simulator extends Thread {
|
||||
|
||||
private MyInterface mjf;
|
||||
private boolean stopFlag;
|
||||
private boolean pauseFlag;
|
||||
private int loopDelay;
|
||||
private int[][] world;
|
||||
private boolean loopingBorder;
|
||||
private int width;
|
||||
private int height;
|
||||
public MyInterface mjf;
|
||||
public boolean stopFlag;
|
||||
public boolean pauseFlag;
|
||||
public int loopDelay;
|
||||
public int[][] world;
|
||||
public boolean loopingBorder;
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
|
||||
public Simulator(MyInterface mjfParam) {
|
||||
|
|
@ -206,7 +206,7 @@ public class Simulator extends Thread {
|
|||
|
||||
|
||||
|
||||
private int countNeighbors(int x, int y) {
|
||||
public int countNeighbors(int x, int y) {
|
||||
int count = 0;
|
||||
|
||||
// Loop through the 8 neighboring cells
|
||||
|
|
|
|||
|
|
@ -1,92 +1,93 @@
|
|||
package windowInterface;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import backend.Simulator;
|
||||
|
||||
public class JPanelDraw extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Simulator mySimu;
|
||||
private MyInterface interfaceGlobal;
|
||||
|
||||
public JPanelDraw(MyInterface itf) {
|
||||
super();
|
||||
mySimu = null;
|
||||
interfaceGlobal = itf;
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent me) {
|
||||
if (mySimu == null) {
|
||||
interfaceGlobal.instantiateSimu();
|
||||
}
|
||||
int x = (me.getX() * mySimu.getWidth()) / getWidth();
|
||||
int y = (me.getY() * mySimu.getHeight()) / getHeight();
|
||||
if (me.getButton() == MouseEvent.BUTTON1) {
|
||||
mySimu.toggleCell(x, y);
|
||||
} else if (me.getButton() == MouseEvent.BUTTON3) {
|
||||
mySimu.setCell(x, y, 2); // Set cell as population2
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setSimu(Simulator simu) {
|
||||
mySimu = simu;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
this.setBackground(Color.black);
|
||||
if (mySimu != null) {
|
||||
// Draw Interface from state of simulator
|
||||
float cellWidth = (float) this.getWidth() / (float) mySimu.getWidth();
|
||||
float cellHeight = (float) this.getHeight() / (float) mySimu.getHeight();
|
||||
g.setColor(Color.gray);
|
||||
for (int x = 0; x < mySimu.getWidth(); x++) {
|
||||
int graphX = Math.round(x * cellWidth);
|
||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||
}
|
||||
for (int y = 0; y < mySimu.getHeight(); y++) {
|
||||
int graphY = Math.round(y * cellHeight);
|
||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||
}
|
||||
for (int x = 0; x < mySimu.getWidth(); x++) {
|
||||
for (int y = 0; y < mySimu.getHeight(); y++) {
|
||||
int cellContent = mySimu.getCell(x, y);
|
||||
if (cellContent == 0) {
|
||||
continue;
|
||||
}
|
||||
if (interfaceGlobal.getGameMode() == 0) { // Two Populations mode
|
||||
g.setColor(Color.WHITE);
|
||||
} else if (interfaceGlobal.getGameMode() == 1) {
|
||||
if (cellContent == 1) {
|
||||
g.setColor(Color.RED);
|
||||
} else if (cellContent == 2) {
|
||||
g.setColor(Color.BLUE);
|
||||
} else if (cellContent == 3) {
|
||||
g.setColor(Color.yellow);
|
||||
} else if (cellContent == 4) {
|
||||
g.setColor(Color.GREEN);
|
||||
} else {
|
||||
g.setColor(Color.GRAY);
|
||||
}
|
||||
} else {
|
||||
g.setColor(Color.WHITE);
|
||||
}
|
||||
g.fillRect((int) Math.round(x * cellWidth), (int) Math.round(y * cellHeight),
|
||||
(int) Math.round(cellWidth), (int) Math.round(cellHeight));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package windowInterface;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import backend.Simulator;
|
||||
|
||||
public class JPanelDraw extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Simulator mySimu;
|
||||
private MyInterface interfaceGlobal;
|
||||
|
||||
public JPanelDraw(MyInterface itf) {
|
||||
super();
|
||||
mySimu = null;
|
||||
interfaceGlobal = itf;
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent me) {
|
||||
if (mySimu == null) {
|
||||
interfaceGlobal.instantiateSimu();
|
||||
}
|
||||
int x = (me.getX() * mySimu.getWidth()) / getWidth();
|
||||
int y = (me.getY() * mySimu.getHeight()) / getHeight();
|
||||
if (me.getButton() == MouseEvent.BUTTON1) {
|
||||
mySimu.toggleCell(x, y);
|
||||
} else if (me.getButton() == MouseEvent.BUTTON3) {
|
||||
mySimu.setCell(x, y, 2); // Set cell as population2
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setSimu(Simulator simu) {
|
||||
mySimu = simu;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
this.setBackground(Color.black);
|
||||
if (mySimu != null) {
|
||||
|
||||
// Draw Interface from state of simulator
|
||||
float cellWidth = (float) this.getWidth() / (float) mySimu.getWidth();
|
||||
float cellHeight = (float) this.getHeight() / (float) mySimu.getHeight();
|
||||
g.setColor(Color.gray);
|
||||
for (int x = 0; x < mySimu.getWidth(); x++) {
|
||||
int graphX = Math.round(x * cellWidth);
|
||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||
}
|
||||
for (int y = 0; y < mySimu.getHeight(); y++) {
|
||||
int graphY = Math.round(y * cellHeight);
|
||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||
}
|
||||
for (int x = 0; x < mySimu.getWidth(); x++) {
|
||||
for (int y = 0; y < mySimu.getHeight(); y++) {
|
||||
int cellContent = mySimu.getCell(x, y);
|
||||
if (cellContent == 0) {
|
||||
continue;
|
||||
}
|
||||
if (interfaceGlobal.getGameMode() == 0) { // Two Populations mode
|
||||
g.setColor(Color.WHITE);
|
||||
} else if (interfaceGlobal.getGameMode() == 1) {
|
||||
if (cellContent == 1) {
|
||||
g.setColor(Color.RED);
|
||||
} else if (cellContent == 2) {
|
||||
g.setColor(Color.BLUE);
|
||||
} else if (cellContent == 3) {
|
||||
g.setColor(Color.yellow);
|
||||
} else if (cellContent == 4) {
|
||||
g.setColor(Color.GREEN);
|
||||
} else {
|
||||
g.setColor(Color.GRAY);
|
||||
}
|
||||
} else {
|
||||
g.setColor(Color.WHITE);
|
||||
}
|
||||
g.fillRect((int) Math.round(x * cellWidth), (int) Math.round(y * cellHeight),
|
||||
(int) Math.round(cellWidth), (int) Math.round(cellHeight));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
package windowInterface;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
|
|
@ -38,6 +37,7 @@ import java.awt.*;
|
|||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
|
||||
import backend.Simulator;
|
||||
|
||||
|
||||
|
|
@ -53,6 +53,9 @@ public class MyInterface extends JFrame {
|
|||
private JSlider randSlider;
|
||||
private JSlider speedSlider;
|
||||
private int gameMode = 0;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Custom Retro Colors
|
||||
|
|
@ -243,10 +246,11 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
|
||||
public void switchGameMode() {
|
||||
gameMode = (gameMode + 1) % 3; // Switch between 0, 1, and 2
|
||||
gameMode = (gameMode + 1) % 3; // Switch between 0, 1, 2, and 3
|
||||
|
||||
mySimu.setGameMode(gameMode); // pass gameMode to the Simulator
|
||||
|
||||
|
||||
// Update labels or perform any other necessary operations based on the game mode
|
||||
switch (gameMode) {
|
||||
case 0:
|
||||
|
|
@ -258,12 +262,14 @@ public class MyInterface extends JFrame {
|
|||
case 2:
|
||||
setBorderBanner2("HighLife Mode");
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
// Repaint the drawing panel to reflect the new game mode
|
||||
panelDraw.repaint();
|
||||
}
|
||||
|
||||
|
||||
public void setBorderBanner(String s) {
|
||||
borderLabel.setText("Border: " + s);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
package windowInterface;import java.awt.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import backend.GameOfLifeSimulation;
|
||||
|
||||
public class WelcomeScreen extends JFrame {
|
||||
|
||||
private JButton startGameButton;
|
||||
private GameOfLifeSimulation simulation;
|
||||
private JPanel contentPanel;
|
||||
private JLabel title;
|
||||
|
||||
public WelcomeScreen() {
|
||||
setTitle("Game of Life Project");
|
||||
setSize(800, 600);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLayout(new BorderLayout());
|
||||
setLocationRelativeTo(null);
|
||||
setResizable(false);
|
||||
|
||||
simulation = new GameOfLifeSimulation(10, 800, 600);
|
||||
|
||||
title = new JLabel("Game of Life", SwingConstants.CENTER);
|
||||
title.setForeground(Color.YELLOW); // Choose your glowing color
|
||||
title.setFont(new Font("Serif", Font.BOLD, 50));
|
||||
title.setOpaque(false);
|
||||
|
||||
startGameButton = new JButton("Start Game");
|
||||
startGameButton.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
new MyInterface().setVisible(true);
|
||||
dispose();
|
||||
}
|
||||
});
|
||||
|
||||
contentPanel = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
simulation.draw(g);
|
||||
}
|
||||
};
|
||||
|
||||
contentPanel.setLayout(new GridBagLayout());
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.gridwidth = GridBagConstraints.REMAINDER;
|
||||
gbc.anchor = GridBagConstraints.NORTH;
|
||||
contentPanel.add(title, gbc);
|
||||
gbc.anchor = GridBagConstraints.CENTER;
|
||||
contentPanel.add(startGameButton, gbc);
|
||||
|
||||
this.add(contentPanel, BorderLayout.CENTER);
|
||||
|
||||
Timer timer = new Timer(100, new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
simulation.update();
|
||||
contentPanel.repaint();
|
||||
}
|
||||
});
|
||||
|
||||
timer.start();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue