forked from mrThe/Game-of-Life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCanvas.java
76 lines (62 loc) · 1.79 KB
/
GCanvas.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @author mr.The
* @skype mr-the
* @twitter @mr_The
*/
package javalife;
import java.awt.*;
/**
* Draw Canwas.
*/
class GCanvas extends Canvas {
private Color color = new Color(200, 200, 200);
JavaLife life;
int cell_size=10;
public void setCellSize(int cell_size) {
this.cell_size = cell_size;
}
public void setLife(JavaLife life) {
this.life = life;
}
public void setColor(int r, int g, int b) {
this.color = new Color(r, g, b);
}
public Color getColor() {
return color;
}
@Override
public void paint(Graphics g) {
Dimension size = size();
g.setColor(getColor());
g.fillRect(0,0, size.width, size.height);
g.setColor(new Color(0, 0, 0));
for(int i=0; i<life.data.getWidth(); i++) {
for(int j=0; j<life.data.getHeight(); j++) {
if(life.data.getCell(i, j) ==1)
g.fillOval(i*cell_size, j*cell_size, cell_size, cell_size);
}
}
//System.out.println("repaint");
}
/**
* Double buffering
*/
@Override
public void update(Graphics g) {
Graphics offgc;
Image offscreen = null;
Rectangle box = g.getClipRect();
// create the offscreen buffer and associated Graphics
offscreen = createImage(box.width, box.height);
offgc = offscreen.getGraphics();
// clear the exposed area
offgc.setColor(getBackground());
offgc.fillRect(0, 0, box.width, box.height);
offgc.setColor(getForeground());
// do normal redraw
offgc.translate(-box.x, -box.y);
paint(offgc);
// transfer offscreen to window
g.drawImage(offscreen, box.x, box.y, this);
}
}