Howto Zoom In/Out With Java Swing

14:33

2005 article resurrected by wayback machine :/



I am working on a graph tool for fun (and maybe profit in future) and need to add zoom/unzoom feature. After many attempts on the issue, here is how I finally managed into it:

  • Components that supports zoom, needs to implement a "Zoomable" interface that knows the original location
  • Java2D 's affine transform object is used for painting with zoom ratio
  • A simple layout manager is written to handle positioning.
First you should override paint method of components that support zoom:
Loading ....
This is the simple interface that zoomable components have to implement:
Loading ....


Here is my (null) layout manager that keeps components where they claim to exist, and size that claim to have; proportional to application zoom ratio.
Loading ....

If you need more advanced layout like GridBagLayout you should implement it!

Note that according to what you plan to zoom/unzoom, things may get complicated. For example if you put multilevel deep containers, or advanced components that consist of multiple components they should all behave according to our plan!

Hope this is helpfull for someone.

Here is the resulting images:
[TODO regenerate images!]
Zoom Ratio: 0.25

Original size

Zoom ratio: 1.5

Zoom Ratio: 0.75

Zoom ratio: 0.10

Tempting isn't it? ;)

You Might Also Like

0 yorum