-1

I can not find a way to set the size of an ImageIcon placed in a JLabel. I i.e. want to load image with size 32x32, but paint in size of 16x16 pixel (or 144 dpi).

I can no where find a way to set the Icons size to 16x16. I tried to save the png with double DPI, but that gets ignored.

There has to be a way to draw Icons with other dimensions (like 2x - 2px for 1px, works well for images) too, but I don't see it. Thx!

I can not find a setter for width/height, dimension, dpi or anything similar.

5
  • 1
    the ImageIcon can be constructed based on an Image, and an Image has the getScaledInstance() method
    – user85421
    Commented Jul 3 at 11:22
  • 1
    Might be worth trying to override getIconWidth and getIconHeight. Not sure of knock-on effects if that works
    – g00se
    Commented Jul 3 at 11:39
  • Maybe you can edit your question and post a minimal reproducible example? I mean, post a Swing application that displays a JLabel and that JLabel contains an icon of the image that you are using.
    – Abra
    Commented Jul 3 at 11:49
  • @user85421 AFAIK, the getScaledInstance()method actually scales the image down (so reduces the pixelcount, if going from 32x32 to 16x16), same as directly using a 16x16 image and not getting a sharper image, when UI is scaled up. It makes me we wonder, if there is nothing provided for hidpi or retina screens.
    – Stuepfnick
    Commented Jul 3 at 13:46
  • You can never scale up to get a better image. You will always get pixelation. You you can scale down and you can control the method of scaling to go for quality or speed etc. If you only want a fixed size image then you should use the Image.getScaledInstance as it will be more efficient since you only do the scaling once. Creating a custom scaling icon should be used when you need dynamic scaling since it will be done every time a component is repainted.
    – camickr
    Commented Jul 3 at 13:49

1 Answer 1

0

Nevermind, I found a way: Just create a class ScaledIcon which implements the Icon Interface, takes the image, width and height as constructor, then implemented my own paintIcon method, which draws the icon with the preferred width and height (as set in Constructor) Usage: JLabel label = new JLabel("Some Text", new ScaledIcon(originalIcon.getImage(), 16, 16), SwingConstants.HORIZONTAL);

Draws the Icon fine with higher DPI, works as expected (i.e. if UI is scaled to 200%), I get the desired result, finally a crisp icon (when loading a 32x32px version scaled to 16x16px).

ScaledIcon class:

public class ScaledIcon implements Icon {
    private final Image image;
    private final int width;
    private final int height;

    public ScaledIcon(Image image, int width, int height) {
        this.image = image;
        this.width = width;
        this.height = height;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.drawImage(image, x, y, width, height, c);
    }

    @Override
    public int getIconWidth() {
        return width;
    }

    @Override
    public int getIconHeight() {
        return height;
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.