如何使用JLayered窗格在另一个图像上显示图像? [英] How to use JLayered Pane to display an image on top of another image?

查看:154
本文介绍了如何使用JLayered窗格在另一个图像上显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一款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:\\\\\\\\\\\\\\\\\\\\\'') PNG);

JLabel pacmansprite = new JLabel(sprite);
JLabel background1 =新的JLabel(背景);

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

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


解决方案

我认为您应该只添加一个画布 JLayeredPane ,并使用 java2D (在 java.awt.Graphics java.awt.Graphics2D 。有关详细信息,请参阅。)。比尝试使用 Graphics2D 方法 drawImage(BufferedImage img,int x,int y,int width,int hight); 。这可能很复杂,但它可能会更好。



我希望这可以工作!


We are working on a pacman game and we want to add a picture of pacman on top of the background. If someone could provide some example code of implementing the Jlayered Pane that would be great.

Here is some of the code we attempted to write. When we try to run it, nothing displays:

  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));

解决方案

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!

这篇关于如何使用JLayered窗格在另一个图像上显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆