I manage to get it to generate the cells, but unfortunately only one cell generates and moves left diagonally and every other generation it activates cells below and to the right of it
package GameofLife; import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent;//need it for the mouse when clicking on the grid import java.awt.event.MouseListener; import java.awt.event.ActionListener;//the buttons react import java.awt.event.ActionEvent; public class GameofLife implements MouseListener, ActionListener, Runnable{ final int WIDTH = 75; //every calculation in the grid will rely on these variable names. final int HEIGHT = 75; JFrame frame = new JFrame("Give me an A, please.");//window heading boolean[][] grid = new boolean[WIDTH][HEIGHT]; CustomPanel panel = new CustomPanel(grid); //buttons to be pressed to perform certain functions. //take the hint please. JButton next = new JButton("Next, give me a passing grade"); JButton start = new JButton("Start, give 90%+"); JButton stop = new JButton("Stop, don't give anything lower than 90%"); Container north = new Container();//holds the buttons boolean running = false;//used for runnable, run public GameofLife(){ frame.setSize(600,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//has the X button frame.setVisible(true);//window is visible frame.setLayout(new BorderLayout()); frame.add(panel,BorderLayout.CENTER);//grid frame.add(north,BorderLayout.NORTH);//button layout panel.addMouseListener(this); next.addActionListener(this); start.addActionListener(this); stop.addActionListener(this); north.setLayout(new GridLayout(1,3));//row and column north.add(next);//button north.add(start);//button north.add(stop); } public static void main(String[] args){ new GameofLife(); } public void mouseClicked(MouseEvent ev){ } public void mouseEntered(MouseEvent ev){ } public void mouseExited(MouseEvent ev){ } public void mousePressed(MouseEvent ev){ } public void mouseReleased(MouseEvent ev)throws ArrayIndexOutOfBoundsException{//activates the cell(turns it black) System.out.println(ev.getX() + "," + ev.getY()); //most aggrivating code, sorry but the only way it works if you click the cell you want to activate in a special way. //this section is a little off T.T(yes that is a crying pain staking emoticon). final short row = (short)(Math.min(ev.getY() /7,75)); final short col = (short)(Math.min(ev.getX() /7,75)); grid[col][row] = (!grid[col][row]); System.out.println(col+","+row+","+panel.getHeight()+","+panel.getWidth()); frame.repaint(); try{//catches out of bounds exception grid[col][row] = grid[col][row]; }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Too Far"); } } public void actionPerformed(ActionEvent ev){ if(ev.getSource().equals(next) == true){ next(); frame.repaint(); } if(ev.getSource().equals(start) == true){ Thread thread = new Thread(this); running = true; thread.start(); } if(ev.getSource().equals(stop) == true){ running = false; } } public void run(){ while(running == true){ next(); try{ Thread.sleep(1000); }catch(Exception ex){//catches all if any exceptions with the run time ex.printStackTrace(); } } } public void next(){ boolean[][] newGrid = new boolean[grid.length][grid.length]; int neighbor = 0; //checks the neighbors. for(int i = 0; i < grid.length; i++){ for(int j = 0; j < grid.length; j++){ if(j > grid.length -1 && grid[i][j-1] == true){//checks bottom neighbor = neighbor + 1; } if(j < grid.length -1 && grid[i][j+1] == true){//checks top neighbor = neighbor + 1; } if(i > grid.length-1 && grid[i-1][j] == true){//checks left neighbor = neighbor + 1; } if(i < grid.length - 1 && grid[i +1][j] == true){//checks right neighbor = neighbor + 1; } if(j < grid.length-1 && i < grid.length-1 && grid[i+1][j+1] == true){//checks top right neighbor = neighbor + 1; } if(j > grid.length-1 && i > grid.length-1 && grid[i-1][j-1] == true){//checks bottom left neighbor = neighbor + 1; } if(j > grid.length-1 && i < grid.length-1 && grid[i+1][j-1] == true){//checks top left neighbor = neighbor + 1; } if(j < grid.length-1 && i > grid.length-1 && grid[i-1][j+1] == true){//checks bottom right neighbor = neighbor + 1; } if(grid[i][j] == true){//if alive and neighbors are two or three, alive, else false. if(neighbor == 2 || neighbor == 3){ newGrid[i][j] = true; } else{//dead cell if(neighbor < 2 || neighbor > 3){ newGrid[i][j] = false; } } } else if(grid[i][j] == false){//dead to alive if(neighbor == 3){ newGrid[i][j] = true; } else{//still dead newGrid[i][j] = false; } } } } grid = newGrid; panel.setGrid(newGrid); frame.repaint(); } }
Here is the second class:
package GameofLife; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Color; public class CustomPanel extends JPanel{ boolean[][] grid; public CustomPanel(boolean[][] newGrid){ //connects to GameofLife grid = newGrid; } public void setGrid(boolean[][] newGrid){//for the changes on the grid grid = newGrid; } public void paintComponent(Graphics g){ super.paintComponent(g); //makes the math much simpler. double cellWidth = ((double)(532))/grid.length; double cellHeight = ((double)(532))/grid.length; //draws out the lines for(int i = 0; i < grid.length; i++){ for(int j = 0; j < grid.length; j++){ g.drawLine(0,(int)(i*cellHeight),532,(int)(i*cellHeight)); g.drawLine((int)(j*cellWidth),0,(int)(j*cellWidth),532); //if clicked on, color the cell that color. if(grid[i][j] == true){ g.setColor(Color.BLACK); g.fillRect((int)(i*cellWidth),(int)(j*cellHeight),(int)(cellWidth),(int)(cellHeight)); } } } } }