8 package org.bitstorm.gameoflife;
10 import java.awt.Canvas;
11 import java.awt.Color;
12 import java.awt.Dimension;
13 import java.awt.Graphics;
14 import java.awt.Image;
15 import java.awt.event.ComponentEvent;
16 import java.awt.event.ComponentListener;
17 import java.awt.event.MouseAdapter;
18 import java.awt.event.MouseEvent;
19 import java.awt.event.MouseMotionAdapter;
20 import java.util.Enumeration;
21 import java.util.Vector;
55 setBackground(
new Color(0x999999));
59 public void mouseReleased(MouseEvent e) {
60 draw(e.getX(), e.getY());
62 public void mousePressed(MouseEvent e) {
67 addMouseMotionListener(
new MouseMotionAdapter() {
68 public void mouseDragged(MouseEvent e) {
69 draw(e.getX(), e.getY());
73 new ComponentListener() {
74 public void componentResized(ComponentEvent e) {
78 public void componentMoved(ComponentEvent e) {}
79 public void componentHidden(ComponentEvent e) {}
80 public void componentShown(ComponentEvent e) {}
101 if ( newCellSize != 0 ) {
105 Dimension canvasDim = this.size();
106 offScreenImage = null;
107 offScreenImageDrawed = null;
108 cellGrid.
resize(canvasDim.width/cellSize, canvasDim.height/cellSize);
109 if ( newShape != null ) {
127 cellUnderMouse = cellGrid.
getCell(x / cellSize, y / cellSize);
128 }
catch (java.lang.ArrayIndexOutOfBoundsException e) {
139 public void draw(
int x,
int y) {
141 cellGrid.
setCell(x / cellSize, y / cellSize, !cellUnderMouse );
143 }
catch (java.lang.ArrayIndexOutOfBoundsException e) {
153 Dimension d = getSize();
154 if ((offScreenImage == null)) {
155 offScreenImage = createImage(d.width, d.height);
156 offScreenGraphics = offScreenImage.getGraphics();
158 paint(offScreenGraphics);
159 g.drawImage(offScreenImage, 0, 0, null);
168 if (offScreenImageDrawed == null) {
170 Dimension d = getSize();
171 offScreenImageDrawed = createImage(d.width, d.height);
172 offScreenGraphicsDrawed = offScreenImageDrawed.getGraphics();
174 offScreenGraphicsDrawed.setColor(getBackground());
175 offScreenGraphicsDrawed.fillRect(0, 0, d.width, d.height);
176 offScreenGraphicsDrawed.setColor(Color.gray);
177 offScreenGraphicsDrawed.fillRect(0, 0, cellSize * dim.width - 1, cellSize * dim.height - 1);
178 offScreenGraphicsDrawed.setColor(getBackground());
179 for (
int x = 1; x < dim.width; x++) {
180 offScreenGraphicsDrawed.drawLine(x * cellSize - 1, 0, x * cellSize - 1, cellSize * dim.height - 1);
182 for (
int y = 1; y < dim.height; y++) {
183 offScreenGraphicsDrawed.drawLine( 0, y * cellSize - 1, cellSize * dim.width - 1, y * cellSize - 1);
186 g.drawImage(offScreenImageDrawed, 0, 0, null);
188 g.setColor(Color.yellow);
189 Enumeration
enum = cellGrid.
getEnum();
191 while (
enum.hasMoreElements() ) {
192 c = (
Cell)
enum.nextElement();
193 g.fillRect(c.
col * cellSize, c.
row * cellSize, cellSize - 1, cellSize - 1);
203 return new Dimension( cellSize * dim.width, cellSize * dim.height );
211 return new Dimension( cellSize, cellSize );
241 dimShape = shape.getDimension();
244 if (dimShape.width > dimGrid.width || dimShape.height > dimGrid.height)
245 throw new ShapeException(
"Shape doesn't fit on canvas (grid: "+dimGrid.width+
"x"+dimGrid.height+
", shape: "+dimShape.width+
"x"+dimShape.height+
")");
248 xOffset = (dimGrid.width - dimShape.width) / 2;
249 yOffset = (dimGrid.height - dimShape.height) / 2;
253 Enumeration cells = shape.getCells();
254 while (cells.hasMoreElements()) {
255 int[] cell = (
int[]) cells.nextElement();
256 cellGrid.
setCell(xOffset + cell[0], yOffset + cell[1],
true);
Dimension getMinimumSize()
void setCell(int col, int row, boolean cell)
synchronized void setShape(Shape shape)
Image offScreenImageDrawed
boolean getCell(int col, int row)
void setAfterWindowResize(Shape newShape, int newCellSize)
void saveCellUnderMouse(int x, int y)
Graphics offScreenGraphics
Dimension getPreferredSize()
Graphics offScreenGraphicsDrawed
void setCellSize(int cellSize)
CellGridCanvas(CellGrid cellGrid, int cellSize)
void resize(int col, int row)