JPanel or JLabel seem to be creating two labels instead of one for each object?
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! :)
1) don't create Object
inside paintComponent(Graphics graphics)
, prepare these Object
before
my view, simple comtainer with Mouse event magic without MouseListener
2) create JPanel
and put there JButton#setBackground(someColor)
, and add JButton#setRolloverIcon(someIcon)
, no longer any MouseListener
needed
3) if you create Tiles then look for GridLayout
4) prepare Icons
and add these Icon
to the JButton
, nothing else, any Array
of Objects
, no code more than I described
import java.awt.*;
import javax.swing.*;
public class TilesWithButton {
private Icon warningIcon = UIManager.getIcon("OptionPane.warningIcon");
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
private Icon errorIconRoll = UIManager.getIcon("OptionPane.errorIcon");
private JPanel myPanel = new JPanel();
public TilesWithButton() {
myPanel.setLayout(new GridLayout(1, 2, 1, 1));
JButton btn = new JButton();
btn.setBackground(Color.white);
btn.setIcon(infoIcon);
btn.setRolloverIcon(errorIconRoll);
btn.setFocusPainted(false);
myPanel.add(btn);
JButton btn1 = new JButton();
btn1.setBackground(Color.white);
btn1.setIcon(warningIcon);
btn1.setRolloverIcon(errorIconRoll);
btn1.setFocusPainted(false);
myPanel.add(btn1);
JFrame frame = new JFrame("Tiles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(myPanel);
frame.pack();
frame.setLocation(150, 100);
frame.setVisible(true);
}
public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TilesWithButton tilesWithButton = new TilesWithButton();
}
});
}
}
精彩评论