Updatet the Control.java and ControlWindow.java and implemented new features.

This commit is contained in:
Noah
2021-11-30 18:41:57 +01:00
parent 359b153460
commit 2164e372b3
5 changed files with 180 additions and 21 deletions
+85 -11
View File
@@ -4,6 +4,8 @@ import gui.GUI;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import static java.lang.Math.round;
public class Control { public class Control {
private GUI gui; private GUI gui;
@@ -11,6 +13,7 @@ public class Control {
private int xSize, ySize, cellCount, gen; private int xSize, ySize, cellCount, gen;
private boolean[][] startingCells;
private boolean[][] cells; private boolean[][] cells;
private boolean[][] newcells; private boolean[][] newcells;
@@ -45,6 +48,7 @@ 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];
startingCells = new boolean[this.xSize][this.ySize];
switch (startMode) { switch (startMode) {
@@ -58,9 +62,9 @@ public class Control {
case Manuel -> { case Manuel -> {
buildManuelGame(); buildManuelGame();
} }
} }
saveFirstGrid();
calcSize(); calcSize();
@@ -84,6 +88,8 @@ public class Control {
} }
} }
syncCells();
} }
private void generateTask1() { private void generateTask1() {
@@ -94,16 +100,12 @@ public class Control {
} }
// cells[1][2] = true;
// cells[2][2] = true;
// cells[2][4] = true;
// cells[3][1] = true;
// cells[3][3] = true;
// cells[4][3] = true;
cells[1][0] = true;
cells[1][1] = true;
cells[1][2] = true; 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(); syncCells();
@@ -131,6 +133,22 @@ public class Control {
} }
} }
public void saveFirstGrid(){
for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) {
startingCells[x][y] = cells[x][y];
}
}
}
private void resetCells(){
for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) {
cells[x][y] = startingCells[x][y];
}
}
}
public boolean[][] getCells() { public boolean[][] getCells() {
return cells; return cells;
} }
@@ -138,7 +156,6 @@ public class Control {
public void nextGen() { public void nextGen() {
gen++; gen++;
System.out.println("Generation:" + gen);
for (int x = 0; x < xSize; x++) { for (int x = 0; x < xSize; x++) {
for (int y = 0; y < ySize; y++) { for (int y = 0; y < ySize; y++) {
@@ -166,6 +183,45 @@ public class Control {
gui.updateGameWindowTitle(gen); gui.updateGameWindowTitle(gen);
} }
public void nextGenLight() {
for (int x = 0; x < xSize; x++) {
for (int y = 0; y < ySize; y++) {
int n = aliveNeigbours(x, y);
switch (n) {
case 3:
newcells[x][y] = true;
break;
case 2:
break;
default:
newcells[x][y] = false;
break;
}
}
}
for (int x = 0; x < this.xSize; x++) {
for (int y = 0; y < this.ySize; y++) {
cells[x][y] = newcells[x][y];
}
}
}
public void previousGen(){
if(gen > 0) {
resetCells();
syncCells();
for (int i = 0; i < gen - 1; i++) {
nextGenLight();
}
gen--;
gui.updateGameWindowTitle(gen);
}
}
// public void checkN(Boolean[][] arr, Boolean[][] futureGen, int columns, int rows) { // public void checkN(Boolean[][] arr, Boolean[][] futureGen, int columns, int rows) {
// ArrayList<Thread> threads = new ArrayList<>(); // ArrayList<Thread> threads = new ArrayList<>();
// final int NUM_OF_THREADS = 8; //Or can be passed as an argument // final int NUM_OF_THREADS = 8; //Or can be passed as an argument
@@ -360,6 +416,24 @@ public class Control {
public int getGeneration(){ public int getGeneration(){
return gen; return gen;
} }
public void jump2Gen(int wantedtGen){
if (wantedtGen < gen){
resetCells();
syncCells();
for (int i = 0; i < wantedtGen; i++) {
nextGenLight();
}
gen = wantedtGen;
gui.updateGameWindowTitle(gen);
} else {
multipleGenSkip(wantedtGen - gen);
}
}
} }
+80 -7
View File
@@ -20,10 +20,13 @@ public class ControlWindow {
private JLabel heading; private JLabel heading;
private JButton toggleGame; private JButton toggleGame;
private JButton nextGenButton; private JButton nextGenButton;
private JButton previousGenButton;
private JButton lockMouse; private JButton lockMouse;
private JButton clearButton; private JButton clearButton;
private JButton multipleGenSkipButton; private JButton multipleGenSkipButton;
private JButton jump2GenButton;
private JTextField multipleGenSkipLable; private JTextField multipleGenSkipLable;
private JTextField jump2GenLable;
private int velocity, minVelocity = 1 , maxVelocity = 5000; private int velocity, minVelocity = 1 , maxVelocity = 5000;
@@ -52,7 +55,7 @@ public class ControlWindow {
controlWindow.setLocation(gameWindow.getPos().x - (width + offset), gameWindow.getPos().y); controlWindow.setLocation(gameWindow.getPos().x - (width + offset), gameWindow.getPos().y);
controlWindow.setLayout(null); controlWindow.setLayout(null);
ImageIcon imageIcon = new ImageIcon("src/files/PNG/Icon.png"); ImageIcon imageIcon = new ImageIcon("src/data/icons/Icon.png");
controlWindow.setIconImage(imageIcon.getImage()); controlWindow.setIconImage(imageIcon.getImage());
controlWindow.addWindowListener(new WindowAdapter() { controlWindow.addWindowListener(new WindowAdapter() {
@@ -79,9 +82,11 @@ public class ControlWindow {
initVelocity(); initVelocity();
initToggleButton(); initToggleButton();
initNextButton(); initNextButton();
initPreviousGenButton();
initDrawLockButton(); initDrawLockButton();
initClearButton(); initClearButton();
initMultibleGenSkip(); initMultibleGenSkip();
initJump2Gen();
} }
private void initHeading(){ private void initHeading(){
@@ -176,6 +181,9 @@ public class ControlWindow {
toggleGame.addActionListener(new ActionListener() { toggleGame.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (gui.getGeneration() == 0){
gui.saveFirstGrid();
}
gui.setRunning(!gui.isRunning()); gui.setRunning(!gui.isRunning());
updateToggleButton(); updateToggleButton();
gui.setVelocity(velocity); gui.setVelocity(velocity);
@@ -187,25 +195,49 @@ public class ControlWindow {
private void initNextButton(){ private void initNextButton(){
nextGenButton = new JButton(); nextGenButton = new JButton();
nextGenButton.setText("Next Generation"); nextGenButton.setText("Next Gen.");
nextGenButton.setFont(text); nextGenButton.setFont(text);
nextGenButton.setForeground(Color.decode("#121212")); nextGenButton.setForeground(Color.decode("#121212"));
nextGenButton.setBorderPainted(true); nextGenButton.setBorderPainted(true);
nextGenButton.setFocusPainted(false); nextGenButton.setFocusPainted(false);
nextGenButton.setContentAreaFilled(false); nextGenButton.setContentAreaFilled(false);
nextGenButton.setBounds(50, 675, 300, 50); nextGenButton.setBounds(225, 675, 125, 50);
nextGenButton.setVisible(true); nextGenButton.setVisible(true);
nextGenButton.addMouseListener(hover); nextGenButton.addMouseListener(hover);
nextGenButton.setBorder(new RoundedBorder(25)); nextGenButton.setBorder(new RoundedBorder(25));
nextGenButton.addActionListener(new ActionListener() { nextGenButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (gui.getGeneration() == 0){
gui.saveFirstGrid();
}
gui.nextGen(); gui.nextGen();
gui.showGrid(gui.getCells()); gui.showGrid(gui.getCells());
} }
}); });
} }
private void initPreviousGenButton(){
previousGenButton = new JButton();
previousGenButton.setText("Prior Gen.");
previousGenButton.setFont(text);
previousGenButton.setForeground(Color.decode("#121212"));
previousGenButton.setBorderPainted(true);
previousGenButton.setFocusPainted(false);
previousGenButton.setContentAreaFilled(false);
previousGenButton.setBounds(50, 675, 125, 50);
previousGenButton.setVisible(true);
previousGenButton.addMouseListener(hover);
previousGenButton.setBorder(new RoundedBorder(25));
previousGenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gui.previousGen();
gui.showGrid(gui.getCells());
}
});
}
private void initDrawLockButton(){ private void initDrawLockButton(){
lockMouse = new JButton(); lockMouse = new JButton();
lockMouse.setFont(text); lockMouse.setFont(text);
@@ -213,7 +245,7 @@ public class ControlWindow {
lockMouse.setBorderPainted(true); lockMouse.setBorderPainted(true);
lockMouse.setFocusPainted(false); lockMouse.setFocusPainted(false);
lockMouse.setContentAreaFilled(false); lockMouse.setContentAreaFilled(false);
lockMouse.setBounds(50, 600, 300, 50); lockMouse.setBounds(50, 450, 300, 50);
lockMouse.setVisible(true); lockMouse.setVisible(true);
lockMouse.addMouseListener(hover); lockMouse.addMouseListener(hover);
lockMouse.setBorder(new RoundedBorder(25)); lockMouse.setBorder(new RoundedBorder(25));
@@ -235,7 +267,7 @@ public class ControlWindow {
clearButton.setBorderPainted(true); clearButton.setBorderPainted(true);
clearButton.setFocusPainted(false); clearButton.setFocusPainted(false);
clearButton.setContentAreaFilled(false); clearButton.setContentAreaFilled(false);
clearButton.setBounds(50, 525, 300, 50); clearButton.setBounds(50, 375, 300, 50);
clearButton.setVisible(true); clearButton.setVisible(true);
clearButton.addMouseListener(hover); clearButton.addMouseListener(hover);
clearButton.setBorder(new RoundedBorder(25)); clearButton.setBorder(new RoundedBorder(25));
@@ -257,7 +289,7 @@ public class ControlWindow {
multipleGenSkipButton.setBorderPainted(true); multipleGenSkipButton.setBorderPainted(true);
multipleGenSkipButton.setFocusPainted(false); multipleGenSkipButton.setFocusPainted(false);
multipleGenSkipButton.setContentAreaFilled(false); multipleGenSkipButton.setContentAreaFilled(false);
multipleGenSkipButton.setBounds(50, 450, 300, 50); multipleGenSkipButton.setBounds(50, 600, 300, 50);
multipleGenSkipButton.setVisible(true); multipleGenSkipButton.setVisible(true);
multipleGenSkipButton.addMouseListener(hover); multipleGenSkipButton.addMouseListener(hover);
multipleGenSkipButton.setBorder(new RoundedBorder(25)); multipleGenSkipButton.setBorder(new RoundedBorder(25));
@@ -273,7 +305,7 @@ public class ControlWindow {
multipleGenSkipLable.setOpaque(false); multipleGenSkipLable.setOpaque(false);
multipleGenSkipLable.setBackground(null); multipleGenSkipLable.setBackground(null);
multipleGenSkipLable.setBorder(null); multipleGenSkipLable.setBorder(null);
multipleGenSkipLable.setBounds(160, 450, 50, 50); multipleGenSkipLable.setBounds(160, 600, 50, 50);
multipleGenSkipLable.setFont(text); multipleGenSkipLable.setFont(text);
multipleGenSkipLable.setForeground(Color.decode("#121212")); multipleGenSkipLable.setForeground(Color.decode("#121212"));
multipleGenSkipLable.setVisible(true); multipleGenSkipLable.setVisible(true);
@@ -286,6 +318,44 @@ public class ControlWindow {
}); });
} }
private void initJump2Gen(){
jump2GenButton = new JButton();
jump2GenButton.setText("Jump to generation");
jump2GenButton.setFont(text);
jump2GenButton.setForeground(Color.decode("#121212"));
jump2GenButton.setBorderPainted(true);
jump2GenButton.setFocusPainted(false);
jump2GenButton.setContentAreaFilled(false);
jump2GenButton.setBounds(50, 525, 300, 50);
jump2GenButton.setVisible(true);
jump2GenButton.addMouseListener(hover);
jump2GenButton.setBorder(new RoundedBorder(25));
jump2GenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gui.jump2Gen(Integer.parseInt(jump2GenLable.getText()));
gui.showGrid(gui.getCells());
}
});
jump2GenLable = new JTextField("6");
jump2GenLable.setOpaque(false);
jump2GenLable.setBackground(null);
jump2GenLable.setBorder(null);
jump2GenLable.setBounds(275, 525, 50, 50);
jump2GenLable.setFont(text);
jump2GenLable.setForeground(Color.decode("#121212"));
jump2GenLable.setVisible(true);
jump2GenLable.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
if (!Character.isDigit(evt.getKeyChar())) {
evt.consume();
}
}
});
}
private void initSaveButton(){} private void initSaveButton(){}
private void initSaveNameField(){} private void initSaveNameField(){}
@@ -297,10 +367,13 @@ public class ControlWindow {
controlWindow.add(velocitySliderLabel); controlWindow.add(velocitySliderLabel);
controlWindow.add(toggleGame); controlWindow.add(toggleGame);
controlWindow.add(nextGenButton); controlWindow.add(nextGenButton);
controlWindow.add(previousGenButton);
controlWindow.add(lockMouse); controlWindow.add(lockMouse);
controlWindow.add(clearButton); controlWindow.add(clearButton);
controlWindow.add(multipleGenSkipLable); controlWindow.add(multipleGenSkipLable);
controlWindow.add(jump2GenLable);
controlWindow.add(multipleGenSkipButton); controlWindow.add(multipleGenSkipButton);
controlWindow.add(jump2GenButton);
} }
public void setVisibility(boolean value) { public void setVisibility(boolean value) {
+12
View File
@@ -155,6 +155,18 @@ public class GUI {
return control.getVelocity(); return control.getVelocity();
} }
public void saveFirstGrid(){
control.saveFirstGrid();
}
public void previousGen(){
control.previousGen();
}
public void jump2Gen(int wantedtGen){
control.jump2Gen(wantedtGen);
}
//TODO: Delet this Methods. Those are shit. //TODO: Delet this Methods. Those are shit.
public void setVisibility(boolean value){ public void setVisibility(boolean value){
controlWindow.setVisibility(value); controlWindow.setVisibility(value);
+1 -1
View File
@@ -32,7 +32,7 @@ public class GameWindow {
gameWindow.getContentPane().setPreferredSize(new Dimension(width + (2 * offset), height + (2 * offset))); gameWindow.getContentPane().setPreferredSize(new Dimension(width + (2 * offset), height + (2 * offset)));
gameWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); gameWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
ImageIcon imageIcon = new ImageIcon("src/files/PNG/Icon.png"); ImageIcon imageIcon = new ImageIcon("src/data/icons/Icon.png");
gameWindow.setIconImage(imageIcon.getImage()); gameWindow.setIconImage(imageIcon.getImage());
gameWindow.addWindowListener(new WindowAdapter() { gameWindow.addWindowListener(new WindowAdapter() {
+2 -2
View File
@@ -57,7 +57,7 @@ public class MenuWindow {
menuWindow.setTitle(title); menuWindow.setTitle(title);
ImageIcon imageIcon = new ImageIcon("src/files/PNG/Icon.png"); ImageIcon imageIcon = new ImageIcon("src/data/icons/Icon.png");
menuWindow.setIconImage(imageIcon.getImage()); menuWindow.setIconImage(imageIcon.getImage());
@@ -362,7 +362,7 @@ public class MenuWindow {
} }
private void initIMG(){ private void initIMG(){
preview = new ImageIcon("src/files/PNG/Preview.png"); preview = new ImageIcon("src/data/pictures/Preview.png");
previewLabel = new JLabel(preview); previewLabel = new JLabel(preview);
previewLabel.setBounds(450, 45, 203, 302); previewLabel.setBounds(450, 45, 203, 302);
} }