I'm developing a game with JLabel
1 to 50, I can access them in line of code without having to use a switch.
You can verify that what changes in the code and only lblBoard1
or lblBoard2
and so on, who will determine JLabel
is the points
variable.
There is no problem in the code below, I would just like to use something like (I'm going to invent because that's exactly what I want) lblBoard[points].setIcon
. One more detail how I created these JLabel
at drawing time did not see how to create an array of them in the netBeans interface. What I really have are JLabel's ending with numbers lblBoard1
, lblBoard2
, and so on.
switch (points){
case 1:{
if (samePlace){
lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
} else {
if (player == "Player 1"){
lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
} else {
lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
}
}
break;
}
case 2:{
if (samePlace){
lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
} else {
if (player == "Player 1"){
lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
} else {
lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
}
}
break;
}
case 3:{
if (samePlace){
lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
} else {
if (player == "Player 1"){
lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
} else {
lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
}
}
break;
}
In case the question is elucidated, this is the screen of my game. wanted to move the players house to house. This works by doing the swicth but the code is not over. 51 Labels.