【发布时间】:2015-12-07 06:41:11
【问题描述】:
所以我有一个扩展 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 中的示例代码中执行:add(background, new Integer(0)),但它显示“Illegal Argument Exception”。
我真的不知道出了什么问题...我想我误解了 JLayeredPane 代码,因为当我使用 setOpaque(false) 但不向 JLayeredPane 添加背景时,它会正确显示所有内容。另外,将 add(background,0) 更改为 add(background,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);
}
}
【问题讨论】:
标签: java swing jpanel transparent jlayeredpane