-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePanel.java
85 lines (73 loc) · 2.55 KB
/
GamePanel.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
77
78
79
80
81
82
83
84
85
package uk.ac.cam.cjo41.gameoflife;
import javax.swing.*;
import java.awt.*;
/**
* Represents the panel showing the game board.
*/
public class GamePanel extends JPanel {
private World mWorld = null;
/**
* Creates game board from world
*
* @param g java.awt.Graphics object
*/
@Override
protected void paintComponent(java.awt.Graphics g) {
// Paint the background white
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
// Returns if no mWorld has been specified
if (mWorld == null) return;
// Gets sizes and stores
int worldWidth = mWorld.getWidth();
int worldHeight = mWorld.getHeight();
int panelWidth = this.getWidth();
int panelHeight = this.getHeight();
// Calculates maximum side size
int dx = panelWidth / worldWidth;
int dy = panelHeight / worldHeight;
int side = Math.min(dx, dy);
// Calculates number of pixels in 'gaps'
int leftOverX = panelWidth - side * worldWidth;
int leftOverY = panelHeight - side * worldHeight;
int leftOver = Math.min(leftOverX, leftOverY);
for (int row = 0; row < worldHeight; row++) {
// Calculates height of given row and y position
int mHeight = side;
int ypos = row * side;
if (row < leftOver) {
ypos += row;
mHeight++;
}
else ypos += leftOver;
for (int col = 0; col < worldWidth; col++) {
// Calculates width of given column and x position
int mWidth = side;
int xpos = col * side;
if (col < leftOver) {
xpos += col;
mWidth++;
}
else xpos += leftOver;
// Draws rectangle
if (mWorld.getCell(col, row)) {
g.setColor(Color.BLACK);
g.fillRect( xpos, ypos, mWidth, mHeight);
}
else {
g.setColor(Color.LIGHT_GRAY);
g.drawRect( xpos, ypos, mWidth, mHeight);
}
}
}
}
/**
* Takes a world as parameter and displays that world in the panel.
*
* @param w World object
*/
public void display(World w) {
mWorld = w;
repaint();
}
}