【问题标题】:How to fix the blurry image when I run my java swing project当我运行我的 java swing 项目时如何修复模糊的图像
【发布时间】:2021-11-02 02:41:57
【问题描述】:

我目前正在学习 java swing builder 作为一项新技术技能。我需要研究如何在我的画布上导入图像。是的,它成功导入,图像清晰定义且不模糊。 但是,当我运行项目时,导入到项目中的图像变得模糊。我不知道为什么会这样。我研究了一些功能,例如缩放,但没有任何反应,它的输出相同,变得模糊。

图片尺寸:250x250

这是我在项目中实现的代码:

private Image doctor_ppe = new ImageIcon(LoginScreen.class.getResource("/assets/large.png")).getImage().getScaledInstance(250, 250, Image.SCALE_SMOOTH);


JLabel lblDds = new JLabel("");
    lblDds.setVerticalAlignment(SwingConstants.BOTTOM);
    lblDds.setHorizontalAlignment(SwingConstants.CENTER);
    lblDds.setIcon(new ImageIcon(doctor_ppe));
    lblDds.setBounds(59, 305, 236, 251);
    panel.add(lblDds);

不可运行项目和可运行项目的区别:

图像不模糊:

运行项目后图像模糊:

希望有人能体验到这一点,希望对我的问题有所帮助 谢谢。

【问题讨论】:

    标签: java swing


    【解决方案1】:

    但是,当我运行项目时,我导入到项目中的图像变得模糊。

    可能是因为您的桌面使用了大于 1.0 的缩放因子,所以图像在绘制时被放大了。

    如果您想更改整个应用程序的缩放比例,您可以尝试:

    1. 使用命令行-Dsun.java2d.uiScale=1.0,或
    2. 使用System.setProperty("sun.java2d.uiScale", "1.0")以编程方式设置它

    另一种选择可能是仅阻止 Icon 缩放:

    import java.awt.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
    
    
    public class NoScalingIcon implements Icon
    {
        private Icon icon;
    
        public NoScalingIcon(Icon icon)
        {
            this.icon = icon;
        }
    
        public int getIconWidth()
        {
            return icon.getIconWidth();
        }
    
        public int getIconHeight()
        {
            return icon.getIconHeight();
        }
    
        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            Graphics2D g2d = (Graphics2D)g.create();
    
            AffineTransform at = g2d.getTransform();
    
            int scaleX = (int)(x * at.getScaleX());
            int scaleY = (int)(y * at.getScaleY());
    
            int offsetX = (int)(icon.getIconWidth() * (at.getScaleX() - 1) / 2);
            int offsetY = (int)(icon.getIconHeight() * (at.getScaleY() - 1) / 2);
    
            int locationX = scaleX + offsetX;
            int locationY = scaleY + offsetY;
    
            //  Reset scaling to 1.0 by concatenating an inverse scale transfom
    
            AffineTransform scaled = AffineTransform.getScaleInstance(1.0 / at.getScaleX(), 1.0 / at.getScaleY());
            at.concatenate( scaled );
            g2d.setTransform( at );
    
            icon.paintIcon(c, g2d, locationX, locationY);
    
            g2d.dispose();
        }
    
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        public static void createAndShowGUI()
        {
            JButton button = new JButton( "Button" );
            NoScalingIcon icon = new NoScalingIcon( new ImageIcon("box.jpg") );
            button.setIcon( icon );
    
            JPanel panel = new JPanel( );
            panel.add( button );
    
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(panel);
            f.setSize(200, 200);
            f.setLocationRelativeTo( null );
            f.setVisible(true);
        }
    }
    

    【讨论】:

    • 这个属性:System.setProperty("sun.java2d.uiScale", "1.0") 解决了我的问题,但是表单变小了。?
    • 正确。如果缩放到 1.0,则整个表单会变小。如果你使用 NoScallingIcon 那么只有图标会更小。
    • 此代码是否假设 2 倍缩放?有没有办法查询实际的比例因子?
    • @Charlie,它不假设任何东西。它从 Graphics AffineTransform 对象获取当前缩放因子,然后反转缩放因子,因此当您将它们连接在一起时,结果值为 1,因此图标被绘制而没有任何缩放。
    • 系统缩放是否显示为与系统缩放匹配的 Graphics2D 上的默认 afine 变换?
    猜你喜欢
    • 2019-12-31
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2013-05-15
    • 2015-02-09
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多