|
| 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 | +} |
0 commit comments