Skip to content

Commit 6ce9848

Browse files
committed
update rendering of game overlay to make it transparent
1 parent 5683624 commit 6ce9848

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/com/redomar/game/Game.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class Game extends Canvas implements Runnable {
6969
private static MouseHandler mouse; // Tracks mouse movement and clicks, and follows the appropriate actions
7070
private static InputContext context; // Provides methods to control text input facilities
7171
// Graphics
72-
private final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
72+
private final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
7373
private final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // Array of red, green and blue values for each pixel
7474
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)
7575
private final BufferedImage image2 = new BufferedImage(WIDTH, HEIGHT - 30, BufferedImage.TYPE_INT_RGB);
@@ -326,7 +326,10 @@ public void init() {
326326
int rr = (r * 255 / 5); // Split all 256 colours into 6 shades (0, 51, 102 ... 255)
327327
int gg = (g * 255 / 5);
328328
int bb = (b * 255 / 5);
329-
colours[index++] = rr << 16 | gg << 8 | bb; // All colour values (RGB) are placed into one 32-bit integer, populating the colour array
329+
// All colour values (RGB) are placed into one 32-bit integer, populating the colour array
330+
// The first 8 bits are for alpha, the next 8 for red, the next 8 for green, and the last 8 for blue
331+
// 0xFF000000 is ignored in BufferedImage.TYPE_INT_RGB, but is used in BufferedImage.TYPE_INT_ARGB
332+
colours[index++] = 0xFF << 24 | rr << 16 | gg << 8 | bb;
330333
}
331334
}
332335
}
@@ -484,9 +487,9 @@ public void render() {
484487
}
485488

486489
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
487-
g.drawRect(0, 0, getWidth(), getHeight());
488490
g.drawImage(image, 0, 0, getWidth(), getHeight() - 30, null);
489491
status(g, isDevMode(), isClosing());
492+
overlayRender(g);
490493
g.drawImage(image2, 0, getHeight() - 30, getWidth(), getHeight(), null);
491494
g.setColor(Color.WHITE);
492495
g.setFont(font.getSegoe());
@@ -515,6 +518,14 @@ public void render() {
515518
bs.show();
516519
}
517520

521+
/**
522+
* This method renders the overlay of the game, which is a transparent layer that is drawn over the game.
523+
*/
524+
private void overlayRender(Graphics2D g) {
525+
g.setColor(new Color(0f, 0f, 0f, .192f)); // Transparent color
526+
g.fillRect(0, 0, getWidth(), getHeight()-30);
527+
}
528+
518529
/*
519530
* This method displays information regarding various aspects/stats of the game, dependent upon
520531
* whether it is running in developer mode, or if the application is closing.

0 commit comments

Comments
 (0)