简体   繁体   中英

Java Game of Life using Swing

I am trying to make a replica of the Game of Life using swing, I admit i have used code from some 1 else as i am trying to get my head around it and then proceed with my own implementation. I have some understanding of their code, yet i wanted to implements 2 additional features to their code. However i am finding that the way it is written is posing problems as i wanted to add a MouseListener(To make a cell come to life when clicked) and WindowListener(To make a start,pause and resume button).

I do understand how they work to some extent, yet i need your help to get my headaround it.

Here is the code:

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[i].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[i][j + 1];
            if(i>0)
                dUpperRight = cellsGrid[i - 1][j + 1];
            if (i < cellsGrid.length - 1)
                dLowerRight = cellsGrid[i + 1][j + 1];
        }

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

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

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

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

        else {
            if (sum == 3)
                cellsGrid[i][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

    }
}

First of all, The button flickers, and only appears when hovered over with the mouse, still flickering. Then i want the iterations to begin after start button is pressed, and pause button to pause it, and resume(Have a decent idea how it would work, but not how to implement it with the structure of swing that is done in this code.)

Secondly, I wanted the cells to come to life when they are pressed with the mouse, But, i am unsure how to implement the mouseListener to do this.

I tried something like cellsGrid[i][j] = 1; when clicked by mouse but i get errors, which is due to my lack of understanding of the implementation of cellsGrid.

I am not expecting solutions to the problem, I would like some guidance to understand the Listeners better and maybe how to make this simpler to understand for me. Thank You :)

Your simulation has a model that is a grid of cells; it has a view that paints an 8 x 8 square to represent a cell in the grid. As suggested here , you can map model and view coordinates using linear interpolation . In particular, given the following proportions, you can cross-multiply and solve for the missing coordinate.

view.x : panelWidthInPixels :: model.x : modelXRange
view.y : panelHeightInPixels :: model.y : modelYRange

For reference, this complete example maps mouse coordinates to pixel coordinates in an image. A complete example of John Conway's Game of Life in Java Swing is cited here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM