From 6ce9848c2b2936800e1ea751fd2d6d98a908bd47 Mon Sep 17 00:00:00 2001 From: Mohamed Omar Date: Mon, 12 Feb 2024 19:50:41 +0000 Subject: [PATCH] update rendering of game overlay to make it transparent --- src/com/redomar/game/Game.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/com/redomar/game/Game.java b/src/com/redomar/game/Game.java index 20b5252..5a86c4a 100644 --- a/src/com/redomar/game/Game.java +++ b/src/com/redomar/game/Game.java @@ -69,7 +69,7 @@ public class Game extends Canvas implements Runnable { private static MouseHandler mouse; // Tracks mouse movement and clicks, and follows the appropriate actions private static InputContext context; // Provides methods to control text input facilities // Graphics - private final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); + private final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); private final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // Array of red, green and blue values for each pixel private final int[] colours = new int[6 * 6 * 6]; // Array of 216 unique colours (6 shades of red, 6 of green, and 6 of blue) private final BufferedImage image2 = new BufferedImage(WIDTH, HEIGHT - 30, BufferedImage.TYPE_INT_RGB); @@ -326,7 +326,10 @@ public void init() { int rr = (r * 255 / 5); // Split all 256 colours into 6 shades (0, 51, 102 ... 255) int gg = (g * 255 / 5); int bb = (b * 255 / 5); - colours[index++] = rr << 16 | gg << 8 | bb; // All colour values (RGB) are placed into one 32-bit integer, populating the colour array + // All colour values (RGB) are placed into one 32-bit integer, populating the colour array + // The first 8 bits are for alpha, the next 8 for red, the next 8 for green, and the last 8 for blue + // 0xFF000000 is ignored in BufferedImage.TYPE_INT_RGB, but is used in BufferedImage.TYPE_INT_ARGB + colours[index++] = 0xFF << 24 | rr << 16 | gg << 8 | bb; } } } @@ -484,9 +487,9 @@ public void render() { } Graphics2D g = (Graphics2D) bs.getDrawGraphics(); - g.drawRect(0, 0, getWidth(), getHeight()); g.drawImage(image, 0, 0, getWidth(), getHeight() - 30, null); status(g, isDevMode(), isClosing()); + overlayRender(g); g.drawImage(image2, 0, getHeight() - 30, getWidth(), getHeight(), null); g.setColor(Color.WHITE); g.setFont(font.getSegoe()); @@ -515,6 +518,14 @@ public void render() { bs.show(); } + /** + * This method renders the overlay of the game, which is a transparent layer that is drawn over the game. + */ + private void overlayRender(Graphics2D g) { + g.setColor(new Color(0f, 0f, 0f, .192f)); // Transparent color + g.fillRect(0, 0, getWidth(), getHeight()-30); + } + /* * This method displays information regarding various aspects/stats of the game, dependent upon * whether it is running in developer mode, or if the application is closing.