Zoom an Image in Runtime

Hi All,

Is possible to zoom an image during the runtime?
My idea is to have two buttons, one that zoom in and the second one to zoom out.


You should be able to use the transform function.


Though determining the current state of your image may be a bit harder.

That transforms the component, not the image in the component. Use of the Paintable Canvas to draw the image is probably necessary, and allows near-infinite variation.

First of all, thanks @pturmel for the hint :wink:

Then, for posterity:

from javax.imageio import ImageIO
from java.io import FileInputStream

# 1. Open the image
fsIP = FileInputStream("path/to/image.png")
myImage = ImageIO.read(fsIP)

# 2. Get the scale factor and convert the Integer percent value to a Float scale factor
scaleFactor = event.source.zoom * 0.01

# 3. Scale and draw the image 
g = event.graphics
g.scale(scaleFactor, scaleFactor)

If you use an image component, and set the image to scale to the bounds, it will also scale the image.
Though scaling components at run time may have some strange results, so your solution (where all components remain in place), is probably more stable.

You really, really shouldn’t be doing the file read in your paint event. Add a custom property to hold your file name, and use ImageIO on propertyChange and cache using setClientProperty(). The paint event just retrieves the cached image and draws it. Take a closer look at the code examples I linked.

1 Like