JLabel在另一个JLabel上不起作用 [英] JLabel over another JLabel not working
问题描述
我一直在为我的Roguelike尝试将JLabel放在另一个JLabel上.不幸的是,它似乎不起作用.到目前为止,这是我的代码:
I've been trying to put a JLabel over another one for my Roguelike. Unfortunaly, it doesn't seems to work. Here's my code so far :
public void updateDraw(int direction){
int[] pos = Dungeon.getPos();
for(int i=pos[1]-(DISPLAY_Y_SIZE/2) ; i<pos[1]+(DISPLAY_Y_SIZE/2) ; i++){
for(int j=pos[0]-(DISPLAY_X_SIZE/2) ; j<pos[0]+(DISPLAY_X_SIZE/2) ; j++){
labelGrid[i-(pos[1]-(DISPLAY_Y_SIZE/2))][j-(pos[0]-(DISPLAY_X_SIZE/2))].setIcon(tiles[Dungeon.getMapTile(i,j)].getIcon());
}
}
labelGrid[DISPLAY_Y_SIZE/2][DISPLAY_X_SIZE/2].add(character);
this.repaint();
}
我已经阅读了一些其他问题的解决方案,而所有这些解决方案都是通过将JLabel添加到另一个解决方案来完成的.它不按这里的预期工作,不知道为什么吗?
I've read some solutions to other problems, and they all do it by simply adding the JLabel to the other one. it doesn't work as intended here, any idea why ?
PS:我不想为我的JPanel使用JLayeredPane.
PS : I don't want to use a JLayeredPane for my JPanel.
推荐答案
一个替代方案
请勿使用组件(即JLabels)创建游戏环境.相反,您可以绘制所有游戏对象.
One Alternative
Don't create your game environment using components (i.e. JLabels). Instead, you can paint all your game objects.
例如,如果您正在执行以下操作:
For instance, if you're doing something like:
JLabel[][] labelGrid = JLabel[][];
...
ImageIcon icon = new ImageIcon(...);
JLabel label = new JLabel(icon);
...
for(... ; ... ; ...) {
container.add(label);
}
您可以改掉所有标签,也可以使用Images
代替ImageIcons
,然后可以将所有图像绘制到单个组件表面.也许像这样:
You could instead get rid of the labels all together, and also use Images
instead of ImageIcons
, then you can just paint all the images to a single component surface. Maybe something like:
public class GamePanel extends JPanel {
Image[][] images = new Image[size][size];
// init images
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,/* Crap! How do we know what location? Look Below */);
}
}
- 因此,要通过位置(即x和y,哦以及大小)解决此问题,我们可以使用一些好的旧的OOP抽象.创建一个包装图像,位置和尺寸的类.例如
So to solve this problem with the locations (i.e. the x and y, oh and the size also), we could use some good old OOP abstractions. Create a class that wraps the image, the locations, and sizes. For example
class LocatedImage { private Image image; private int x, y, width, height; private ImageObserver observer; public LocatedImage(Image image, int x, int y, int width, int height, ImageObserver observer) { this.image = image; ... } public void draw(Graphics2D g2d) { g2d.drawImage(image, x, y, width, height, observer); } }
然后,您可以在面板中使用许多此类的实例.像
Then you can use a bunch of instances of this class in your panel. Something like
public class GamePanel extends JPanel { List<LocatedImage> imagesToDraw; // init images // e.g. imagesToDraw.add(new LocatedImage(img, 20, 20, 100, 100, this)); @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g.create(); for (LocatedImage image: imagesToDraw) { image.draw(g2d); } g2d.dispose(); } }
一旦您有了这个概念,就有许多不同的可能性.
Once you have this concept down, there are many different possibilities.
- 请参考执行自定义绘画关于绘画的想法.
- Have a look at Performing Custom Painting for some general ideas about painting.
这篇关于JLabel在另一个JLabel上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!