/* * GameOfLife.java * * Created on June 2, 2003, 7:02 AM */ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.Color; import java.awt.Dimension; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author dickey */ public class GOLDisplay3 extends JPanel implements MouseListener { private int cellWidth = 32; private int cellHeight = 28; private int cellsAcross; private int cellsDown; private GameOfLife3 game; public GOLDisplay3(int across, int down, GameOfLife3 aGame) { super(); this.cellsAcross = across; this.cellsDown = down; this.game = aGame; JFrame frame = new JFrame("Game of Life"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); int totalWidth = cellWidth*cellsAcross; int totalHeight = cellHeight*cellsDown; frame.setSize(totalWidth + 60, totalHeight + 70); this.setPreferredSize(new Dimension(totalWidth+15, totalHeight+15)); this.setBackground(Color.cyan); Container container = frame.getContentPane(); container.setBackground(Color.red); container.setLayout(new java.awt.FlowLayout()); container.add(this); frame.show(); this.addMouseListener(this); } private final Color ALIVE_COLOR = Color.green; private final Color DEAD_COLOR = Color.yellow; private final int margin = 4; public void paintComponent(java.awt.Graphics origG) { super.paintComponent(origG); java.awt.Graphics2D g = (java.awt.Graphics2D) origG; for (int r = 0; r < cellsDown; r++) { for (int c = 0; c < cellsAcross; c++) { java.awt.Shape cellOutline = new java.awt.Rectangle( c*cellWidth+margin, r*cellHeight+margin, cellWidth, cellHeight); g.setColor(Color.black); g.draw(cellOutline); java.awt.Shape cellShape = new java.awt.Rectangle( c*cellWidth+margin+1, r*cellHeight+ margin+1, cellWidth-1, cellHeight-1); if (game.cellStatus(r,c) == GameOfLife.STATUS_ALIVE) { g.setColor(ALIVE_COLOR); } else { g.setColor(DEAD_COLOR); } g.fill(cellShape); } } } public void mousePressed(MouseEvent e) { int col = (e.getX() - margin)/cellWidth; int row = (e.getY() - margin)/cellHeight; game.toggleCell(row, col); repaint(); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} //end class }