Skip to content

Commit b93e63e

Browse files
committed
Uploaded homecoming project
1 parent a000271 commit b93e63e

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

Diff for: src/homecoming/GamePanel.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package homecoming;
2+
3+
import java.awt.Color;
4+
import java.awt.Font;
5+
import java.awt.FontMetrics;
6+
import java.awt.Graphics;
7+
import java.awt.Image;
8+
import java.awt.Rectangle;
9+
import java.awt.Toolkit;
10+
import java.awt.image.BufferedImage;
11+
import java.awt.image.FilteredImageSource;
12+
import java.awt.image.ImageFilter;
13+
import java.awt.image.ImageProducer;
14+
import java.awt.image.RGBImageFilter;
15+
import java.io.File;
16+
import java.io.IOException;
17+
18+
import javax.imageio.ImageIO;
19+
import javax.swing.JPanel;
20+
21+
public class GamePanel extends JPanel {
22+
static {
23+
BufferedImage img = null;
24+
BufferedImage background = null;
25+
try {
26+
img = ImageIO.read(new File("download.png"));
27+
background = ImageIO.read(new File("download.jpg"));
28+
} catch (IOException ioe) {
29+
System.err.println("Fatal error: Failed to load images");
30+
System.exit(1);
31+
}
32+
backgroundImg = background;
33+
heartImg = makeColorTransparent(img, Color.WHITE);
34+
}
35+
static final Image heartImg;
36+
static final Image backgroundImg;
37+
static final Font font = new Font("Courier", Font.BOLD, 30);
38+
39+
@Override
40+
public void paintComponent(Graphics g) {
41+
g.setColor(Color.WHITE);
42+
g.fillRect(0, 0, getWidth(), getHeight());
43+
44+
g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), null);
45+
46+
47+
g.setColor(Color.WHITE);
48+
g.setFont(font);
49+
drawCenteredString(g, String.format(Main.GREETING, Main.NAME), new Rectangle(0, 0, getWidth(), getHeight()),
50+
font);
51+
52+
for (Heart heart : Main.state.getHearts()) {
53+
g.drawImage(heartImg, heart.pos.x, heart.pos.y, GameState.FALLING_HEART_SIZE, GameState.FALLING_HEART_SIZE,
54+
null);
55+
}
56+
}
57+
58+
// Copied from
59+
// https://stackoverflow.com/questions/27706197/how-can-i-center-graphics-drawstring-in-java
60+
public void drawCenteredString(Graphics g, String text, Rectangle rect, Font font) {
61+
// Get the FontMetrics
62+
FontMetrics metrics = g.getFontMetrics(font);
63+
// Determine the X coordinate for the text
64+
int x = rect.x + (rect.width - metrics.stringWidth(text)) / 2;
65+
// Determine the Y coordinate for the text (note we add the ascent, as in java
66+
// 2d 0 is top of the screen)
67+
int y = rect.y + ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent();
68+
// Set the font
69+
g.setFont(font);
70+
// Draw the String
71+
g.drawString(text, x, y);
72+
}
73+
74+
// Copied from https://www.javaworld.com/article/2074105/making-white-image-backgrounds-transparent-with-java-2d-groovy.html
75+
public static Image makeColorTransparent(final BufferedImage im, final Color color) {
76+
final ImageFilter filter = new RGBImageFilter() {
77+
// the color we are looking for (white)... Alpha bits are set to opaque
78+
public int markerRGB = color.getRGB() | 0xFFFFFFFF;
79+
80+
public final int filterRGB(final int x, final int y, final int rgb) {
81+
if ((rgb | 0xFF000000) == markerRGB) {
82+
// Mark the alpha bits as zero - transparent
83+
return 0x00FFFFFF & rgb;
84+
} else {
85+
// nothing to do
86+
return rgb;
87+
}
88+
}
89+
};
90+
91+
final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
92+
return Toolkit.getDefaultToolkit().createImage(ip);
93+
}
94+
}

Diff for: src/homecoming/GameState.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package homecoming;
2+
3+
import java.awt.Point;
4+
import java.util.ArrayList;
5+
6+
public class GameState {
7+
static final int FALLING_HEART_SIZE = 50;
8+
9+
static final int SPEED = 5;
10+
static final int SPACING = 10;
11+
12+
private ArrayList<Heart> hearts;
13+
private int counter;
14+
15+
public GameState() {
16+
hearts = new ArrayList<>();
17+
}
18+
19+
public void update() {
20+
counter++;
21+
if(counter % 25 == 0) {
22+
Point pos = new Point((int) (500 * Math.random()), 0);
23+
int speed = (int) (4 * Math.random()) + 2;
24+
hearts.add(new Heart(pos, speed));
25+
}
26+
27+
for (Heart heart : hearts) {
28+
heart.pos.y += heart.speed;
29+
}
30+
31+
}
32+
33+
public ArrayList<Heart> getHearts() {
34+
return hearts;
35+
}
36+
37+
}

Diff for: src/homecoming/Heart.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package homecoming;
2+
3+
import java.awt.Point;
4+
5+
public class Heart {
6+
public final Point pos;
7+
public final int speed;
8+
9+
public Heart(Point p, int speed) {
10+
pos = p;
11+
this.speed = speed;
12+
}
13+
}

Diff for: src/homecoming/Main.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package homecoming;
2+
3+
import java.awt.Dimension;
4+
5+
import javax.swing.JFrame;
6+
7+
public class Main {
8+
static final String NAME = "Alex";
9+
static final String GREETING = "Homecoming %s?";
10+
11+
static final GameState state = new GameState();
12+
public static void main(String args[]) throws InterruptedException {
13+
JFrame frame = new JFrame("Homecoming?");
14+
15+
GamePanel game = new GamePanel();
16+
frame.add(game);
17+
18+
frame.setPreferredSize(new Dimension(500, 500));
19+
frame.pack();
20+
21+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22+
frame.setResizable(false);
23+
24+
frame.setVisible(true);
25+
26+
while (true) {
27+
long startTime = System.currentTimeMillis();
28+
29+
state.update();
30+
frame.repaint();
31+
32+
long elapsedTime = System.currentTimeMillis() - startTime;
33+
Thread.sleep(Math.max(0, 1000 / 30 - elapsedTime));
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)