1

Im making a swing desktop application. It draws layered vector graphics and the interface uses a scrollbar and has the ability to zoom in. There are about 5 layers of vector graphics and one layer in particualy is very expensive to draw. The layers may require to be repainted from time to time (simple animation).

The situation right now is as follows:

  1. A buffered image is created
  2. Every layers gets drawn on the image
  3. the buffered image is displayed on the screen using a panel that has the same size so that a scrollbar can be used.

Im running into performance issues when i zoom in. The zooming can make the buffered image really big. An image can peak at 60000x60 when the scale is at 16x. Offcourse this takes up a lot of memmory and i want to redesign this.

I really dont know where to start though, i feel like im reinventing the wheel. My knowledge of java2d is also very limited. I really want to improve this by maybe using a library, i know there must be something that has the functionality i want. Any libraries that come to mind?

sjakubowski
  • 2,913
  • 28
  • 36
Maarten Blokker
  • 604
  • 1
  • 5
  • 23

2 Answers2

1

Don't create the buffered image. You are rasterizing the output before you know how much of it is to be displayed.

Look into "damage" rectangles, and have a callback system which tells you the "sub portion" of the raster it needs to present, then only rasterize the required pixels. That should give you relatively consistent performance no matter what zoom level, because the only pixels to be computed are the pixels to be displayed, not an image which has conceptually 16^2 times more pixels than it did before.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • I thought of this, but im really affraid it would affect the smoothness of the scrolling when using the scrollpane. Also, i feel like creating a system that detects the dirty area would be like reinventing the wheel, there must be a library that deals with this heavy lifting – Maarten Blokker Aug 17 '12 at 21:39
  • if there is a per-second speed-limit to the scrolling, let's call it 'c', you can render c + viewport + c; voila: smooth scrolling, because you'll always have the cached image to fall back on, and you can refill it via fast blipping – tucuxi Aug 17 '12 at 21:42
  • ah yes, you just calculate the difference! thanks a lot, i think this will get me going :) – Maarten Blokker Aug 19 '12 at 13:28
  • You really are reinventing the wheel, but that's because you want to deal with rasterizing yourself. If you could leverage the Java2D system, it would do this for you; however, I guess there's some reason you don't. – Edwin Buck Aug 20 '12 at 14:13
1

You could have a look at JHotDraw. I am not sure that it has the features you need.

Another idea is not to create a big image, only a small image and magnify it when zooming in (the magnifying is fast in Java). Of course, this leads to a lower-quality (pixelated) image, but this could be acceptable for the moment, and you could replace it with the high-quality vector graphics as soon as it is rendered.

lbalazscs
  • 17,474
  • 7
  • 42
  • 50