所以我有一个扩展JPanel的“GameCourt”类。此类将覆盖paintComponent代码,以便绘制一些组件(迷宫,角色和一些硬币)。这个JPanel是一个扩展JLayeredPane的类的一部分,它有两个层,一个用于绘制背景(使用扩展JPanel的BackgroundPanel类),另一个用于绘制我想要的所有元素(一个重置按钮,一个标签..)
所以我想要的是在JLayeredPane背景之上的GameCourt,不要绘制它的背景,这样我就能看到漂亮的图像。
我在GameCourt中尝试了setOpaque(false)but then it only displays the background.
I tried setBackground (new Color(0,0,0,0)) but that didn't work.那是因为GameCourt JPanel后面还有另一个blackBackground我不知道它来自哪里。我试过super.setBackground,但没有做任何事。
我尝试在JLayeredPane中更改背景索引,使其高于GameCourt(JPanel),但这没有什么区别。 (这很奇怪,因为它至少应该在GameCourt JPanel上面绘制背景。
另外,我尝试过:在JavaDocs的示例代码中添加(background,new Integer(0)),但是它说“非法参数异常”。
我真的不知道出了什么问题...我认为我误解了JLayeredPane代码,因为当我使用setOpaque(false)但不将背景添加到JLayeredPane时,它会正确显示所有内容。另外,如何更改添加(背景,0)添加(背景,2)没有区别?
这是“GameCourt”代码(简化): m.draw(g),coins.drawCoins(g)等。只是画了一堆png图像。
public class GameCourt extends JPanel {
public GameCourt(JLabel status) {
// setOpaque(false);
setBackground(new Color(0,0,0,0));
// Game constants
public static final int COURT_WIDTH = 1275;
public static final int COURT_HEIGHT = 765;
}
/**
* (Re-)set the game to its initial state.
*/
public void reset() {
playing = true;
status.setText("Running...");
// Make sure that this component has the keyboard focus
requestFocusInWindow();
}
/**
* This method is called every time the timer defined in the constructor
* triggers.
*/
private void tick() {
if (playing) {
repaint();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
m.drawMaze(g);
player.draw(g);
coins.drawCoins(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(COURT_WIDTH, COURT_HEIGHT);
}
}
这是JLayeredPanel代码(也略微简化):
public class LayeredGameCourt extends JLayeredPane {
package Game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class LayeredGameCourt extends JLayeredPane {
private GameCourt court;
public LayeredGameCourt() {
setPreferredSize(new Dimension(GameCourt.COURT_WIDTH, GameCourt.COURT_HEIGHT+90));
setLayout(new BorderLayout());
final JBackgroundPanel background = new JBackgroundPanel();
add(background,0);
background.setBounds(0,0,GameCourt.COURT_WIDTH,GameCourt.COURT_HEIGHT+90);
// Status panel
final JLabel status = new JLabel("Running...");
// Main playing area
final GameCourt court = new GameCourt(status);
this.court=court;
add(court, 1);
court.setBounds(0,90,GameCourt.COURT_WIDTH,GameCourt.COURT_HEIGHT);
// Reset button
final JPanel control_panel = new JPanel();
add(control_panel, 1);
final JButton reset = new JButton("Reset");
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
court.reset();
}
});
control_panel.add(status);
control_panel.add(reset);
// Start game
court.reset();
}
public void requestFocusForGC(){
court.requestFocusInWindow();
}
}
最后,(无用的)backgroundPanel代码,如果它有帮助:
@SuppressWarnings("serial")
public class JBackgroundPanel extends JPanel {
private BufferedImage background;
public JBackgroundPanel() {
try {
if (background == null) {
background = ImageIO.read(new File("background.png"));
}
} catch (IOException e) {
System.out.println("Internal Error:" + e.getMessage());
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0,null);
System.out.println("attempting to paint");
}
@Override
public Dimension getPreferredSize() {
return new Dimension(GameCourt.COURT_WIDTH, GameCourt.COURT_HEIGHT+90);
}
}
答案 0 :(得分:0)
没关系,让它发挥作用!
对于可能遇到同样问题的其他人。以下是我的思考过程。
为什么我不能添加(Background,new Integer(0))?
也许是因为背景不是一个组件?不。 看了一下java文档,意识到他们没有设置布局类型。 也许我的.add调用我添加的BorderLayout的.add,这就是它失败的原因。
所以我删除它并且它有效!!!
所有我需要做的就是发表评论,说出一些我认为需要在那里做出一些重要理由的话。
但它有效!!!!
澄清:这是我删除的行:
setLayout(new BorderLayout());
感谢(如果)有人试图提供帮助!