my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Graphics and animation in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Graphics and animation in Java" Watch "Graphics and animation in Java" New topic

Graphics and animation in Java

Mani Pava

Joined: Apr 29, 2008
Posts: 5

If anyone can, please give me a way to get some resources(tutorials & examples) related to Graphics2D,Grphics3D and animation stuff in Java.

I've found various materials by searching in the net. But couln't get a nice tutorial with examples.

Thanks in advance,
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37970
Have you been through the Java Tutorials? You will find 2D there, but I don't think animation.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Trail: 2D Graphics
You'll find an animation under the second paragraph on How to Make Applets followed by a link to the source code.
Andy Selador
Ranch Hand

Joined: May 02, 2008
Posts: 34
What about hardware acceleration? I've heard that you can only do it using a Canvas because of the createBufferStrategy() method in combination with the GraphicsConfiguration class, but can you do it using Swing components? I've read some about Volatile images, but do not know much about some of the fundamental concepts (i.e. if I just draw to a BufferedImage and then have the JPanel graphic draw that image, is that the same thing, or do I have to allocate the memory in the accelerated graphics memory? Whats the difference?). Sorry for kind of commandeering this thread, but it seems in line with the topic.

Andy Selador
Ranch Hand

Joined: May 02, 2008
Posts: 34
I figured it out:

GraphicsConfiguration configuration = GraphicsEnvironment.

Then, when you want to draw the image:

VolatileImage image = configuration.
createCompatibleVolatileImage(getWidth(), getHeight());
/* assuming you are extending the component */

Check the VolatileImage API for information on checking if the image is available to be drawn on, and make sure to flush the image if you create a new one assigned to the reference to save resources.

This reply is kind of crap in its explanation, but the JavaDocs for the VolatileImage class and createCompatibleVolatileImage method are good.

(The whole reason to use hardware acceleration is to bypass the CPU, as best as I can tell.)

As for how to handle the buffer strategy with Swing components, and if you even need to...
I have no clue.

I agree. Here's the link:
subject: Graphics and animation in Java
Similar Threads
web based gaming code
============ applet's refreshing problem ============
how about some java games
WA #1.....word association
Animation and othere featurs in console