JPanel 或 JLabel 似乎为每个对象创建两个标签而不是一个?
如果这是一个非常简单的解决方案或愚蠢的错误,请原谅我 - 这是我第一次尝试用 Java 实现图形! :)
我正在尝试制作一块图块板,每个图块都是一个图块对象,图块的所有位置都存储在名为“内容”的图块三重数组中(内容[][][])。
为了使每个图块“可点击”,我基本上使用每个图块图标创建一个标签,并根据图块对象的 x,y 坐标将该图块放置在板上。我对内容数组中的每个非空 Tile 对象执行此操作。
当我使用 Graphics.drawImage 函数时,效果很好,但是当我使用 setBorders() 函数定位每个标签时,它:
- 创建图块的布局,但并不完美 - 似乎有些缺失或低于其他。
它
- 会在位于正确位置的其他图块上方创建一个重复的未定位图层。
我正在调用的函数的代码是:
public void paintComponent(Graphics graphics) {
// let superclass paint to fill in background
super.paintComponent(graphics);
Tile[][][] content = b.getContent();
if (content==null || tileImages==null) {
return;
}
/* Set dummy previous label */
prevT.setBounds(-1,-1,1,1);
// draw tiles back to front
for (int i = 0; i<content.length; i++) {
for (int y = 0; y<content[i].length; y++) {
for (int x = 0; x<content[i][y].length; x++) {
final Tile t = content[i][y][x];
if (t!=null) {
if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
continue;
}
if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
continue;
}
Image image = tileImages[t.getValue()][t.getSubindex()];
if (b.free(t)) {
image = tileImagesHL[t.getValue()][t.getSubindex()];
}
/* Mouse event magic */
graphics.drawImage(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, null);
/* Create icon to be displayed */
ImageIcon icon = new ImageIcon(image);
/* Label that acts as the clickable tile */
final JLabel label = new JLabel();
/* Associate image with label */
label.setIcon(icon);
/* Allow mouse events to interact with label */
label.addMouseListener(this);
/* Position labels according to tile coordinates */
label.setBounds(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));
/* Associate label with specified tile */
t.setLabel(label);
/* Add label to list*/
labels.add(label);
this.setVisible(true);
this.add(label);
}
}
}
}
}
任何关于为什么会发生这种情况的解释都将非常感激!我已经尝试重写这个函数很多次了,但我没有主意了!
谢谢你! :)
Please excuse me if this is a very simple solution or stupid mistake - this is my first time attempting to implement graphics in Java! :)
I'm attempting to make a board of tiles, each is a Tile object, and all positions of the tiles are stored in a triple array of Tiles called 'content' (content[][][]).
In order to make each tile "clickable", I'm basically creating a label with each tile icon and positioning that tile on the board based on the Tile object's x,y coordinates. I do this for each of the non-null Tile objects in the content array.
This works fine when I use the graphics.drawImage function, but when I position each label using the setBorders() function it:
- Creates the layout of tiles, but not perfectly - it seems some are missing or below others.
and
- It creates a duplicate unpositioned layer above the other tiles that are in their sort-of correct positions.
The code for the function I'm calling is:
public void paintComponent(Graphics graphics) {
// let superclass paint to fill in background
super.paintComponent(graphics);
Tile[][][] content = b.getContent();
if (content==null || tileImages==null) {
return;
}
/* Set dummy previous label */
prevT.setBounds(-1,-1,1,1);
// draw tiles back to front
for (int i = 0; i<content.length; i++) {
for (int y = 0; y<content[i].length; y++) {
for (int x = 0; x<content[i][y].length; x++) {
final Tile t = content[i][y][x];
if (t!=null) {
if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
continue;
}
if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
continue;
}
Image image = tileImages[t.getValue()][t.getSubindex()];
if (b.free(t)) {
image = tileImagesHL[t.getValue()][t.getSubindex()];
}
/* Mouse event magic */
graphics.drawImage(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, null);
/* Create icon to be displayed */
ImageIcon icon = new ImageIcon(image);
/* Label that acts as the clickable tile */
final JLabel label = new JLabel();
/* Associate image with label */
label.setIcon(icon);
/* Allow mouse events to interact with label */
label.addMouseListener(this);
/* Position labels according to tile coordinates */
label.setBounds(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));
/* Associate label with specified tile */
t.setLabel(label);
/* Add label to list*/
labels.add(label);
this.setVisible(true);
this.add(label);
}
}
}
}
}
Any explanation for why this is happening would be greatly greatly appreciated! I've tried to re-write this function SO many times and am out of ideas!
Thank you! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)不要在
paintComponent(Graphicsgraphics)
中创建Object
,在我的视图之前准备这些
Object
,简单的容器与鼠标事件魔术没有< code>MouseListener2) 创建
JPanel
并放置JButton#setBackground(someColor)
,然后添加JButton#setRolloverIcon(someIcon)
,不再需要任何MouseListener
3) 如果您创建 Tiles,则查找 GridLayout
4) 准备
Icons
并将这些Icon
添加到JButton
,没有别的,对象
的任何Array
,没有比我描述的更多的代码1) don't create
Object
insidepaintComponent(Graphics graphics)
, prepare theseObject
beforemy view, simple comtainer with Mouse event magic without
MouseListener
2) create
JPanel
and put thereJButton#setBackground(someColor)
, and addJButton#setRolloverIcon(someIcon)
, no longer anyMouseListener
needed3) if you create Tiles then look for GridLayout
4) prepare
Icons
and add theseIcon
to theJButton
, nothing else, anyArray
ofObjects
, no code more than I described