Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
990 views
in Technique[技术] by (71.8m points)

java - How do I speed up this swing animation?

I am drawing an animation onto a swing JPanel. The 1024x1024 screen is divided up into 2000 pieces which cover the screen exactly (no overlap).

each piece is a very small piece of the screen (1/2000'th of it). The animation draws one piece every millisecond by changing the pixels in the buffered image and calling reaint(). So at the entire screen changes every 2 seconds. The animation is run in a java.util.Timer task.

The buffered image is not accelerated and is not volatile. I set it's priority to 1.

The frame root pane is optimized. Both the front and back buffer are accelerated but not volatile.

This works ok.

Profiling indicates that almost all of the grahics time is spend drawing the buffered image as one would expect, Using some dirty rectangles does not seem to help reduce the paint time.

If I clip the drawing of the buffered image with it's exact shape, that piece gets painted and the rest of the screen turns white.

What I want to is to have the rest of the screen stay the way it was and just have the clipped shape get painted.

Making the pixels for each piece requires blending 10 layers, so there is some computation done there. could/should this be done better in an awt timer thread?

Or should i use a canvas and the update trick http://java.sun.com/products/jfc/tsc/articles/painting/src/UpdateDemo.java

I have seen suggestion that include Toolkit.getDefaultToolkit().setDynamicLayout(true); System.setProperty("sun.awt.noerasebackground","true");

other suggestions include paintimmedialtely, and subclassing JComponent instead of JPanel.

I find this all somewhat confusing.

I would like to keep this OS independent as much as possible, but the app will run mostly on windows 7.

What is the next (small) logical step for me to take here?

Update: Using a canvas (without the update trick) remarkably reduced the time spent drawing the buffered image. Instead of this being on the top line of the profiler, i can't find it! i may be doing more than i should when i use the panel.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here are a few suggestions:

  • Critically examine the need to repaint() every millisecond; in particular, see if some updates may be coalesced.

  • Consider the convenience of java.swing.Timer, which renders on the EDT and supports coalescing events.

  • Calls to drawImage() are fastest when no scaling is required, as shown in this AnimationTest.

  • Always pre-compute images to the extent possible, as suggested in this KineticModel that illustrates several animation techniques.

  • TexturePaint, used in KineticModel, is also shown here.

  • IndexColorModel, illustrated here, may be applicable.

  • An sscce will allow you to isolate various approaches for easier profiling.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...