我现在已经花了将近 2 个小时,但我无法让它工作。我只想在JPanel上绘制图像。
我想imageChaser
在竞技场JPanel 上绘制图像。但它没有显示。我究竟做错了什么?
这是我的代码:
public class GuiGameBoard extends JPanel {
//import stuff
private JPanel arena;
BufferedImage imageChaser;
BufferedImage imageChaserSelected;
BufferedImage imageTarget;
public GuiGameBoard() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
arena = new JPanel();
arena.setPreferredSize(new Dimension(500, 500));
arena.setBackground(Color.BLACK);
this.add(arena);
try
{
File inputChaser = new File("resources\\chaser.png");
imageChaser = ImageIO.read(inputChaser);
File inputChaserSelected = new File("resources\\chaser_selected.png");
imageChaserSelected = ImageIO.read(inputChaserSelected);
File inputTarget = new File("resources\\target.png");
imageTarget = ImageIO.read(inputTarget);
}
catch (IOException ie)
{
System.out.println("Error:"+ie.getMessage());
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imageChaser, 0, 0, null);
}
}