我必须为学校做一个Java项目,我决定制作一个棋盘游戏:Stratego。但是我现在已经在实际电路板的GUI上停留了一段时间。 我的目的是制作一个带有背景图像的框架,并在其上面(如在上层)一个网格,在那里我可以为网格的每个方格分配坐标。创建一个片段时,播放器可以为其分配一个正方形,这样该正方形必须能够显示一个片段的图像。
这就是我被困了几个星期的地方!我似乎无法找到如何制作具有多个图层的框架。你知道更简单的实现吗? 如果有人能提供帮助,我真的很感激。
编辑:我应该把我已经完成的工作放在那里:public class FenetreJeu extends JFrame {
private static final long serialVersionUID = 1L;
public static int TILE_OFFSET= 60;//Constant for the coordinates
public FenetreJeu() {
setVisible(true);
setSize(700,700);
Panel background= new Panel();
background.setBorder(new EmptyBorder(0, 0, 0, 0));
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(background);
background.setLayout(new GridLayout(10, 10,1,1));
}
private void createAndAddPiece(int couleur, int rang, int x, int y) {
Image img = this.getImagePion(couleur, rang);
switch(rang){
case Pion.RANG_MARECHAL:
Marechal marechal = new Marechal();
break;
case Pion.RANG_GENERAL:
General general = new General();
break;
case Pion.RANG_COLONEL:
Colonel colonel = new Colonel();
break;
case Pion.RANG_COMMANDANT:
Commandant commandant = new Commandant();
break;
case Pion.RANG_CAPITAINE:
Capitaine capitaine = new Capitaine();
break;
case Pion.RANG_LIEUTENANT:
Lieutenant lieutenant = new Lieutenant();
break;
case Pion.RANG_SERGENT:
Sergent sergent = new Sergent();
break;
case Pion.RANG_DEMINEUR:
Demineur demineur = new Demineur();
break;
case Pion.RANG_ECLAIREUR:
Eclaireur eclaireur = new Eclaireur();
break;
case Pion.RANG_ESPION:
Espion espion = new Espion();
break;
case Pion.RANG_DRAPEAU:
Drapeau drapeau = new Drapeau(Pion.RANG_DRAPEAU,Pion.STATIQUE);
break;
case Pion.RANG_BOMBE:
Bombe bombe = new Bombe(Pion.RANG_BOMBE,Pion.STATIQUE);
break;
}
}
private Image getImagePion(int couleur, int rang) {//Method to get the image of the piece
String filename = "";
filename += ( couleur == Pion.COLOR_RED ? "/Ressources/PionsRouges/" : "/Ressources/PionsBleus/");
switch (rang) {
case pions.Pion.RANG_MARECHAL:
filename += "9";
break;
case pions.Pion.RANG_GENERAL:
filename += "8";
break;
case pions.Pion.RANG_COLONEL:
filename += "7";
break;
case pions.Pion.RANG_COMMANDANT:
filename += "6";
break;
case pions.Pion.RANG_CAPITAINE:
filename += "5";
break;
case pions.Pion.RANG_LIEUTENANT:
filename += "4";
break;
case pions.Pion.RANG_SERGENT:
filename += "3";
break;
case pions.Pion.RANG_DEMINEUR:
filename += "2";
case pions.Pion.RANG_ECLAIREUR:
filename += "1";
break;
case pions.Pion.RANG_ESPION:
filename += "10";
break;
case pions.Pion.RANG_DRAPEAU:
filename += "0";
break;
case pions.Pion.RANG_BOMBE:
filename += "-1";
break;
}
filename += ".gif";
URL urlPieceImg = getClass().getResource(filename);
return (new ImageIcon(urlPieceImg).getImage());
}
}