gpt4 book ai didi

java - 调整应用程序窗口大小时如何调整应用程序内图像的大小?

转载 作者:行者123 更新时间:2023-12-03 18:11:05 29 4
gpt4 key购买 nike

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

编辑:我正在使用 BufferedImage

最佳答案

这是一个开放式问题。

您想缩放以填充还是缩放以适合该区域,或者您不关心纵横比?

scale to fill 和 scale to fit 的区别

enter image description here enter image description here

此示例将对帧大小的变化使用react并实时重新缩放图像。

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);
}
}
}

更好的解决方案是拥有某种后台线程,它可以对组件大小的变化使用react,并在后台重新缩放原始图像,提供低质量和高质量缩放。

还应注意 Image.getScaledInstance 既不是最快的也不是最高质量的缩放算法。看看The Perils of Image.getScaledInstance获取更多信息。

您可能还会对以下内容感兴趣

Java: maintaining aspect ratio of JPanel background image

关于java - 调整应用程序窗口大小时如何调整应用程序内图像的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876615/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com