/* Judy Yang * CS210 * 4/5/11 * * The class extends JPanel and draws a grid of squares (each square * represents a cell) that represents a grid of cells that are either * alive or dead depending on the values held in the GameOfLife * class. This paintComponent method in this class gets the values of * each cell from the GameOfLife class and colors a cell rose-pink if * it is dead and navy blue if it is alive. This class also keeps * track of where the mouse is clicked so the user can choose to * change whether a cell is alive or dead by clicking on it. */ import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.BorderFactory; import javax.swing.JPanel; public class CellGrid extends JPanel implements MouseListener { // RGB values of the color of a dead cell private static final int C1R = 190; private static final int C1G = 131; private static final int C1B = 143; // RGB values of the color of a live cell private static final int C2R = 36; private static final int C2G = 42; private static final int C2B = 127; // the size of the grid private int gridSize; // the Game of Life rendered in this JPanel private GameOfLife gol; // the location of the user's mouse click private int xClick = 0; private int yClick = 0; // creates a JPanel, sets the background to white, gets the // size of the game's grid from the GameOfLife gol public CellGrid(GameOfLife gol) { // creates a JPanel by calling the constructor of the superclass super(); //sets gol to the gol sent in as a parameter this.gol = gol; // gets the size of the GameOfLife grid gridSize = gol.getSize(); // sets the background of the JPanel to white setBackground(Color.white); // adds this JPanel as a mouse listener addMouseListener(this); } // draws a grid of rectangles that for each cell from the // GameOfLife and colors them pink for dead and blue for live public void paintComponent(Graphics g){ // colors the background of the JPanel super.paintComponent(g); // the length and width of each square cell int length = 15; for (int i = 0; i < gridSize; i++) { for (int j = 0; j < gridSize; j++){ // if the mouse has not been clicked, color // the grid according to the current values // held in gol's gameGrid if (xClick == 0 && yClick == 0){ if (gol.isLive(j, i)) g.setColor(new Color(C2R, C2G, C2B)); else g.setColor(new Color(C1R, C1G, C1B)); } // if the mouse has been clicked, find where // the mouse has been clicked and change the // cell to live if dead before, or to dead if // live before and then color the cell // accordingly if (xClick > i*length && xClick < (i+1)*length && yClick > j*length && yClick < (j+1)*length) { gol.change(j, i); if (gol.isLive(j, i)) g.setColor(new Color(C2R, C2G, C2B)); else g.setColor(new Color(C1R, C1G, C1B)); } // if the mouse has been clicked, color the // rest of the grid accordingly else { if (gol.isLive(j, i)) g.setColor(new Color(C2R, C2G, C2B)); else g.setColor(new Color(C1R, C1G, C1B)); } // make a filled rectangle at the specified // location g.fillRect(i*length, j*length, length, length); // make the outlines of the rectangles and // color them black g.setColor(Color.black); g.drawRect(i*length, j*length, length, length); } } // reset where the mouse was clicked back to zero xClick = yClick = 0; } // gets the x and y values of where the mouse is clicked when the // mouse is clicked public void mouseClicked(MouseEvent arg0) { // gets the x and y values of the location where the mouse is // clicked and sets xClick and yClick to the values // respectively and calls repaint xClick = arg0.getX(); yClick = arg0.getY(); repaint(); } public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mousePressed(MouseEvent arg0) {} public void mouseReleased(MouseEvent arg0) {} }