0

我们正在开发一款吃豆人游戏,我们想在背景上添加一张吃豆人的图片。如果有人可以提供一些实现 Jlayered Pane 的示例代码,那就太好了。

这是我们尝试编写的一些代码。当我们尝试运行它时,没有任何显示:

  JLayeredPane pacman = new JLayeredPane();  

  pacman.setPreferredSize(new Dimension(576, 655));





  ImageIcon sprite = new ImageIcon("C:\\\\Users\\\\16ayoubc\\\\Desktop\\\\Pacman-moving.gif");  
  ImageIcon background = new ImageIcon("C:\\\\Users\\\\16ayoubc\\\\Desktop\\\\background.png");  

  JLabel pacmansprite = new JLabel(sprite);
  JLabel background1 = new JLabel(background);

  background1.setVisible(true);
  pacman.setLocation(255, 255);
  pacman.setVisible(true);
  pacman.setOpaque(true);

  pacman.add(background1, new Integer(0));
4

1 回答 1

0

I think that you should just add a canvas to the JLayeredPane, and than paint the image using java2D (found in java.awt.Graphics, and java.awt.Graphics2D. More info on this is available.). than, try using the Graphics2D method drawImage(BufferedImage img, int x, int y, int width, int hight);. This may be complicated, but it might work better.

I hope this works!

于 2014-04-25T22:33:02.407 回答