如何在Java中将图像带到前台? [英] How to bring an image to the foreground in Java?

查看:89
本文介绍了如何在Java中将图像带到前台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java创建Pacman游戏,到目前为止我已经展示了背景图像(蓝色和黑色的迷宫),但是我无法展示Pacman的图像。当我用与显示背景相同的方法尝试显示他时,除非我稍微手动更改Jframe的大小,否则他不会出现。即使如此,当他出现时,他的图像右下角有一个白色的小方块。我能做些什么来解决这个问题?有没有其他的方法可以插入pacman图片?



这是代码:

  JFrame window = new JFrame(); 

ImageImplement pacman = new ImageImplement(new ImageIcon(C:\\\\ Users \\\\\\\\\\\\\\\\\\\\\\\\'')。getImage() );
ImageImplement panel = new ImageImplement(new ImageIcon(C:\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\')。getImage());

pacman.setLocation(255,255);
pacman.setVisible(true);
pacman.setOpaque(true);
window.add(pacman);
window.add(panel);

window.setVisible(true);
window.setSize(576,655);
window.setName(Pacman);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


解决方案

直接使用窗口的JLayeredPane作为容器并在添加组件时指定组件的深度(z顺序)。


I'm trying to create a Pacman game in Java, and so far I've displayed the background image (the blue and black maze), but I'm having trouble showing the image of Pacman. When I try to display him in the same method with which I displayed the background, he doesn't appear unless I slightly manually alter the size of the Jframe. And even then when he appears there's a small white square in the bottom right corner of his image. What can I do to fix this? Is there any other way I can insert the pacman image in that works?

This is the code:

  JFrame window = new JFrame();

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

  pacman.setLocation(255, 255);
  pacman.setVisible(true);
  pacman.setOpaque(true);
  window.add(pacman);
  window.add(panel);

  window.setVisible(true);
  window.setSize(576,655);
  window.setName("Pacman");
  window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

解决方案

Use a JLayeredPane insted of the window directly for a container and specify the depth (z-order) of the components when you add them.

这篇关于如何在Java中将图像带到前台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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