Java Tips Weblog

  • Blog Stats

    • 2,076,564 hits
  • Categories

  • Archives

  • Advertisements

Stretch Icon

Posted by Darryl Burke on March 31, 2012

An implementation of the Icon interface reports its size via the two methods getIconWidth() and getIconHeight().  It’s normal to expect that the Icon’s paintIcon(Component, Graphics, int, int) method will respect these bounds.  If I had designed Swing’s interaction with Icons, I would have made sure of that by setting a clip to the Graphics passed to paintIcon(…).

Luckily for me, I didn’t design Swing, as that would have made StretchIcon impossible.

StretchIcon is an Icon that scales its image to fill the component area, excluding any border or insets, optionally maintaining the image’s aspect ratio by padding and centering the scaled image horizontally or vertically.  Since the component now determines the size of the Icon, rather than the other way round, StretchIcon can be used only in conjunction with a component whose size is determined by the size and layout of the container it is placed in; for example, a JLabel placed in the BorderLayout.CENTER of a JFrame.

Using a StretchIcon is a viable alternative to extending a JComponent or JPanel solely to paint a custom background.  The class can also improve the aesthetics of JLabels or JButtons in a resizable GridLayout, as in a game board. For both use cases, the image chosen should be of a size equal or near the maximum anticipated size of the component, since drawing an image larger than its natural size can lead to pixelation.

StretchIcon is a drop-in replacement for ImageIcon, which it extends, except that ImageIcon’s no-arg constructor isn’t supported. That’s because I feel that an ImageIcon without an Image is an oxymoron.

Get The Code

See Also

Shrink Icon
Thumbnail Icon

Related Reading

Java API: javax.swing.Icon
Java API: javax.swing.ImageIcon
The Java™ Tutorials: How to Use Icons


9 Responses to “Stretch Icon”

  1. Gildas said

    That worked great, thank you!

  2. Bill h said

    So worked great on windows, exactly what I needed. Having a little issue on Mac OS X though. It seems that when you press and hold on a button with the StretchIcon in it, there is a phantom image in the background that becomes visible. It seems like it is something from ImageIcon that maybe the StretchIcon doesn’t take into account?!?! It’s a bit strange….trying to see if I can figure out a work around currently. Thanks again though for the original code!!

    • Bill H said

      Tried to fix it and boiled it down to the MacOS System Look and Feel. No other OS or L&F caused this issue (tried Linux, Metal, Nimbus, Motif, Windows 8). For now I just switched those buttons to be Nimbus when the OS is Mac as those are pretty close looking. Would love to figure this out, but couldn’t figure out how to dig deeper into the ImageIcon class and the L&F behavior when a JButton is pressed.

  3. bspkrs said

    Thanks for making this, saved me from having to figure out the math myself! I did make one small alteration that is highlighted in this gist:

    This change produces better quality scaled images :D

  4. Thanks for sharing your code. Could you please add a suitable license to it, so it can be used by others without worry?

  5. if (proportionate) {
    int iw = image.getWidth(c);
    int ih = image.getHeight(c);

    if (iw * h < ih * w) {
    iw = (h * iw) / ih;
    x += (w – iw) / 2;
    w = iw;
    } else {
    ih = (w * ih) / iw;
    y += (h – ih) / 2;
    h = ih;

    Can you please explain this math logic ? i tried to understand it but couldn't.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: