我们正在制作一个pacman游戏,我们想在背景上添加pacman的图片。 如果有人可以提供一些实现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));
答案 0 :(得分:0)
我认为您应该向JLayeredPane
添加一个画布,然后使用java2D
(在java.awt.Graphics
和java.awt.Graphics2D
中找到图像。)这是可用的。)然后,尝试使用Graphics2D
方法drawImage(BufferedImage img, int x, int y, int width, int hight);
。这可能很复杂,但可能会更好。
我希望这有效!