[英]How can I get my JLabel Icon Images to be high resolution?
我想在其角落制作一個帶有小 jLabel(50x50) 的應用程序。
我現在遇到的問題是 Label 顯示的圖像看起來非常糟糕。
我還在桌面上的 windows 的快捷方式中添加了相同的圖像作為圖標,作為比較。
左側為 Windows,右側為 Java JLabel。
如何在 Jave 中存檔類似的縮放結果而不損失質量?
它不需要使用 JLabel。
代碼:
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
使用奇怪的分辨率圖像編輯在情況 2 中,確保您用於從中獲取尺寸的JComponent
(您調用getWidth
和getHeight
的那個)具有相同的寬度和高度尺寸。
我剪切了您的左側圖像,寬度/高度為 62 像素。 第一行顯示縮放的圖像,第二行顯示當我首先在圖形程序中將源圖像縮小到 32px 時發生的情況:
尺寸,如下所示,go 從 62px 向上增加 10px。 代碼在 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);
}
}
您可以在BufferedImage
中使用,這比JLabel
分辨率要高得多。 您可以使用ImageIO
class 從.png 文件創建BufferedImage
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.