Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java2D Lighting  (Read 4786 times)
0 Members and 1 Guest are viewing this topic.
Offline StumpyStrust
« Posted 2012-02-28 11:06:07 »

So I was bored in my statistics class and thought about lighting and java2D.   Roll Eyes

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.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-02-28 12:38:25 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online theagentd
« Reply #2 - Posted 2012-02-28 14:40:18 »

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

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-02-28 14:43:09 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Cero
« Reply #4 - Posted 2012-02-28 14:55:28 »

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


Offline Cero
« Reply #5 - Posted 2012-02-28 14:58:06 »

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

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2012-02-28 17:41:25 »

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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline StumpyStrust
« Reply #7 - Posted 2012-02-29 00:46:50 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
  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.

Offline pitbuller
« Reply #8 - Posted 2012-02-29 10:06:04 »

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.
Offline The Voice

Junior Newbie





« Reply #9 - Posted 2012-03-21 07:53:11 »

Here is a short Java2D Lighting example.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
  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();
  }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Damocles
« Reply #10 - Posted 2012-03-21 08:16:36 »

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)

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!