是什么导致 Java JLabel 图标图像质量差?

2023-11-23

Java JLabel图标显示的像素扭曲JFrame。不同的 png 图像(均为 32x32)都会出现这种情况。我没有缩放图像,它们在程序中显示为 32x32,我使用它进行了验证getWidth and getHeight在 JLabel 上。每次程序运行时,扭曲都会出现在同一位置,而不是随机的。

Screenshot using the example code provided below.
example screenshot (1)

In this screenshot you can see an array of JLabel icons, each affected one differently.
example screenshot (2)

从侧面调整窗口大小时,随着图标随窗口移动,扭曲会像垂直线一样在图标上移动。

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class FrameApp extends JFrame
{
    public static void main(String[] args) throws IOException
    {
        FrameApp frameApp = new FrameApp();
    }

    private FrameApp() throws IOException
    {
        BufferedImage image;

        URL url = new URL("https://i.stack.imgur.com/L5DGx.png");
        image = ImageIO.read(url);

        JLabel label = new JLabel(new ImageIcon(image));

        add(label);

        pack();
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

Edit:
我在 Windows 8.1 64 位上使用 JDK 11.0.3、Java SE 运行时环境版本 1.8.0_202。


您可能认为您正在以 32x32 大小显示图像,但平铺图像的示例表明事实并非如此。您有一个 9x2 的图标网格,应为 288x64 像素,但在示例图像中网格为 302x66。

如果仔细检查平铺图像,您可以看到各个平铺图像的显示宽度为 34 像素 - 请参阅从 32 像素延伸到 66 像素的洋红色边框。 (请注意,某些图块显示的宽度为 33 像素;看起来为 33、34、34、33、34...)

为了将图块拉伸到更宽的宽度,某些列被加倍(红色边框),这会产生您所看到的视觉故障。

marked up portion of provided sample image

您是否尝试过修复 JLabel 的大小,而不是让它根据其内容调整大小?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是什么导致 Java JLabel 图标图像质量差? 的相关文章

随机推荐