File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Game Development and the fly likes Nice 2D graphics? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Nice 2D graphics?" Watch "Nice 2D graphics?" New topic

Nice 2D graphics?

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I'm looking to create a nice-looking display for a Game of Life application I wrote... currently it looks like this:

but I want it to look like this:

Any suggestions of how to do this? Especially would like nice lighting effects, and preferably with a 2D API since I've never done anything in 3D. Is the built-in Java 2D at all useable? I've looked at PulpCore briefly, but I think that's mainly for applets rather than desktop apps.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

Java 2D is certainly useable, it is hardware-accelerated, it's very well suited to do nice, smooth animations.

It can do double-buffering (or even use more than 2 buffers) for that. That means that you have two buffers to draw on in your application: one that is shown on screen, the other one is what your program is actually drawing on. When you're done drawing the next frame of the animation, you swap the two buffers (and then start drawing the next frame on the one that's now hidden).

There are several libraries for 3D graphics. There is for example jMonkeyEngine which is an impressive 3D graphics engine, with which you can relatively easy make great looking 3D graphics. Another one is LWJGL, which also provides you with OpenGL 3D graphics, OpenAL 3D sound and other things.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 119
The target example has a black background and white circles painted on it. It also seems to have partially transparent "glows" that can overlap.

You example seems to have a white background and blue squares painted on it.

The biggest hurdle seems to me to be figuring out how to make glowing effects. You can probably figure out for yourself how to do the black background and paint circles instead of squares. You should look into how to use colors that have "alpha" values, such as RGBA format where the "alpha" is the degree of transparency. Also, there are possibilities such as using GradientPaint or RadialGradientPaint (and the gradients can apply to alpha as well as R, G, or B).

Here's the link for Color. Be sure and check out the info there on transparency (alpha settings) and try applying it to Paint objects.

So, paint the background black, pop on a larger circle with the desired "glow" transparent color, then put a smaller white circle over that. That might get you 80% of the way to what you want.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Thanks. The RadialGradientPaint class looks interesting - I might have a closer look at what it can do.

I already coded a Gaussian blur demo that works:

I found a useful site on producing bloom effects:

Also some interesting pages on image filters in Java:

Applying full Gaussian blurs in real time is computationally demanding (and inefficient), so I'll look at pre-computing the LED images. If I only want to display 1 size and a restricted colour palette I could just render those images in a paint package, but the problem becomes less interesting. I'd like to be able to scale to any resolution without a high performance penalty, and also have freedom to easily change colours.

Also I'll probably implement a mirror effect for the surface the panel is sitting on, which shouldn't be too hard.

Here is a demo in PulpCore (click the moons for reflection and other effects) - surprising there is such a good API for applets but nothing similar AFAIK for normal Swing apps (maybe JavaFX was supposed to fill this niche?). Maybe I should program this as an applet, but I don't suppose those will be hardware accelerated?
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4616

You can generate an image once when the window resizes, and then you can simply render that image with alpha compositing.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: Nice 2D graphics?
It's not a secret anymore!