gpt4 book ai didi

java - 背景图像帮助,JPanel,JFrame,什么?

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:07 24 4
gpt4 key购买 nike

我想为我正在设计的游戏制作背景图像,但我无法找到获得我想要的效果的正确方法,我想要一个可以在文本后面看到的背景等。基本上让背景覆盖整个 JFrame/JPanel 不仅仅是布局的一部分(例如 BorderLayout.Center)我认为它无论如何都会这样做,但如果它确实这样做,我如何使那些透明的背景以查看后面的背景...

我知道这很令人困惑,但我希望这里有人理解我正在尝试做的事情并可以提供帮助......这是我当前的代码。我一直在研究背景,所以不要读太多我写的内容。

import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;

public class GamePanel extends JPanel {

private JTextPane playertext;
private JTextField wealthstring, currentwealth;

public GamePanel() {

super();
setLayout(new BorderLayout());
setBackground(Game.getBackgroundColor());
Border raised = BorderFactory.createRaisedBevelBorder();
Border lowered = BorderFactory.createLoweredBevelBorder();
setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(4, 4, 4, 4), (BorderFactory.createCompoundBorder(raised, lowered))));
add(northpanel(), BorderLayout.NORTH);
add(eastpanel(), BorderLayout.EAST);


}


private JLabel northpanel() {

Font northfont = new Font("Engravers MT", Font.BOLD, 12);
ImageIcon banner = new ImageIcon("images/banner.png", "North Background");

playertext = new JTextPane();
playertext.setFont(northfont);
playertext.setEditable(false);
playertext.setText("Player: \n" + Game.getName());
playertext.setBackground(Game.getBackgroundColor());
playertext.setBorder(new EmptyBorder(10,10,10,10));

wealthstring = new JTextField("Money: ");
wealthstring.setFont(northfont);
wealthstring.setEditable(false);
wealthstring.setHorizontalAlignment(wealthstring.RIGHT);
wealthstring.setBorder(null);
wealthstring.setBackground(Game.getBackgroundColor());

currentwealth = new JTextField();
currentwealth.setFont(northfont);
currentwealth.setEditable(false);
currentwealth.setHorizontalAlignment(wealthstring.RIGHT);
currentwealth.setBackground(Game.getBackgroundColor());
currentwealth.setBorder(null);
String wealthrounded = String.format("%.2f", Game.getMoney());
currentwealth.setText(wealthrounded);

JPanel wealthtext = new JPanel();
wealthtext.setLayout(new GridLayout(2, 1));
wealthtext.setBackground(Game.getBackgroundColor());
wealthtext.setBorder(new EmptyBorder(10,10,10,10));
wealthtext.add(wealthstring);
wealthtext.add(currentwealth);

JLabel northpanel = new JLabel();
northpanel.setLayout(new BorderLayout());
northpanel.setIcon(banner);
northpanel.add(new JSeparator(), BorderLayout.PAGE_END);
northpanel.add(playertext, BorderLayout.WEST);
northpanel.add(wealthtext, BorderLayout.EAST);
return northpanel;
}

private JPanel eastpanel() {


JButton tab1 = new JButton("Tab 1");
JButton tab2 = new JButton("Tab 2");
JButton tab3 = new JButton("Tab 3");

JPanel easttabs = new JPanel();
easttabs.setLayout(new GridLayout(1, 3));
easttabs.add(tab1);
easttabs.add(tab2);
easttabs.add(tab3);

JPanel eastpanels = new JPanel();
eastpanels.setLayout(new BorderLayout());
eastpanels.setBackground(Game.getBackgroundColor());
eastpanels.add(easttabs, BorderLayout.NORTH);

return eastpanels;
}

}

最佳答案

因此,如果您已经有一个以图像作为背景的 JPanel,并且想要使用组件在其上添加另一个面板,则可以使用不透明度来实现此目的。只有添加到此面板的组件才是可见的。代码如下:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class TransparentPane extends JPanel {

public TransparentPane() {

setOpaque(false);

}

@Override
protected void paintComponent(Graphics g) {


super.paintComponent(g);


Graphics2D g2d = (Graphics2D) g.create();

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0f));

g2d.setColor(getBackground());
g2d.fill(getBounds());

g2d.dispose();

}

}

关于java - 背景图像帮助,JPanel,JFrame,什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27185622/

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