11

here is my code"

ImageIcon ii=new ImageIcon("/Users/tushar_chutani/Desktop/apple.jpg");  

Image image= ii.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);

the image is not being scaled what is wrong with the code?

azro
  • 53,056
  • 7
  • 34
  • 70
Tushar Chutani
  • 1,522
  • 5
  • 27
  • 57
  • 2
    What errors are you getting? Can you actually display the original ImageIcon? How do you know is not scaling. Based on the two lines of code you posted you don't do anything with the image. Post your [SSCCE](http://sscce.org) that demonstrates the problem. – camickr Aug 31 '11 at 05:44
  • 1
    the image isn't showing...the image changes to white... – Tushar Chutani Aug 31 '11 at 05:46
  • 2
    Then it probably didn't read the image. You didn't post a SSCCE, so there is not much else we can do for you. – camickr Aug 31 '11 at 05:56
  • 1
    does it work with another image. Some problems were reported here with some jpeg due to their color model. – Snicolas Aug 31 '11 at 06:12
  • 1
    it doesn't work with any other files either – Tushar Chutani Aug 31 '11 at 07:07

3 Answers3

18

The problem is that Image.getScaledInstance() does not return a finished, scaled image. It leaves much of the scaling work for a later time when the image pixels are used.

For example, if you use the scaled image in a Graphics2D.drawImage() call then the method will return false and continue drawing asynchronously. You then have to use the ImageObserver parameter in the Graphics2D.drawImage() call to wait for completion of the scaling and drawing.

The following example shows how to scale images more simply without an ImageObserver. The scaling is done by drawing the icon into a BufferedImage instead.

import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Color;
import java.awt.Graphics2D;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.RenderingHints;

public class Tushar2
{
        public void scaleImage()
        {
                try
                {
                        ImageIcon ii = new ImageIcon("/tmp/apple.jpg");
                        BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
                        Graphics2D g2d = (Graphics2D)bi.createGraphics();
                        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,
                                RenderingHints.VALUE_RENDER_QUALITY));
                        boolean b = g2d.drawImage(ii.getImage(), 0, 0, 50, 50, null);
                        System.out.println(b);
                        ImageIO.write(bi, "jpg", new File("/tmp/apple50.jpg"));
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
        }

        public static void main(String []args)
        {
                new Tushar2().scaleImage();
        }
}
Simon C
  • 1,977
  • 11
  • 14
7

You can wrap the image in an image icon again. An image icon is generally loading the image in its constructor and uses its own media tracker for this purpose. I am using the following code now:

     Image image = icon.getImage().getScaledInstance(
          icon.getIconWidth() * NEW / OLD,
          icon.getIconHeight() * NEW / OLD,
          Image.SCALE_SMOOTH);
     icon = new ImageIcon(image, icon.getDescription());

You can directly work with the new icon, or call getImage() to work with the new image. The above code does also an aspect ratio preserving scaling given on some NEW and OLD pair.

Bye

  • 1
    The accepted answer works good, but I think this answer's a lot easier to understand, thanks for the insight! – Kcits970 Jan 19 '20 at 12:28
1

Your code:

ImageIcon ii=new 
ImageIcon("/Users/tushar_chutani/Desktop/apple.jpg");  

Add this one:

Image image= ii.getImage().getScaledInstance(50, 50, 
Image.SCALE_SMOOTH);
ii=new ImageIcon(image);

IF YOU WANT TO display : Add this..

jLabel1.setIcon(ii);