简体   繁体   中英

How can I get my JLabel Icon Images to be high resolution?

I want to make an application with a small jLabel(50x50) in its corner.

The Problem I now have is that the Image the Label displays is looking really bad.

I also added the same Image as an Icon to a shortcut in windows on my desktop just as a comparison.

Windows on the left side and Java JLabel on the right.

How can I archive a similar scaling result in Jave with no loss in quality?

It does not need to use JLabel.

Code:

    ImageIcon imgIcon = new ImageIcon(path);
    Image img = imgIcon.getImage();
    Image imgScaled = img.getScaledInstance((int) (getWidth()), (int) (getHeight()), 
    Image.SCALE_SMOOTH);

    ImageIcon image = new ImageIcon(imgScaled);
    label.setIcon(image);

EDIT:

If you look at these Google Chrome Icons, they are extremely tiny but still sharp and high resolution, how can I archive this in Java?

I see two options, or maybe a combination of this:

  1. You're using a weird resolution image for your ImageIcon
  2. Ratio of width to height is not equal, thus skewed scaling

EDIT In case 2, make sure the JComponent you're using to fetch dimensions from (the one you're calling getWidth and getHeight on) has equal dimensions for both width and height.

I cut your left image, at 62px width/height. First row shows that image scaled, second row shows what happens when I scale the source image down to 32px in graphics program first:

在此处输入图像描述

Dimensions, as you can see below, go from 62px up by increments of 10px. Code was run on Java 1.8, Windows 10:

void addSeries(Image srcImg, JPanel targetPanel) {
    for (int i = 0; i < 50; i += 10) {
        int dimension = 62 + i;
        Image imgScaled = srcImg.getScaledInstance(dimension, dimension, Image.SCALE_SMOOTH);

        ImageIcon scaledIcon = new ImageIcon(imgScaled);
        JLabel label = new JLabel();
        label.setIcon(scaledIcon);
        targetPanel.add(label);
    }
}

Your can to use in BufferedImage , this is much higher resolution the JLabel . You can to create BufferedImage from.png file with ImageIO class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM