I am trying to develop some kind of lighting system for my game. So far I made rectangle with cut out circle in the middle with some gradient, and I really like it but I would love to have more than one light source at once and I have no idea how to do this properly.
My current code in two versions:
First:
Point2D center = new Point2D.Float(Main.localConfig.WINDOW_WIDTH/2, Main.localConfig.WINDOW_HEIGHT/2);
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,100,dist,colors);
g2D.setPaint(p);
g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.95f));
g2D.fillRect(0, 0, Main.localConfig.WINDOW_WIDTH, Main.localConfig.WINDOW_HEIGHT);
g2D.dispose();
And second version:
public class LightArea {
public BufferedImage Shadows;
int circleSize;
int circleCenterCordX, circleCenterCordY;
public LightArea(int _circleSize, int _circleCenterCordX, int _circleCenterCordY){
this.circleSize = _circleSize;
this.circleCenterCordX = _circleCenterCordX;
this.circleCenterCordY = _circleCenterCordY;
UpdateLight();
}
public void UpdateLight(){
Shadows = new BufferedImage(Main.localConfig.WINDOW_WIDTH, Main.localConfig.WINDOW_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = (Graphics2D) Shadows.getGraphics();
g2D.setColor(Color.BLACK);
Area Rectangle = new Area(new Rectangle2D.Double(0, 0, Main.localConfig.WINDOW_WIDTH, Main.localConfig.WINDOW_HEIGHT));
Area Circle = new Area(new Ellipse2D.Double(circleCenterCordX, circleCenterCordY, circleSize, circleSize));
Rectangle.subtract(Circle);
Color[] color = new Color[5];
float[] spacing = new float[5];
color[0] = new Color(0,0,0,0f);
color[1] = new Color(0,0,0,0.25f);
color[2] = new Color(0,0,0,0.50f);
color[3] = new Color(0,0,0,0.75f);
color[4] = new Color(0,0,0,0.95f);
spacing[0] = 0f;
spacing[1] = 0.25f;
spacing[2] = 0.50f;
spacing[3] = 0.75f;
spacing[4] = 0.95f;
RadialGradientPaint gradientPaint = new RadialGradientPaint(circleCenterCordX, circleCenterCordY, circleSize,spacing,color);
g2D.setPaint(gradientPaint);
g2D.fill(Circle);
g2D.fill(Rectangle);
g2D.dispose();
}
public void Render(Graphics2D g2D) {
g2D.drawImage(Shadows,0,0,null);
}
}
And both of them were executed from this method:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
for (List<Node> tempInnerList : Map)
for (Node tempNode : tempInnerList)
tempNode.Render(g2D);
}
(The method is in a Class
which extends JPanel
.
I tried to just use my code once again and draw it with:
g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.95f));
With different values and rules but it didn't work. Sometimes I everything was bright as if the shadows didn't appear. Sometimes I was getting weird white pixels at the edges of the circles, and at the place where they connect they were darker instead of brighter as I was expecting it to be.