Maarten Blokker
Maarten Blokker

Reputation: 604

Layered drawing in java

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?

Upvotes: 1

Views: 792

Answers (2)

lbalazscs
lbalazscs

Reputation: 17784

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.

Upvotes: 1

Edwin Buck
Edwin Buck

Reputation: 70909

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.

Upvotes: 1

Related Questions