Skip to main content

getScaledInstance

3 replies [Last post]
nitinchauhan
Offline
Joined: 2004-11-07
Points: 0

Hello Forum !

Profiling one of our app I spotted Image.getScaledInstance() taking more time ! searched on web and found this:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6196792

I wrote utility method as follows for getting the same result as Image.getScaledInstance() but should be faster. it does not work :( shows half image in specified region !

public static javax.swing.ImageIcon getScaledImage(javax.swing.ImageIcon icon, int width, int height)
{
// maintain the aspect ratio as in Image.getScaledInstance()

int iconWidth = icon.getIconWidth();
int iconHeight = icon.getIconHeight();
if (width < 0) {
if (height < 0) {
width = iconWidth;
height = iconHeight;
}
else {
width = iconWidth * height / iconHeight;
}
}
else if (height < 0) {
height = iconHeight * width / iconWidth;
}

java.awt.Image image = icon.getImage();
java.awt.image.BufferedImage bi = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);

java.awt.Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(java.awt.RenderingHints.KEY_INTERPOLATION, java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();

javax.swing.ImageIcon iImage = new javax.swing.ImageIcon(bi);
return iImage;
}

Any ideas? Thank you.
Nitin

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
j2d_lover
Offline
Joined: 2006-08-02
Points: 0

Hi Nitin,

I can not tell you exactly without looking at the gif image. But if you want to change the size of animated gifs, you can not use the scaling method that you have written. It will only scale the first frame of the image. You can use Image.getScaledInstance() instead but it may have performance implications.

If you are using ImageIcon as it is (not the image directly) and if you want the icon to be drawn to the size specified by you, you can actually extend ImageIcon class and create your own icon. Include a setSize() method and override the paintIcon(), getIconWidth(),getIconHeight() methods of ImageIcon to draw the image to the specified size. I have given a small example below:

class MyIcon extends ImageIcon {

int width, height;

public MyIcon(String fileName) {
super(fileName);
}

public void paintIcon(Component c, Graphics g, int x, int y) {
if (getImage() != null) {
g.drawImage(getImage(), x, y, getIconWidth(), getIconHeight(), c);
}
}

public void setSize(int w, int h) {
width = w;
height = h;
}

public int getIconWidth() {
return width;
}

public int getIconHeight() {
return height;
}
}

Then you can create the icon as follows:
MyIcon icon = new MyIcon(fileName);
icon.setSize(width, height);
jlabel.setIcon(icon);

This will draw the icon to the required size. But remember that the image represented by this icon would still have the original size.
Not sure if this is what you are looking for.

j2d_lover
Offline
Joined: 2006-08-02
Points: 0

Hi Nitin,

You seem to be not resizing the image when drawing into the buffered image.

Change this
g2d.drawImage(image, 0, 0, null);
to
g2d.drawImage(image, 0, 0, width, height, null);

Also take a look at this, if you would like to preserve the alpha channel.
http://java.sun.com/products/java-media/2D/reference/faqs/index.html#Q_H...

nitinchauhan
Offline
Joined: 2004-11-07
Points: 0

Thank you for quick reply.

Discovered I was testing with animated gif.

Now, It works fine with all but animated gif files.
If I pass width,height, Insted of showing first frame it draws red filled rectangle. if I do not pass width,height it draws half of first frame :(