【问题标题】:How to use JLayered Pane to display an image on top of another image?如何使用 JLayered Pane 在另一个图像之上显示图像?
【发布时间】:2014-06-08 09:38:28
【问题描述】:

我们正在开发一款吃豆人游戏,我们想在背景上添加一张吃豆人的图片。 如果有人可以提供一些实现 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));

【问题讨论】:

  • 对于这种情况,您最好在单个组件中使用painting things yourself。 Swing 组件层次结构实际上是用于构建带有按钮、文本字段和类似内容的 UI。
  • 你没有接受上一个问题 (stackoverflow.com/q/23061863/131872) 的任何答案,所以我想我会跳过这个。

标签: image swing graphics jlayeredpane pacman


【解决方案1】:

我认为您应该只向JLayeredPane 添加一个画布,然后使用java2D 绘制图像(在java.awt.Graphicsjava.awt.Graphics2D 中找到。有关这方面的更多信息可用。)。然后,尝试使用Graphics2D 方法drawImage(BufferedImage img, int x, int y, int width, int hight);。这可能很复杂,但它可能会更好。

我希望这行得通!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2018-10-04
    • 2015-07-24
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多