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:
- A buffered image is created
- Every layers gets drawn on the image
- 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 60000×60 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?
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.