如何在 JPanel 中居中对齐背景图像

发布于 2024-10-09 12:47:52 字数 734 浏览 0 评论 0原文

我想将背景图像添加到我的 JFrame 中。
背景图像意味着我可以稍后在 JFrameJPanel 上添加组件
虽然我找不到如何向 JFrame 添加背景图像,
我从这里找到了如何将背景图像添加到 JPanel
如何在Java中设置背景图像?

这解决了我的问题,但是现在,由于我的 JFrame 可以调整大小,我想将图像保持在中心位置。
我找到的代码使用了这个方法

public void paintComponent(Graphics g) { //Draw the previously loaded image to Component.  
    g.drawImage(img, 0, 0, null);   //Draw image
}  

谁能说一下如何将图像对齐到 JPanel 的中心。
由于 g.drawImage(img, 0, 0, null); 提供 x=0 和 y=0
另外,如果有一种方法可以将背景图像添加到 JFrame 那么我想知道。
谢谢。

I wanted to add background image to my JFrame.
Background image means I can later add Components on the JFrame or JPanel
Although I coudn't find how to add background image to a JFrame,
I found out how to add background image to a JPanel from here:
How to set background image in Java?

This solved my problem, but now since my JFrame is resizable I want to keep the image in center.
The code I found uses this method

public void paintComponent(Graphics g) { //Draw the previously loaded image to Component.  
    g.drawImage(img, 0, 0, null);   //Draw image
}  

Can anyone say how to align the image to center of the JPanel.
As g.drawImage(img, 0, 0, null); provides x=0 and y=0
Also if there is a method to add background image to a JFrame then I would like to know.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-10-16 12:47:52

假设有一个合适的图像,您可以像这样将其居中:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    int x = (this.getWidth() - image.getWidth(null)) / 2;
    int y = (this.getHeight() - image.getHeight(null)) / 2;
    g2d.drawImage(image, x, y, null);
}

如果您希望其他组件随背景移动,您可以更改图形上下文的仿射变换以保持图像居中,如下所示包含旋转的完整示例

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
    g2d.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
    g2d.drawImage(image, 0, 0, null);
}

Assuming a suitable image, you can center it like this:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    int x = (this.getWidth() - image.getWidth(null)) / 2;
    int y = (this.getHeight() - image.getHeight(null)) / 2;
    g2d.drawImage(image, x, y, null);
}

If you want the other components to move with the background, you can alter the graphics context's affine transform to keep the image centered, as shown in this more complete example that includes rotation.

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
    g2d.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
    g2d.drawImage(image, 0, 0, null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文