Java Game of Life benutzt SwingJava

Java-Forum
Anonymous
 Java Game of Life benutzt Swing

Post by Anonymous »

Ich versuche, mit Swing eine Nachbildung des Lebensspiels zu erstellen. Ich gebe zu, ich habe Code von ein anderer von 1 verwendet, da ich versuche, meinen Kopf darum zu machen und dann mit meiner eigenen Implementierung fortzufahren. Ich habe ein gewisses Verständnis für ihren Code, aber ich wollte 2 zusätzliche Funktionen in ihren Code implementieren. Ich stelle jedoch fest, dass die Art und Weise, wie es geschrieben wird, Probleme aufweist, da ich einen Mouselistener hinzufügen wollte (um eine Zelle zum Leben zu erwecken, wenn ich klickte) und Fensterlistener (um einen Start zu machen, innehalten und wieder aufnehmen). < /p>

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.Transient;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class ConwaysGameOfLife extends JPanel implements MouseListener{

private int[][] cellsGrid; // grid is the size of the 2d array
private static final Random rnd = new Random(); // make a new random generator
private int generationCounter; // counter for the generation

public ConwaysGameOfLife(int width, int height) {
this.cellsGrid = new int[width / 4][height / 4];// divides by 4 whatever the width and height set is
setupGrid();
}// new method for creating the game with input sizes for the size of the game window

/*The grid consists fully of cells, the grid size is divided by 4 to make the cells
* setupGrid makes the grid of cells
*
* */
private void setupGrid() {
for (int[] row : cellsGrid) {
for (int j = 0; j < row.length; j++) {
if (rnd.nextDouble() < 0.92)
continue;
row[j] = rnd.nextInt(2);
//
}
}
}
/*
* applies the rule to the existing cells changing their state depending on the position to neighbors set in the rules
* */
public void updateGrid() {
for (int i = 0; i < cellsGrid.length; i++) {
for (int j = 0; j < cellsGrid.length; j++) {
applyRule(i, j);
}
}
}

// Rules of game of life cells iterations
private void applyRule(int i, int j) {
int left = 0, right = 0, up = 0, down = 0;
int dUpperLeft = 0, dUpperRight = 0, dLowerLeft = 0, dLowerRight = 0;
//this shows the 8 possible neighbors in terms of position

if (j < cellsGrid.length - 1) {
right = cellsGrid[j + 1];
if(i>0)
dUpperRight = cellsGrid[j + 1];
if (i < cellsGrid.length - 1)
dLowerRight = cellsGrid[i + 1][j + 1];
}

if (j > 0) {
left = cellsGrid[j - 1];
if (i > 0)
dUpperLeft = cellsGrid[j - 1];
if (i< cellsGrid.length-1)
dLowerLeft = cellsGrid[i + 1][j - 1];
}

if (i > 0)
up = cellsGrid[j];
if (i < cellsGrid.length - 1)
down = cellsGrid[i + 1][j];

int sum = left + right + up + down + dUpperLeft + dUpperRight
+ dLowerLeft
+ dLowerRight;

if (cellsGrid[j] == 1) {
if (sum < 2)
cellsGrid[j] = 0;
if (sum > 3)
cellsGrid[j] = 0;
}

else {
if (sum == 3)
cellsGrid[j] = 1;
}

}

@Override
@Transient
public Dimension getPreferredSize() {
return new Dimension(cellsGrid.length * 4, cellsGrid[0].length * 4);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color gColor = g.getColor();

g.drawString("Generation: " + generationCounter++, 0, 10);
for (int i = 0; i < cellsGrid.length; i++) {
for (int j = 0; j < cellsGrid[i].length; j++) {
if (cellsGrid[i][j] == 1) {
g.setColor(Color.black); // change colour
g.fillRect(j * 8, i * 8, 8, 8); // change size of cells
}
}
}

g.setColor(gColor);
//paint the cells to a colour
}

public static void main(String[] args) {
final ConwaysGameOfLife c = new ConwaysGameOfLife(800, 800);
JFrame frame = new JFrame();
frame.getContentPane().add(c);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);

JButton start=new JButton("START");
/* This method specifies the location and size
* of button. In method setBounds(x, y, width, height)
* x,y) are cordinates from the top left
* corner and remaining two arguments are the width
* and height of the button.
*/
start.setBounds(80,0,80,20);

//Adding button onto the frame
//frame.add(start);

new Timer(100, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
frame.add(start);
c.updateGrid();
c.repaint();
}
}).start();
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}
}
< /code>

Zunächst flackern die Taste und erscheint nur, wenn sie mit der Maus schwebt und immer noch flackern. Dann möchte ich, dass die Iterationen nach dem Start -Taste beginnen und die Taste pausieren, um sie zu pausieren und wieder aufzunehmen. Dies. Wenn ich von der Maus geklickt habe, aber Fehler erhalte, was an meinem mangelnden Verständnis der Implementierung von CellsGrid zurückzuführen ist. Danke :)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post