views:

80

answers:

6

We have an old Java Swing application. we need to display thousands, hundreds of thousands small circle spots on the canvas based on the real data. Right now we have an image file of a small circle spot. When we need it, we draw that image onto the canvas, thousands, hundreds of thousands times.

Now I am think it may be better (better performance and memory usage) to just draw a filled circle each time instead of load the image and draw it.

how about your opinion?

thanks,

A: 

I don't now if this would be helpful but you can test which one works for you by testing worst case . But I think filled circle would be best .

Ender
+2  A: 

You only need to load the template image once and hold it in memory and copy it to the canvas as needed using Graphics2D drawImage function. Drawing multiple filled circles may become expensive due to calls to the Flood-fill/Scan-fill algorithm as well as Bresenham to draw the circle. To optimize the rendering you can also decimate the rendered result or perform clustering, since the user will not really appreciate dense overlapping circles anyway.

To reduce render calls test the pixel where your template is going and pass a render if it is already coloured.

Here is a nice benchmarking applet.

whatnick
I don't think circles, or any other regular geometric objects, are filled using a flood fill algorithm.
ammoQ
I believe they use scan-fill. Reading java native code would be handy.
whatnick
A: 
ammoQ
why the downvote?
ammoQ
+1  A: 

It is almost certainly much faster to hold a single image and draw it many times than to make a call to draw a filled circle. Here is a recent presentation on the subject, showing that it is faster to draw an image than even a simple horizontal cross. http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-4170&yr=2009&track=javase

DJClayworth
A: 

It's hard to predict which is faster, because certain operations under certain circumstances are accelerated by the GPU hardware of the video card. If the GPU is used to make the circle, then that would be much faster than the cpu copying pixels of a buffered circle as an image.

There is VolatileImage as well. Perhaps it's possible to make the image blits so that they end up being accelerated. The only way to find out is to benchmark it yourself.

Mike
A: 

Time your code

It is most definitely faster to draw an image lots of times than drawing a circle or String lots of times and it's very easy to test. At the beginning of your paintComponent() method add the line:

paintComponent(){
   long start = System.currentTimeMillis();

   ...
   // draw 100,000 circles as images or circles
   ...


   System.out.println("Rendering time: " + 
             (start - System.currentTimeMillis()) + " ms");
}

If the times turn out to be zero all the time, you can instead use System.nanoTime().

Paint to Cached Image

Another thing you can do is to paint these circles onto an image and only recreate the image when the content changes. If nothing has changed just draw that image onto the Graphics2D object instead of redrawing all of the circles. This is commonly called double buffering. You also can use Volatile Images to take advantage of hardware acceleration.

Create Compatible Images

You should also make sure you use images that are compatible with the user's monitor by using createCompatibleImage() as shown below:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();

// Create an image that does not support transparency
BufferedImage bimage = gc.createCompatibleImage(width, height, Transparency.OPAQUE);

// Create an image that supports transparent pixels
bimage = gc.createCompatibleImage(width, height, Transparency.BITMASK);

// Create an image that supports arbitrary levels of transparency
bimage = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);

More Tips

I'd recommend the book Filthy Rich Clients. It has lots of great tips for speeding up swing. Especially look at chapters 4 and 5 about images and performance.

Jay Askren