Ok what im trying to do is make a image called hidden and it will be a card face down. Now I want to make it so the image will change to a face up card and what i tried to do was make a image for six cards three for each team. "p1, p2, p3" and "o1, o2, o3" those are the names I gave the cards face down and i want the hidden JLabel to change or equal those and then change to a face up card I only put this part of the code because i thought thats all you need but if you need to see more of the code i can put it here. Any help would be appreciated.
public class Cards {
static int[] fullDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 52, 51, 50, 49, 48,
47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
30, 29, 28, 27, };
static int oDeck;
static int pDeck;
static int pTurn;
static int oTurn;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cards window = new Cards();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Cards() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel hidden = new JLabel("");
JLabel p1 = new JLabel("New label");
p1.setBounds(47, 24, 82, 82);
frame.getContentPane().add(p1);
JLabel p2 = new JLabel("New label");
p2.setBounds(139, 24, 82, 82);
frame.getContentPane().add(p2);
JLabel p3 = new JLabel("New label");
p3.setBounds(211, 24, 82, 82);
frame.getContentPane().add(p3);
JLabel o1 = new JLabel("New label");
o1.setBounds(47, 95, 82, 82);
frame.getContentPane().add(o1);
JLabel o2 = new JLabel("New label");
o2.setBounds(139, 95, 82, 82);
frame.getContentPane().add(o2);
JLabel o3 = new JLabel("New label");
o3.setBounds(211, 95, 82, 82);
frame.getContentPane().add(o3);
//This creates the players hand and the opponents hand
ArrayList<Integer> playerDeck = new ArrayList<>();
ArrayList<Integer> opponentDeck = new ArrayList<>();
for (int i = 0; i <= fullDeck.length; i++) {
if (i % 2 == 0) {
playerDeck.add(i);
} else {
opponentDeck.add(i);
}
Collections.shuffle(playerDeck);
Collections.shuffle(opponentDeck);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {for (int i = 0; i < 4; i++) {
/*if (i == 1){
pDeck = playerDeck.get(1);
hidden = p1;
hidden.setBounds(20, 200, 106, 61);
System.out.println("Check1");
}
else if (i == 2){
pDeck = playerDeck.get(2);
hidden = p2;
hidden.setBounds(162, 200, 106, 61);
System.out.println("Check2");
}
else if (i == 3){
pDeck = playerDeck.get(3);
hidden = p3;
System.out.println("Check3");
}*/
pDeck = playerDeck.get(1);
hidden = p1;
switch (pDeck) {
case 1:
Image img = new ImageIcon(this.getClass()
.getResource("/bin/1.png")).getImage();
hidden.setIcon(new ImageIcon(img));
pTurn = 14;
break;
case 2:
Image img2 = new ImageIcon(this.getClass()
.getResource("/bin/2.png")).getImage();
hidden.setIcon(new ImageIcon(img2));
pTurn = 14;
break;
case 3:
Image img3 = new ImageIcon(this.getClass()
.getResource("/bin/3.png")).getImage();
hidden.setIcon(new ImageIcon(img3));
pTurn = 14;
break;
case 4:
Image img4 = new ImageIcon(this.getClass()
.getResource("/bin/4.png")).getImage();
hidden.setIcon(new ImageIcon(img4));
pTurn = 14;
break;
case 5:
Image img5 = new ImageIcon(this.getClass()
.getResource("/bin/5.png")).getImage();
hidden.setIcon(new ImageIcon(img5));
pTurn = 13;
break;
case 6:
Image img6 = new ImageIcon(this.getClass()
.getResource("/bin/6.png")).getImage();
hidden.setIcon(new ImageIcon(img6));
pTurn = 13;
case 7:
Image img7 = new ImageIcon(this.getClass()
.getResource("/bin/7.png")).getImage();
hidden.setIcon(new ImageIcon(img7));
pTurn = 13;
break;
case 8:
Image img8 = new ImageIcon(this.getClass()
.getResource("/bin/8.png")).getImage();
hidden.setIcon(new ImageIcon(img8));
pTurn = 13;
break;
case 9:
Image img9 = new ImageIcon(this.getClass()
.getResource("/bin/9.png")).getImage();
hidden.setIcon(new ImageIcon(img9));
pTurn = 12;
break;
case 10:
Image img10 = new ImageIcon(this.getClass()
.getResource("/bin/10.png")).getImage();
hidden.setIcon(new ImageIcon(img10));
pTurn = 12;
break;
case 11:
Image img11 = new ImageIcon(this.getClass()
.getResource("/bin/11.png")).getImage();
hidden.setIcon(new ImageIcon(img11));
pTurn = 12;
break;
case 12:
Image img12 = new ImageIcon(this.getClass()
.getResource("/bin/12.png")).getImage();
hidden.setIcon(new ImageIcon(img12));
pTurn = 12;
break;
case 13:
Image img13 = new ImageIcon(this.getClass()
.getResource("/bin/13.png")).getImage();
hidden.setIcon(new ImageIcon(img13));
pTurn = 11;
break;
case 14:
Image img14 = new ImageIcon(this.getClass()
.getResource("/bin/14.png")).getImage();
hidden.setIcon(new ImageIcon(img14));
pTurn = 11;
break;
case 15:
Image img15 = new ImageIcon(this.getClass()
.getResource("/bin/15.png")).getImage();
hidden.setIcon(new ImageIcon(img15));
pTurn = 11;
break;
case 16:
Image img16 = new ImageIcon(this.getClass()
.getResource("/bin/16.png")).getImage();
hidden.setIcon(new ImageIcon(img16));
pTurn = 11;
break;
case 17:
Image img17 = new ImageIcon(this.getClass()
.getResource("/bin/17.png")).getImage();
hidden.setIcon(new ImageIcon(img17));
pTurn = 10;
break;
case 18:
Image img18 = new ImageIcon(this.getClass()
.getResource("/bin/18.png")).getImage();
hidden.setIcon(new ImageIcon(img18));
pTurn = 10;
break;
case 19:
Image img19 = new ImageIcon(this.getClass()
.getResource("/bin/19.png")).getImage();
hidden.setIcon(new ImageIcon(img19));
pTurn = 10;
break;
case 20:
Image img20 = new ImageIcon(this.getClass()
.getResource("/bin/20.png")).getImage();
hidden.setIcon(new ImageIcon(img20));
pTurn = 10;
break;
case 21:
Image img21 = new ImageIcon(this.getClass()
.getResource("/bin/21.png")).getImage();
hidden.setIcon(new ImageIcon(img21));
pTurn = 9;
break;
case 22:
Image img22 = new ImageIcon(this.getClass()
.getResource("/bin/22.png")).getImage();
hidden.setIcon(new ImageIcon(img22));
pTurn = 9;
break;
case 23:
Image img23 = new ImageIcon(this.getClass()
.getResource("/bin/23.png")).getImage();
hidden.setIcon(new ImageIcon(img23));
pTurn = 9;
break;
case 24:
Image img24 = new ImageIcon(this.getClass()
.getResource("/bin/24.png")).getImage();
hidden.setIcon(new ImageIcon(img24));
pTurn = 9;
break;
case 25:
Image img25 = new ImageIcon(this.getClass()
.getResource("/bin/25.png")).getImage();
hidden.setIcon(new ImageIcon(img25));
pTurn = 8;
break;
case 26:
Image img26 = new ImageIcon(this.getClass()
.getResource("/bin/26.png")).getImage();
hidden.setIcon(new ImageIcon(img26));
pTurn = 8;
break;
case 27:
Image img27 = new ImageIcon(this.getClass()
.getResource("/bin/27.png")).getImage();
hidden.setIcon(new ImageIcon(img27));
pTurn = 8;
break;
case 28:
Image img28 = new ImageIcon(this.getClass()
.getResource("/bin/28.png")).getImage();
hidden.setIcon(new ImageIcon(img28));
pTurn = 8;
break;
case 29:
Image img29 = new ImageIcon(this.getClass()
.getResource("/bin/29.png")).getImage();
hidden.setIcon(new ImageIcon(img29));
pTurn = 7;
break;
case 30:
Image img30 = new ImageIcon(this.getClass()
.getResource("/bin/30.png")).getImage();
hidden.setIcon(new ImageIcon(img30));
pTurn = 7;
break;
case 31:
Image img31 = new ImageIcon(this.getClass()
.getResource("/bin/31.png")).getImage();
hidden.setIcon(new ImageIcon(img31));
pTurn = 7;
break;
case 32:
Image img32 = new ImageIcon(this.getClass()
.getResource("/bin/32.png")).getImage();
hidden.setIcon(new ImageIcon(img32));
pTurn = 7;
break;
case 33:
Image img33 = new ImageIcon(this.getClass()
.getResource("/bin/33.png")).getImage();
hidden.setIcon(new ImageIcon(img33));
pTurn = 6;
break;
case 34:
Image img34 = new ImageIcon(this.getClass()
.getResource("/bin/34.png")).getImage();
hidden.setIcon(new ImageIcon(img34));
pTurn = 6;
break;
case 35:
Image img35 = new ImageIcon(this.getClass()
.getResource("/bin/35.png")).getImage();
hidden.setIcon(new ImageIcon(img35));
pTurn = 6;
break;
case 36:
Image img36 = new ImageIcon(this.getClass()
.getResource("/bin/36.png")).getImage();
hidden.setIcon(new ImageIcon(img36));
pTurn = 6;
break;
case 37:
Image img37 = new ImageIcon(this.getClass()
.getResource("/bin/37.png")).getImage();
hidden.setIcon(new ImageIcon(img37));
pTurn = 5;
break;
case 38:
Image img38 = new ImageIcon(this.getClass()
.getResource("/bin/38.png")).getImage();
hidden.setIcon(new ImageIcon(img38));
pTurn = 5;
break;
case 39:
Image img39 = new ImageIcon(this.getClass()
.getResource("/bin/39.png")).getImage();
hidden.setIcon(new ImageIcon(img39));
pTurn = 5;
break;
case 40:
Image img40 = new ImageIcon(this.getClass()
.getResource("/bin/40.png")).getImage();
hidden.setIcon(new ImageIcon(img40));
pTurn = 5;
break;
case 41:
Image img41 = new ImageIcon(this.getClass()
.getResource("/bin/41.png")).getImage();
hidden.setIcon(new ImageIcon(img41));
pTurn = 4;
break;
case 42:
Image img42 = new ImageIcon(this.getClass()
.getResource("/bin/42.png")).getImage();
hidden.setIcon(new ImageIcon(img42));
pTurn = 4;
break;
case 43:
Image img43 = new ImageIcon(this.getClass()
.getResource("/bin/43.png")).getImage();
hidden.setIcon(new ImageIcon(img43));
pTurn = 4;
break;
case 44:
Image img44 = new ImageIcon(this.getClass()
.getResource("/bin/44.png")).getImage();
hidden.setIcon(new ImageIcon(img44));
pTurn = 4;
break;
case 45:
Image img45 = new ImageIcon(this.getClass()
.getResource("/bin/45.png")).getImage();
hidden.setIcon(new ImageIcon(img45));
pTurn = 3;
break;
case 46:
Image img46 = new ImageIcon(this.getClass()
.getResource("/bin/46.png")).getImage();
hidden.setIcon(new ImageIcon(img46));
pTurn = 3;
break;
case 47:
Image img47 = new ImageIcon(this.getClass()
.getResource("/bin/47.png")).getImage();
hidden.setIcon(new ImageIcon(img47));
pTurn = 3;
break;
case 48:
Image img48 = new ImageIcon(this.getClass()
.getResource("/bin/48.png")).getImage();
hidden.setIcon(new ImageIcon(img48));
pTurn = 3;
break;
case 49:
Image img49 = new ImageIcon(this.getClass()
.getResource("/bin/49.png")).getImage();
hidden.setIcon(new ImageIcon(img49));
pTurn = 2;
break;
case 50:
Image img50 = new ImageIcon(this.getClass()
.getResource("/bin/50.png")).getImage();
hidden.setIcon(new ImageIcon(img50));
pTurn = 2;
break;
case 51:
Image img51 = new ImageIcon(this.getClass()
.getResource("/bin/51.png")).getImage();
hidden.setIcon(new ImageIcon(img51));
pTurn = 2;
break;
case 52:
Image img52 = new ImageIcon(this.getClass()
.getResource("/bin/52.png")).getImage();
hidden.setIcon(new ImageIcon(img52));
pTurn = 2;
break;
default:
}
if (i == 1){
oDeck = opponentDeck.get(1);
JLabel hidden = o1;
}
else if (i == 2){
oDeck = opponentDeck.get(2);
JLabel hidden = o2;
}
else if (i == 3){
oDeck = opponentDeck.get(3);
JLabel hidden = o3;
}
switch (oDeck) {
case 1:
Image img = new ImageIcon(this.getClass()
.getResource("/bin/1.png")).getImage();
hidden.setIcon(new ImageIcon(img));
oTurn = 14;
break;
case 2:
Image img2 = new ImageIcon(this.getClass()
.getResource("/bin/2.png")).getImage();
hidden.setIcon(new ImageIcon(img2));
oTurn = 14;
break;
case 3:
Image img3 = new ImageIcon(this.getClass()
.getResource("/bin/3.png")).getImage();
hidden.setIcon(new ImageIcon(img3));
oTurn = 14;
break;
case 4:
Image img4 = new ImageIcon(this.getClass()
.getResource("/bin/4.png")).getImage();
hidden.setIcon(new ImageIcon(img4));
oTurn = 14;
break;
case 5:
Image img5 = new ImageIcon(this.getClass()
.getResource("/bin/5.png")).getImage();
hidden.setIcon(new ImageIcon(img5));
oTurn = 13;
break;
case 6:
Image img6 = new ImageIcon(this.getClass()
.getResource("/bin/6.png")).getImage();
hidden.setIcon(new ImageIcon(img6));
oTurn = 13;
case 7:
Image img7 = new ImageIcon(this.getClass()
.getResource("/bin/7.png")).getImage();
hidden.setIcon(new ImageIcon(img7));
oTurn = 13;
break;
case 8:
Image img8 = new ImageIcon(this.getClass()
.getResource("/bin/8.png")).getImage();
hidden.setIcon(new ImageIcon(img8));
oTurn = 13;
break;
case 9:
Image img9 = new ImageIcon(this.getClass()
.getResource("/bin/9.png")).getImage();
hidden.setIcon(new ImageIcon(img9));
oTurn = 12;
break;
case 10:
Image img10 = new ImageIcon(this.getClass()
.getResource("/bin/10.png")).getImage();
hidden.setIcon(new ImageIcon(img10));
oTurn = 12;
break;
case 11:
Image img11 = new ImageIcon(this.getClass()
.getResource("/bin/11.png")).getImage();
hidden.setIcon(new ImageIcon(img11));
oTurn = 12;
break;
case 12:
Image img12 = new ImageIcon(this.getClass()
.getResource("/bin/12.png")).getImage();
hidden.setIcon(new ImageIcon(img12));
oTurn = 12;
break;
case 13:
Image img13 = new ImageIcon(this.getClass()
.getResource("/bin/13.png")).getImage();
hidden.setIcon(new ImageIcon(img13));
oTurn = 11;
break;
case 14:
Image img14 = new ImageIcon(this.getClass()
.getResource("/bin/14.png")).getImage();
hidden.setIcon(new ImageIcon(img14));
oTurn = 11;
break;
case 15:
Image img15 = new ImageIcon(this.getClass()
.getResource("/bin/15.png")).getImage();
hidden.setIcon(new ImageIcon(img15));
oTurn = 11;
break;
case 16:
Image img16 = new ImageIcon(this.getClass()
.getResource("/bin/16.png")).getImage();
hidden.setIcon(new ImageIcon(img16));
oTurn = 11;
break;
case 17:
Image img17 = new ImageIcon(this.getClass()
.getResource("/bin/17.png")).getImage();
hidden.setIcon(new ImageIcon(img17));
oTurn = 10;
break;
case 18:
Image img18 = new ImageIcon(this.getClass()
.getResource("/bin/18.png")).getImage();
hidden.setIcon(new ImageIcon(img18));
oTurn = 10;
break;
case 19:
Image img19 = new ImageIcon(this.getClass()
.getResource("/bin/19.png")).getImage();
hidden.setIcon(new ImageIcon(img19));
oTurn = 10;
break;
case 20:
Image img20 = new ImageIcon(this.getClass()
.getResource("/bin/20.png")).getImage();
hidden.setIcon(new ImageIcon(img20));
oTurn = 10;
break;
case 21:
Image img21 = new ImageIcon(this.getClass()
.getResource("/bin/21.png")).getImage();
hidden.setIcon(new ImageIcon(img21));
oTurn = 9;
break;
case 22:
Image img22 = new ImageIcon(this.getClass()
.getResource("/bin/22.png")).getImage();
hidden.setIcon(new ImageIcon(img22));
oTurn = 9;
break;
case 23:
Image img23 = new ImageIcon(this.getClass()
.getResource("/bin/23.png")).getImage();
hidden.setIcon(new ImageIcon(img23));
oTurn = 9;
break;
case 24:
Image img24 = new ImageIcon(this.getClass()
.getResource("/bin/24.png")).getImage();
hidden.setIcon(new ImageIcon(img24));
oTurn = 9;
break;
case 25:
Image img25 = new ImageIcon(this.getClass()
.getResource("/bin/25.png")).getImage();
hidden.setIcon(new ImageIcon(img25));
oTurn = 8;
break;
case 26:
Image img26 = new ImageIcon(this.getClass()
.getResource("/bin/26.png")).getImage();
hidden.setIcon(new ImageIcon(img26));
oTurn = 8;
break;
case 27:
Image img27 = new ImageIcon(this.getClass()
.getResource("/bin/27.png")).getImage();
hidden.setIcon(new ImageIcon(img27));
oTurn = 8;
break;
case 28:
Image img28 = new ImageIcon(this.getClass()
.getResource("/bin/28.png")).getImage();
hidden.setIcon(new ImageIcon(img28));
oTurn = 8;
break;
case 29:
Image img29 = new ImageIcon(this.getClass()
.getResource("/bin/29.png")).getImage();
hidden.setIcon(new ImageIcon(img29));
oTurn = 7;
break;
case 30:
Image img30 = new ImageIcon(this.getClass()
.getResource("/bin/30.png")).getImage();
hidden.setIcon(new ImageIcon(img30));
oTurn = 7;
break;
case 31:
Image img31 = new ImageIcon(this.getClass()
.getResource("/bin/31.png")).getImage();
hidden.setIcon(new ImageIcon(img31));
oTurn = 7;
break;
case 32:
Image img32 = new ImageIcon(this.getClass()
.getResource("/bin/32.png")).getImage();
hidden.setIcon(new ImageIcon(img32));
oTurn = 7;
break;
case 33:
Image img33 = new ImageIcon(this.getClass()
.getResource("/bin/33.png")).getImage();
hidden.setIcon(new ImageIcon(img33));
oTurn = 6;
break;
case 34:
Image img34 = new ImageIcon(this.getClass()
.getResource("/bin/34.png")).getImage();
hidden.setIcon(new ImageIcon(img34));
oTurn = 6;
break;
case 35:
Image img35 = new ImageIcon(this.getClass()
.getResource("/bin/35.png")).getImage();
hidden.setIcon(new ImageIcon(img35));
oTurn = 6;
break;
case 36:
Image img36 = new ImageIcon(this.getClass()
.getResource("/bin/36.png")).getImage();
hidden.setIcon(new ImageIcon(img36));
oTurn = 6;
break;
case 37:
Image img37 = new ImageIcon(this.getClass()
.getResource("/bin/37.png")).getImage();
hidden.setIcon(new ImageIcon(img37));
oTurn = 5;
break;
case 38:
Image img38 = new ImageIcon(this.getClass()
.getResource("/bin/38.png")).getImage();
hidden.setIcon(new ImageIcon(img38));
oTurn = 5;
break;
case 39:
Image img39 = new ImageIcon(this.getClass()
.getResource("/bin/39.png")).getImage();
hidden.setIcon(new ImageIcon(img39));
oTurn = 5;
break;
case 40:
Image img40 = new ImageIcon(this.getClass()
.getResource("/bin/40.png")).getImage();
hidden.setIcon(new ImageIcon(img40));
oTurn = 5;
break;
case 41:
Image img41 = new ImageIcon(this.getClass()
.getResource("/bin/41.png")).getImage();
hidden.setIcon(new ImageIcon(img41));
oTurn = 4;
break;
case 42:
Image img42 = new ImageIcon(this.getClass()
.getResource("/bin/42.png")).getImage();
hidden.setIcon(new ImageIcon(img42));
oTurn = 4;
break;
case 43:
Image img43 = new ImageIcon(this.getClass()
.getResource("/bin/43.png")).getImage();
hidden.setIcon(new ImageIcon(img43));
oTurn = 4;
break;
case 44:
Image img44 = new ImageIcon(this.getClass()
.getResource("/bin/44.png")).getImage();
hidden.setIcon(new ImageIcon(img44));
oTurn = 4;
break;
case 45:
Image img45 = new ImageIcon(this.getClass()
.getResource("/bin/45.png")).getImage();
hidden.setIcon(new ImageIcon(img45));
oTurn = 3;
break;
case 46:
Image img46 = new ImageIcon(this.getClass()
.getResource("/bin/46.png")).getImage();
hidden.setIcon(new ImageIcon(img46));
oTurn = 3;
break;
case 47:
Image img47 = new ImageIcon(this.getClass()
.getResource("/bin/47.png")).getImage();
hidden.setIcon(new ImageIcon(img47));
oTurn = 3;
break;
case 48:
Image img48 = new ImageIcon(this.getClass()
.getResource("/bin/48.png")).getImage();
hidden.setIcon(new ImageIcon(img48));
oTurn = 3;
break;
case 49:
Image img49 = new ImageIcon(this.getClass()
.getResource("/bin/49.png")).getImage();
hidden.setIcon(new ImageIcon(img49));
oTurn = 2;
break;
case 50:
Image img50 = new ImageIcon(this.getClass()
.getResource("/bin/50.png")).getImage();
hidden.setIcon(new ImageIcon(img50));
oTurn = 2;
break;
case 51:
Image img51 = new ImageIcon(this.getClass()
.getResource("/bin/51.png")).getImage();
hidden.setIcon(new ImageIcon(img51));
oTurn = 2;
break;
case 52:
Image img52 = new ImageIcon(this.getClass()
.getResource("/bin/52.png")).getImage();
hidden.setIcon(new ImageIcon(img52));
oTurn = 2;
break;
default:
}
}
}
});
btnNewButton.setBounds(171, 227, 89, 23);
frame.getContentPane().add(btnNewButton);
}
} }
From your code, I am guessing you have a
hidden
instance variable declared and in yourswitch
statement, you are addingicon
to this JLabel.But in your
if
condition, you are not using thishidden
variable, but creating a new one.Fix: Inside your for loop, search for all
JLabel hidden
strings and replace it just withhidden
and see if your problem is solved.