[help] Basic lighting

Hi JGO!

I am currently creating a game that required lighting.
Currently i have something working but that only works with 1 light.
I tried adding more lamps using a arrayList but that failed.
What has to be changed to support multiple lights?


ArrayList<Light> lights = new ArrayList<Light>();

public void addRandomLight() { // Called on mouse-click
	try {
		lights.add(new Light(mouse.getX(), mouse.getY(), 200));
	} catch (Exception e) {}
}

public void createLightMap(Graphics g) { // Called int render (fow now, gonna split it up eventially), after rendering the map
	Graphics2D g2d = (Graphics2D) g;
	RadialGradientPaint p = null;
	for (Light l : lights) {
	    java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float(l.x, l.y);
	    float radius = l.r;
	    float[] dist = {0.0f, 1.0f};
	    Color[] colors = {new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK};
	    p = new RadialGradientPaint(center, radius, dist, colors);
	    g2d.setPaint(p);
	    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
	    g2d.fillRect(0, 0, 1280, 736);
	}
}

From what I can see it seems like you’re trying to paint rectangles over eachother, and the latest one is the one showing up; and thus it seems like there is only one. I’m not too familiar with Java’s Graphics, so I don’t have the solution for you…

Edit: Nevermind that, saw the alpha values and I suppose this shouldn’t happen with proper alpha blending.

Color[] colors = {new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK};

Might be the fault, a zero alpha color turning to black wouldn’t produce color on a black background?

I know that fading to black is the problem, That’s why I am here to ask if someone has a solution.

Then fade to a transparent color?

Color[] colors = {new Color(lightR, lightG, lightB, 1.0f), new Color(lightR, lightG, lightB, 0.0f)};

Edit: Do you have any code from when you were rendering just one light as well? It seems you know there is a problem, but I don’t know if a light is being rendered properly at all with current information. I’d say you are not putting any color into the light, and the fadeout color shouldn’t be opaque, but some insight(or snippets) to see how your code worked before and after changing to multiple lights could help us understand your problem.

Why are you fading from black to black?