在调整应用程序窗口大小时,如何在应用程序内调整图像大小? [英] How do I resize images inside an application when the application window is resized?

查看:114
本文介绍了在调整应用程序窗口大小时,如何在应用程序内调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我最大化我的应用程序时,JPanel内部的图像不会随之调整大小。如何在窗口最大化时调整JPanel及其内容?

When I maximize my application, the images inside of the JPanel don't resize with it. How can I make the JPanel and its contents adjust when the window is maximized?

编辑:我正在使用BufferedImage

I am using BufferedImage

推荐答案

这是一个开放式问题。

你想缩放以填充或缩放以适应该区域或不你关心的是宽高比吗?

Do you want to scale to fill or scale to fit the area or don't you care about the aspect ratio??

缩放到填充和缩放之间的差异

The difference between scale to fill and scale to fit


此示例将对帧大小的变化作出反应,并实时重新缩放图像。

This example will react to changes in the size of the frame and rescale the image in real time.

public class TestScaling {

    public static void main(String[] args) {
        new TestScaling();
    }

    public TestScaling() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ScalingPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class ScalingPane extends javax.swing.JPanel {

        private BufferedImage image;

        public ScalingPane() {
            try {
                image = ImageIO.read(getClass().getResource("/AtDesk.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }    
            setBackground(Color.red);
        }

        public double getScaleFactor(int iMasterSize, int iTargetSize) {    
            double dScale = 1;
            dScale = (double) iTargetSize / (double) iMasterSize;

            return dScale;    
        }

        public double getScaleFactorToFit(Dimension original, Dimension toFit) {    
            double dScale = 1d;

            if (original != null && toFit != null) {    
                double dScaleWidth = getScaleFactor(original.width, toFit.width);
                double dScaleHeight = getScaleFactor(original.height, toFit.height);

                dScale = Math.min(dScaleHeight, dScaleWidth);
            }    
            return dScale;
        }

        public double getScaleFactorToFill(Dimension masterSize, Dimension targetSize) {
            double dScaleWidth = getScaleFactor(masterSize.width, targetSize.width);
            double dScaleHeight = getScaleFactor(masterSize.height, targetSize.height);

            double dScale = Math.max(dScaleHeight, dScaleWidth);

            return dScale;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            double scaleFactor = Math.min(1d, getScaleFactorToFit(new Dimension(image.getWidth(), image.getHeight()), getSize()));
//            double scaleFactor = Math.min(1d, getScaleFactorToFill(new Dimension(image.getWidth(), image.getHeight()), getSize()));

            int scaleWidth = (int) Math.round(image.getWidth() * scaleFactor);
            int scaleHeight = (int) Math.round(image.getHeight() * scaleFactor);

            Image scaled = image.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH);

            int width = getWidth() - 1;
            int height = getHeight() - 1;

            int x = (width - scaled.getWidth(this)) / 2;
            int y = (height - scaled.getHeight(this)) / 2;

            g.drawImage(scaled, x, y, this);
        }
    }
}

更好的解决方案是有一种背景线程可以对组件大小的变化作出反应,并在后台重新缩放原始图像,提供低质量和高质量的比例。

A better solution would be to have a background thread of some kind that could react to changes in the size of the component and rescale the original image in the background, providing a low quality and high quality scale.

还应该注意, Image.getScaledInstance 既不是最快或最高质量的缩放算法。看看 The Perils有关更多信息,请参阅Image.getScaledInstance

It should also be noted that Image.getScaledInstance is neither the fastest or highest quality scaling algorithim. Take a look at The Perils of Image.getScaledInstance for more information.

您可能还会找到以下感兴趣的内容

You might also find the following of interest

Java:维护JPanel背景图片的宽高比

这篇关于在调整应用程序窗口大小时,如何在应用程序内调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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