是什么导致Java JLabel图标的图像质量差? [英] What causes poor image quality in Java JLabel icons?

查看:193
本文介绍了是什么导致Java JLabel图标的图像质量差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java JLabel图标在JFrame中显示时像素变形.对于不同的png图像(全部为32x32),这会始终发生.我没有缩放图像,它们显示在程序32x32中,我在JLabel上使用getWidthgetHeight进行了验证.每次运行程序时,变形会出现在同一位置,而不是随机出现.

Java JLabel icons are displaying with distorted pixels in JFrame. This is happening consistently with different png images (all 32x32). I am not scaling the images, they are displayed in the program 32x32, which I verified using getWidth and getHeight on the JLabel. The distortions appear in the same place each time the program is run, not randomly.

使用下面提供的示例代码截屏.

Screenshot using the example code provided below.

在此屏幕快照中,您可以看到一系列JLabel图标,每个图标的影响方式不同.

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

从侧面调整窗口大小时,随着图标与窗口一起移动,变形会像垂直线一样在图标上移动.

When resizing the window from sideways, as the icon moves with the window, the distortions move across the icon like a vertical line.

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("http://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);
    }
}

修改:
我正在Windows 8.1 64位上使用JDK 11.0.3,Java SE Runtime Environment构建1.8.0_202.


I am using JDK 11.0.3, Java SE Runtime Environment build 1.8.0_202, on Windows 8.1 64-bit.

推荐答案

您可能会认为您以32x32的大小显示图像,但是平铺图像的示例却并非如此.您有一个9x2的图标网格,应为288x64像素,但在示例图像中,该网格为302x66.

You may think you're displaying the images at 32x32 size, but your example of the tiled images says that's not so. You have a 9x2 grid of icons, which should be 288x64 pixels, but in your sample image the grid is 302x66.

如果您仔细检查平铺的图像,可以看到单个平铺的显示宽度为34px-洋红色边框从32px扩展到66px. (请注意,某些磁贴显示为33px宽;它看起来是33、34、34、33、34 ...)

If you carefully examine your tiled image, you can see that the individual tiles are being displayed 34px wide - see the magenta border that extends from 32px to 66px. (Note, some of the tiles are displayed 33px wide; it appears to be 33, 34, 34, 33, 34...)

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

In order to stretch the tiles to the wider width, certain columns are being doubled (red borders) and this creates the visual glitches you are seeing.

您是否尝试过固定JLabel的大小,而不是根据其内容来调整其大小?

Have you tried fixing the size of the JLabel instead of allowing it to size based on its contents?

这篇关于是什么导致Java JLabel图标的图像质量差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆