Hello!
I’m working on my first real game. It is tile-based, and I am only using a library to play mp3’s at the moment.
I added a lighting effect to my game with G2D RadialGradientPaint and setComposite, and I am having troubles drawing anything on top of that layer in regular graphics g.
This class draws a light circle around the player which gets darker towards the edges and allows me to adjust the radius, simulating night/day vision distance somewhat.
public void render(Graphics g) {
x = TileGame.getPlayer().getX() + 16;
y = TileGame.getPlayer().getY() + 16;
Graphics2D g2d = (Graphics2D) g;
java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float((float)x, (float)y);
float[] dist = {0.0f, 1.0f};
Color[] colors = {new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK};
RadialGradientPaint p = new RadialGradientPaint(center, currentRadius, dist, colors);
g2d.setPaint(p);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, .6f));
g2d.fillRect(64, 64, 640, 640);
g2d.dispose();
}
In my main game loop, I have multiple things being rendered in a specific order.
. . . .
levelManager.render(g);
ui.render(g);
crops.render(g);
player.render(g);
light.render(g);
My issue is this: In the above snippet, I want to draw UI.render() after the light.render() effect (which uses g2d). For some reason, if I put them in this order:
. . . .
levelManager.render(g);
crops.render(g);
player.render(g);
light.render(g);
ui.render(g);
None of the UI gets rendered, even ones which weren’t within the original g2d.fillRect(64, 64, 640, 640) area. It seems like I need to cast my g2d back into g? Not sure how to solve this, I would appreciate some assistance.