Java2D Lighting

So I was bored in my statistics class and thought about lighting and java2D. ::slight_smile:

After a little while (1-2 hours class time) I got something working…its kinda a hack and only is useful if you have only one source of light (lame) but I think it is very fast and involves very simple code.

The advantage is that you can use any image as the light sources/shape. You can also set whether you want to use color from the image or not.

I am wondering if there are any better ways of doing thing using only java2D.

There is a way that has none of the downsides you mention and has a lot more features, running twice as fast. So yes, there are certainly better ways than the one you didn’t describe.

I guess he was wondering how the Java2D pros do lighting even though that wasn’t exactly what he wrote. Anyway, you still have a point. xD

Any sane developer does not implement lighting in Java2D. It’s a poor fit, at best.

just dont combine those two words lighting and Java2D

you can do a flashlight effect with average performance, thinking of Rescue Squad 2

But nothing more

http://www.triangularpixels.com/RescueSquad2/Images/Night.png

well nevermind it seems the Rescue Squad games are lwjgl too

@StumpyStrust just go use Slick or LibGdx as suggested in your subimage thread =P

Rescue Squad is indeed OpenGL/LWJGL - it wouldn’t be possible to do in J2D since it runs at a (comparatively) high resolution and supports tens of lights at a time.

The best example of lighting in J2D is probably L4kD ( http://www.mojang.com/notch/j4k/l4kd/ ), which largely gets away with it because it’s running at a tiny resolution and scaling it up at the end.

Yes I know that java2D is really bad for lights and I did not show how I did my little lighting thing, I just wanted to know if anyone came up with some interesting ways of trying light in java2D.

Oh and the left 4k dead game is freaking awesome. Any idea how the light is done in that?

And here is they way I got some light working.


  public class MyPanel extends JPanel
	    {
			public void paintComponent(Graphics g)
	    	{
	    		Rectangle2D shape = new Rectangle.Double(x-(size/2),y-(size/2),size,size);
	    		Graphics2D g2d = (Graphics2D)g;

	    		g2d.setClip(shape);
	    	
	    		g2d.drawImage(images[random], x-(size/2),y-(size/2),size, size,null);
			g2d.drawImage(images[0], 0,0, frame.getWidth(),frame.getHeight(),null);
			g2d.setClip(null);

				
				
				drawLight(g);
				
				
				
				
				
	    		g2d.dispose();
				g.setColor(Color.yellow);
				g.drawString("FPS: " + fps, 15,15);
				g.dispose();
				
	    		
	    	}
	    	
	    }
	    
	    public void drawLight(Graphics g)
	    {
	    	Graphics2D g2d = (Graphics2D) g.create();
	    	Composite alphaComp;
	    	
    		alphaComp = AlphaComposite.getInstance(
                    AlphaComposite.DST_ATOP, 1f);
    		g2d.setComposite(alphaComp);
    		g2d.drawImage(images[random], x-(size/2),y-(size/2),size, size,null);
 
    		
    		alphaComp = AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, 0f);
    		g2d.setComposite(alphaComp);
    		g2d.drawImage(images[random], x-(size/2),y-(size/2),size, size,null);
    		
    		
    		g2d.dispose();
	    }

I just looked at it again and it works rather well. If you want a single light point it is not too bad. The second alphaComosite in the drawLight method is if you want to use any of the color in the light texture/image.

http://code.google.com/p/box2dlights/

Light geometry is cpu side and drawing light meshes is gpu job.
If you draw lights to bitmap and draw that over the scene with something like opengl blend mode destColor, srcColor it works nicely. Can’t remember is there anything like that thought.

Here is a short Java2D Lighting example.


  public void paint (Graphics g){
    super(g);
    Graphics2D g2d = (Graphics2D)g;
    java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float(targetX, targetY);
    float radius = 72;
    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, radius, dist, colors);
    g2d.setPaint(p);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f));
    g2d.fillRect(0, 0, window.getWidth(), window.getHeight());
    g2d.dispose();
  }

If your game has a static background / static lights on it, the performance is not a problem.
(only rendered once)

The advantage of using just Java2D is that the game requires no external libs.
Wich makes sense in a “high compatability” approach. (eg small Applet games)