调整图像大小并同时调整jPanel大小时保持纵横比

时间:2018-04-12 16:23:20

标签: java swing

我正在尝试通过计算原始图像的宽度百分比和高度百分比来维持宽高比,然后根据百分比设置新的高度和新宽度,但我的代码中有错误:

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

public class Test extends JFrame {
private JPanel principalPanel;
private BufferedImage image;
public Test() {
    setSize(600,600);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    try {
        URL url = new 
       URL("https://boygeniusreport.files.wordpress.com/2017/04/earth.jpg");
        image = ImageIO.read(url);
    } catch (Exception e) {
        System.out.println("Problem loading image");
        e.printStackTrace();
    }
    principalPanel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int width = image.getWidth();
            int height = image.getHeight();
            int wPercentage = (width*100)/(width+height);
            int hPercentage = (height*100)/(width+height);
            int w = (width+((getHeight()-height)*wPercentage)/hPercentage);
            int h = (height+((getWidth()-width)*hPercentage)/wPercentage);
            g.drawImage(image,0,0,w,h,this);
        }
    };
    setContentPane(principalPanel);
    setVisible(true);
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Test();
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

以下是解决方案:

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

public class Test extends JFrame {
private JPanel principalPanel;
private BufferedImage image;
public Test() {
    setSize(600,600);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    try {
        URL url = new
                URL("https://boygeniusreport.files.wordpress.com/2017/04/earth.jpg");
        image = ImageIO.read(url);
    } catch (Exception e) {
        System.out.println("Problem loading image");
        e.printStackTrace();
    }
    principalPanel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int original_width = image.getWidth();
            int original_height = image.getHeight();
            int bound_width = getWidth();
            int bound_height = getHeight();
            int new_width = original_width;
            int new_height = original_height;

            // first check if we need to scale width
            if (original_width > bound_width) {
                //scale width to fit
                new_width = bound_width;
                //scale height to maintain aspect ratio
                new_height = (new_width * original_height) / original_width;
            }

            // then check if we need to scale even with the new height
            if (new_height > bound_height) {
                //scale height to fit instead
                new_height = bound_height;
                //scale width to maintain aspect ratio
                new_width = (new_height * original_width) / original_height;
            }
            g.drawImage(image,0,0,new_width,new_height,this);
        }
    };
    setContentPane(principalPanel);
    setVisible(true);
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Test();
        }
    });
}
}