我知道这已经被问过好几次了,但没有一个解决方案对我们有用。我们正在重新创建一个游戏,它有一张世界地图,并且应该在每个国家都显示国旗。我们通过使用图形方法让它工作(有点),但这不是持久的,我们也不能通过 var 名称来处理标志。
这就是为什么我们希望将每个标志都放在 aJLabel
中,并将它们绘制在 中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();
}
}