1

I'm using an ImageIcon in order to display an image in a JPanel. I need to implement zoom in / zoom out functions over that image.

If I use the following code it does the zoom out/in work, but the quality of the image reduces significantly due to the transparency.

        BufferedImage resizedImage = new BufferedImage(newImageWidth, newImageHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();           

        g.drawImage(image, 0, 0, newImageWidth , newImageHeight , null);
        g.dispose();

        return resizedImage;

I have read that this other following code is a solution but it gives me the same result as above.

    AffineTransform af = new AffineTransform();
    af.scale(zoomLevel, zoomLevel);

    AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage result  = operation.filter((BufferedImage) image, null);
    return result;

Any suggestion? Thanks in advance guys.

  • 1
    One step scaling very rarely ends well. Take a look at [this for an example](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) for a multi-step solution – MadProgrammer Jun 02 '15 at 04:02
  • 1
    Note that [`AffineTransformOp.TYPE_BICUBIC`](http://docs.oracle.com/javase/8/docs/api/java/awt/image/AffineTransformOp.html#TYPE_BICUBIC) would be expected to result in a smoother image than `AffineTransformOp.TYPE_NEAREST_NEIGHBOR`! – Andrew Thompson Jun 02 '15 at 04:36

0 Answers0