Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Graphics and animation in Java

 
Mani Pava
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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,
Mani
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the Java Tutorials? You will find 2D there, but I don't think animation.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andy Selador
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out:

GraphicsConfiguration configuration = GraphicsEnvironment.
getLocalGraphicsEnvironment().
getDefaultScreenDevice().
getDefaultConfiguration();

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.

Andy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic