我知道这已经有好几次了,但是没有一个解决方案对我们有效。
我们正在重新创建一个具有世界地图的游戏,并且应该在每个国家/地区显示标志。我们通过使用图形方法得到它的工作(有点),但这不是持久的,也不能用var名称来标记一个标志。
这就是为什么我们想让JLabel中的每个标志都在gamePane中绘制,但是在它们的特定坐标上。所以我们需要能够用z轴进行定位。
GamePane的JLabel里面有一个ImageIcon,它应该形成背景。最重要的是,我们想创建标志
一些代码(可能没有帮助): -
gamePane = new JPanel();
[...]
public void paintFlagLabel() {
for (Country currentCountry : risiko.getCountryList()) {
JLabel flag = new JLabel();
int x = currentCountry.getX();
int y = currentCountry.getY();
if (currentCountry.getOwningPlayer().equals(this.player)) {
flag.setIcon(new ImageIcon(greenFlag));
} else {
flag.setIcon(new ImageIcon(redFlag));
}
String coordinates = "pos " + x + " " + y;
gamePane.add(flag, coordinates);
gamePane.revalidate();
gamePane.repaint();
}
}