Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

java - How do I resize images inside an application when the application window is resized?

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?

EDIT: I am using BufferedImage

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's an open ended question.

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

enter image description here enter image description here

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.

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: maintaining aspect ratio of JPanel background image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...