Skip to content

Commit e5fa135

Browse files
committed
Added comments
1 parent 0b07b09 commit e5fa135

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

src/homecoming/GamePanel.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import javax.swing.JPanel;
2020

2121
public class GamePanel extends JPanel {
22+
/*
23+
* This known as a static constructor
24+
*/
2225
static {
2326
BufferedImage img = null;
2427
BufferedImage background = null;
@@ -32,24 +35,34 @@ public class GamePanel extends JPanel {
3235
backgroundImg = background;
3336
heartImg = makeColorTransparent(img, Color.WHITE);
3437
}
38+
3539
static final Image heartImg;
3640
static final Image backgroundImg;
3741
static final Font font = new Font("Courier", Font.BOLD, 30);
3842

3943
@Override
4044
public void paintComponent(Graphics g) {
45+
/*
46+
* The getWidth() and getHeight() methods are inherited from JFrame
47+
*/
4148
g.setColor(Color.WHITE);
4249
g.fillRect(0, 0, getWidth(), getHeight());
4350

4451
g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), null);
4552

46-
53+
/*
54+
* Draw the greeting in the center of the screen
55+
*/
4756
g.setColor(Color.WHITE);
4857
g.setFont(font);
4958
drawCenteredString(g, String.format(Main.GREETING, Main.NAME), new Rectangle(0, 0, getWidth(), getHeight()),
5059
font);
51-
52-
for (Heart heart : Main.state.getHearts()) {
60+
61+
/*
62+
* Draw all of the hearts. Note that because it is drawn later, these will show
63+
* up on top of the text and background.
64+
*/
65+
for (Heart heart : Main.gameState.getHearts()) {
5366
g.drawImage(heartImg, heart.pos.x, heart.pos.y, GameState.FALLING_HEART_SIZE, GameState.FALLING_HEART_SIZE,
5467
null);
5568
}
@@ -71,7 +84,8 @@ public void drawCenteredString(Graphics g, String text, Rectangle rect, Font fon
7184
g.drawString(text, x, y);
7285
}
7386

74-
// Copied from https://www.javaworld.com/article/2074105/making-white-image-backgrounds-transparent-with-java-2d-groovy.html
87+
// Copied from
88+
// https://www.javaworld.com/article/2074105/making-white-image-backgrounds-transparent-with-java-2d-groovy.html
7589
public static Image makeColorTransparent(final BufferedImage im, final Color color) {
7690
final ImageFilter filter = new RGBImageFilter() {
7791
// the color we are looking for (white)... Alpha bits are set to opaque

src/homecoming/GameState.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ public GameState() {
1616
hearts = new ArrayList<>();
1717
}
1818

19+
/*
20+
* Update the game state
21+
*/
1922
public void update() {
2023
counter++;
24+
25+
/*
26+
* Every 25 ticks, create a new heart
27+
*/
2128
if(counter % 25 == 0) {
22-
Point pos = new Point((int) (500 * Math.random()), 0);
29+
Point position = new Point((int) (500 * Math.random()), 0);
2330
int speed = (int) (4 * Math.random()) + 2;
24-
hearts.add(new Heart(pos, speed));
31+
hearts.add(new Heart(position, speed));
2532
}
2633

2734
for (Heart heart : hearts) {

src/homecoming/Heart.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import java.awt.Point;
44

5+
/*
6+
* Wrapper for heart objects. We need to store both position and speed.
7+
*/
58
public class Heart {
69
public final Point pos;
710
public final int speed;
8-
11+
912
public Heart(Point p, int speed) {
1013
pos = p;
1114
this.speed = speed;

src/homecoming/Main.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
public class Main {
88
static final String NAME = "Alex";
9+
10+
/*
11+
* This is a format string, passed to String.format(GREETING, NAME);
12+
*/
913
static final String GREETING = "Homecoming %s?";
1014

11-
static final GameState state = new GameState();
15+
static final GameState gameState = new GameState();
1216
public static void main(String args[]) throws InterruptedException {
1317
JFrame frame = new JFrame("Homecoming?");
1418

@@ -26,7 +30,13 @@ public static void main(String args[]) throws InterruptedException {
2630
while (true) {
2731
long startTime = System.currentTimeMillis();
2832

29-
state.update();
33+
/*
34+
* Update the game state
35+
*/
36+
gameState.update();
37+
/*
38+
* Repaint the frame.
39+
*/
3040
frame.repaint();
3141

3242
long elapsedTime = System.currentTimeMillis() - startTime;

0 commit comments

Comments
 (0)