Search code examples
javaimageswingimageicon

How to speed up zoom-in/zoom-out of the image with swing?


How to reduce the overhead of the image zoom in and zoom out.

Here is my code which takes too long time to zoom-in/zoom-out the image...

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
    int myX = 0;
    int myY = 0;

    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        try {
            float amount = e.getWheelRotation() * 5f;
            myX = (int) (label.getWidth() + amount);
            myY = (int) (label.getHeight() + amount);
            imgX = myX;
            imgY = myY;
            label.setSize(myX, myY);
            scalledItemImage = itemImage.getScaledInstance(imgX, imgY,
                    BufferedImage.TYPE_INT_ARGB);
            ImageIcon icon = new ImageIcon(scalledItemImage);
            label.setIcon(icon);
            repaint();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

How to speedup the image rendering with the expected resolution and clarity of the image.


Solution

  • you call the scale method in a wrong way....

    try to call :

    scalledItemImage = itemImage.getScaledInstance(imgX, imgY,
                    BufferedImage.SCALE_FAST);
    

    (as proposed above)