(Java)How to change picture(War Card Game)

656 views Asked by At

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);




}

} }

2

There are 2 answers

3
Codebender On BEST ANSWER

From your code, I am guessing you have a hidden instance variable declared and in your switch statement, you are adding icon to this JLabel.

But in your if condition, you are not using this hidden variable, but creating a new one.

Fix: Inside your for loop, search for all JLabel hidden strings and replace it just with hidden and see if your problem is solved.

1
Hovercraft Full Of Eels On

I'm not sure when you want your cards to flip, but if it were me, I'd do things entirely different, and would try to do things in more of an OOP-kind of way. For one, I'd create a Card object, one that is composed of a suit and a rank fields, both of which are enums:

public enum Suit {
   CLUBS, DIAMONDS, HEARTS, SPADES
}

public enum Rank {
   ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
         9), TEN(10), JACK(11), QUEEN(12), KING(13);
   private int value;

   private Rank(int value) {
      this.value = value;
   }

   public int getValue() {
      return value;
   }
}

The Card.java class would have an equals and hashCode method so that I could use it in a HashMap and thereby associate an ImageIcon with each card.

public class Card {
   private Suit suit;
   private Rank rank;

   public Card(Suit suit, Rank rank) {
      this.suit = suit;
      this.rank = rank;
   }

   public Suit getSuit() {
      return suit;
   }

   public Rank getRank() {
      return rank;
   }

   @Override
   public String toString() {
      return "Card [suit=" + suit + ", rank=" + rank + "]";
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((rank == null) ? 0 : rank.hashCode());
      result = prime * result + ((suit == null) ? 0 : suit.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Card other = (Card) obj;
      if (rank != other.rank)
         return false;
      if (suit != other.suit)
         return false;
      return true;
   }

}

I'd create a Deck.java class that would hold and shuffle all 52 cards, could be regenerated and could deal Cards:

public class Deck {
   private List<Card> cards = new ArrayList<>();

   public Deck() {
      regenerate();
   }

   public void regenerate() {
      cards.clear();
      for (Suit suit : Suit.values()) {
         for (Rank rank : Rank.values()) {
            cards.add(new Card(suit, rank));
         }
      }
      Collections.shuffle(cards);
   }

   public Card deal() {
      if (cards.size() > 0) {
         return cards.remove(0);
      } else {
         // or throw exception
         return null;
      }
   }
}

Then I'd create a method that would allow me to get my Card images (I have used a publicly available sprite sheet for this) and assign the appropriate icon to the appropriate card in the HashMap:

   public static Map<Card, Icon> createCardIconMap(String pathToDeck)
         throws IOException {
      BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck));
      int width = fullDeckImg.getWidth();
      int height = fullDeckImg.getHeight();
      Map<Card, Icon> cardIconMap = new HashMap<>();
      for (int suitInt = 0; suitInt < Suit.values().length; suitInt++) {
         for (int rankInt = 0; rankInt < Rank.values().length; rankInt++) {
            int x = (rankInt * width) / Rank.values().length;
            int y = (suitInt * height) / Suit.values().length;
            int w = width / Rank.values().length;
            int h = height / Suit.values().length;
            BufferedImage cardImg = fullDeckImg.getSubimage(x, y, w, h);

            Suit suit = Suit.values()[suitInt];
            Rank rank = Rank.values()[rankInt];
            cardIconMap.put(new Card(suit, rank), new ImageIcon(cardImg));
         }
      }
      return cardIconMap;
   }

I would then create a GUI with which to manipulate the cards. If I wanted to flip cards with a mouse press, I'd add a MouseListener to my JLabels that hold the card image icons. e.g.,

private class LabelListener extends MouseAdapter {
  @Override
  public void mousePressed(MouseEvent e) {
     // get the pressed JLabel 
     JLabel label = (JLabel) e.getSource();

     // find out where it is in our array of JLabel
     int index = -1;
     for (int i = 0; i < labels.length; i++) {
        if (labels[i] == label) {
           // here it is!
           index = i;  
           break;
        }
     }
     if (index < 0) {
        // if index not found, get out of here
        return;
     }

     // get the label's current icon
     Icon icon = label.getIcon();
     if (icon == null) {
        // if it has none, get out of here!
        return;
     } else if (icon == cardBackIcon) {
        // if the icon is the card-back
        // then get the corresponding Card from our "model"
        // here simply an array of Card called dealtCards
        Card card = dealtCards[index];
        if (card == null) {
           // if array has nulls, get out of here!
           return;
        }
        // else set the current label's icon to 
        // the icon represented by the current card
        label.setIcon(cardIconMap.get(card));
     } else {
        // else a card is already showing, so let's show
        // a card-back instead
        label.setIcon(cardBackIcon);
     }
  }
}

The running program could look like so:

enter image description here

And the whole code could look something like so (please feel free to compile and run the program below to see what I mean):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;

public class CardProg {
   public static final String CARD_IMAGES = "http://www.jfitz.com/cards/classic-playing-cards.png";
   public static final String CARD_BACK_IMG = "http://www.jfitz.com/cards/b1fv.png";

   private static void createAndShowGui() {
      Map<Card, Icon> cardIconMap = null;
      Icon cardBackIcon = null;
      try {
         cardIconMap = CreateCardFaces.createCardIconMap(CARD_IMAGES);
         cardBackIcon = new ImageIcon(ImageIO.read(new URL(CARD_BACK_IMG)));
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }
      TablePanel mainPanel = new TablePanel(cardBackIcon, cardIconMap);

      JFrame frame = new JFrame("CardProg");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.setResizable(false);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

}

@SuppressWarnings("serial")
class TablePanel extends JPanel {
   private static final int PREF_W = 600;
   private static final int PREF_H = 400;
   private static final Color BASE_COLOR = new Color(0, 80, 0);
   private static final int ROWS = 2;
   private static final int COLS = 6;

   private Icon cardBackIcon;
   private Map<Card, Icon> cardIconMap;
   private JLabel[] labels = new JLabel[ROWS * COLS];
   private Card[] dealtCards = new Card[ROWS * COLS];
   private Deck deck = new Deck();

   public TablePanel(Icon cardBackIcon, Map<Card, Icon> cardIconMap) {
      this.cardBackIcon = cardBackIcon;
      this.cardIconMap = cardIconMap;

      JPanel centerPanel = new JPanel(new GridLayout(ROWS, COLS));
      centerPanel.setOpaque(false);
      for (int i = 0; i < labels.length; i++) {
         labels[i] = new JLabel();
         JPanel lPanel = new JPanel(new GridBagLayout());
         lPanel.setOpaque(false);
         lPanel.add(labels[i]);
         centerPanel.add(lPanel);

         labels[i].addMouseListener(new LabelListener());;
      }

      JButton dealButton = new JButton(new DealAction("Deal"));
      JPanel bottomPanel = new JPanel();
      bottomPanel.setOpaque(false);
      bottomPanel.add(dealButton);

      setBackground(BASE_COLOR);
      setLayout(new BorderLayout());
      add(centerPanel, BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.PAGE_END);
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private class LabelListener extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent e) {
         // get the pressed JLabel 
         JLabel label = (JLabel) e.getSource();

         // find out where it is in our array of JLabel
         int index = -1;
         for (int i = 0; i < labels.length; i++) {
            if (labels[i] == label) {
               // here it is!
               index = i;  
               break;
            }
         }
         if (index < 0) {
            // if index not found, get out of here
            return;
         }

         // get the label's current icon
         Icon icon = label.getIcon();
         if (icon == null) {
            // if it has none, get out of here!
            return;
         } else if (icon == cardBackIcon) {
            // if the icon is the card-back
            // then get the corresponding Card from our "model"
            // here simply an array of Card called dealtCards
            Card card = dealtCards[index];
            if (card == null) {
               // if array has nulls, get out of here!
               return;
            }
            // else set the current label's icon to 
            // the icon represented by the current card
            label.setIcon(cardIconMap.get(card));
         } else {
            // else a card is already showing, so let's show
            // a card-back instead
            label.setIcon(cardBackIcon);
         }
      }
   }

   private class DealAction extends AbstractAction {
      public DealAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         deck.regenerate();
         for (int i = 0; i < dealtCards.length; i++) {
            dealtCards[i] = deck.deal();
            labels[i].setIcon(cardBackIcon);
         }
      }
   }

}

class Deck {
   private List<Card> cards = new ArrayList<>();

   public Deck() {
      regenerate();
   }

   public void regenerate() {
      cards.clear();
      for (Suit suit : Suit.values()) {
         for (Rank rank : Rank.values()) {
            cards.add(new Card(suit, rank));
         }
      }
      Collections.shuffle(cards);
   }

   public Card deal() {
      if (cards.size() > 0) {
         return cards.remove(0);
      } else {
         // or throw exception
         return null;
      }
   }
}

enum Suit {
   CLUBS, DIAMONDS, HEARTS, SPADES
}

enum Rank {
   ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
         9), TEN(10), JACK(11), QUEEN(12), KING(13);
   private int value;

   private Rank(int value) {
      this.value = value;
   }

   public int getValue() {
      return value;
   }
}

class Card {
   private Suit suit;
   private Rank rank;

   public Card(Suit suit, Rank rank) {
      this.suit = suit;
      this.rank = rank;
   }

   public Suit getSuit() {
      return suit;
   }

   public Rank getRank() {
      return rank;
   }

   @Override
   public String toString() {
      return "Card [suit=" + suit + ", rank=" + rank + "]";
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((rank == null) ? 0 : rank.hashCode());
      result = prime * result + ((suit == null) ? 0 : suit.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Card other = (Card) obj;
      if (rank != other.rank)
         return false;
      if (suit != other.suit)
         return false;
      return true;
   }

}

class CreateCardFaces {
   // method only works with 
   // http://www.jfitz.com/cards/classic-playing-cards.png
   // sprite sheet
   public static Map<Card, Icon> createCardIconMap(String pathToDeck)
         throws IOException {
      BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck));
      int width = fullDeckImg.getWidth();
      int height = fullDeckImg.getHeight();
      Map<Card, Icon> cardIconMap = new HashMap<>();

      // the sprite sheet that I'm using uses this order of suits: 
      Suit[] suitOrder = {Suit.CLUBS, Suit.SPADES, Suit.HEARTS, Suit.DIAMONDS};
      for (int suitInt = 0; suitInt < suitOrder.length; suitInt++) {
         for (int rankInt = 0; rankInt < Rank.values().length; rankInt++) {
            int x = (rankInt * width) / Rank.values().length;
            int y = (suitInt * height) / suitOrder.length;
            int w = width / Rank.values().length;
            int h = height / suitOrder.length;
            BufferedImage cardImg = fullDeckImg.getSubimage(x, y, w, h);

            Suit suit = suitOrder[suitInt];
            Rank rank = Rank.values()[rankInt];
            cardIconMap.put(new Card(suit, rank), new ImageIcon(cardImg));
         }
      }

      return cardIconMap;
   }
}