我想创建一个在其角落有一个小的jLabel(50x50)的应用程序。
我现在遇到的问题是标签显示的图像看起来真的很差。
我也将与图标相同的图像添加到了桌面上Windows的快捷方式中,作为比较。
左侧为Windows,右侧为Java JLabel。
如何在Jave中归档类似的缩放结果而又不损失质量?
代码:
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);
我看到两个选项,或者可能是它们的组合:
ImageIcon
使用怪异的分辨率图像EDIT在第2种情况下,请确保用于获取尺寸的JComponent
(您正在调用getWidth
和getHeight
的尺寸)具有相等的宽度和高度尺寸。
我以62像素的宽度/高度剪切了您的左侧图片。第一行显示图像缩放,第二行显示当我首先在图形程序中将源图像缩放到32px时会发生什么:
尺寸,如下所示,从62像素向上增加10像素。代码已在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);
}
}