2017-04-10 182 views
0

我剛剛收到一個新的課程項目來製作二十一點紙牌遊戲。沒有真正的遊戲玩法尚未完成,我目前只是在用戶界面上工作。隨機Jlabel圖片

我想獲得一個隨機生成器,當按下「hit」按鈕時會給用戶一個隨機卡,但是我需要爲圖像創建某種數組,但我不確定如何去關於這樣做。

public class BlackJack extends JFrame { 

    private JPanel contentPane; 

    public int card, increase = 1; 
    public int cardSlot[] = new int[5]; 

    ImageIcon cardType = new ImageIcon("E:\\Noahs Coding\\Blackjack\\Cards\\0.gif"); 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        BlackJack frame = new BlackJack(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public BlackJack() { 

     setTitle("Blackjack"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setResizable(false); 
     setBounds(100, 100, 844, 499); 
     setLocationRelativeTo(null); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     JLabel holder1Card1 = new JLabel(""); 
     holder1Card1.setIcon(new ImageIcon("E:\\Noahs Coding\\Blackjack\\Cards\\0.gif")); 
     holder1Card1.setBounds(0, 295, 73, 222); 
     contentPane.add(holder1Card1); 

     JLabel holder1Card2 = new JLabel(""); 
     holder1Card2.setIcon(new ImageIcon("E:\\Noahs Coding\\Blackjack\\Cards\\1.gif")); 
     holder1Card2.setBounds(83, 295, 73, 222); 
     contentPane.add(holder1Card2); 

     JLabel holder1Card3 = new JLabel(""); 
     holder1Card3.setIcon(new ImageIcon("E:\\Noahs Coding\\Blackjack\\Cards\\2.gif")); 
     holder1Card3.setBounds(166, 295, 73, 222); 
     contentPane.add(holder1Card3); 

     JLabel holder1Card4 = new JLabel(""); 
     holder1Card4.setIcon(new ImageIcon("E:\\Noahs Coding\\Blackjack\\Cards\\4.gif")); 
     holder1Card4.setBounds(249, 295, 73, 222); 
     contentPane.add(holder1Card4); 

     JLabel holder1Card5 = new JLabel(""); 
     holder1Card5.setIcon(new ImageIcon("E:\\Noahs Coding\\Blackjack\\Cards\\33.gif")); 
     holder1Card5.setBounds(332, 295, 73, 222); 
     contentPane.add(holder1Card5); 

     JLabel Dealer1 = new JLabel(""); 
     Dealer1.setIcon(new ImageIcon("E:\\Noahs Coding\\Blackjack\\Cards\\20.gif")); 
     Dealer1.setBounds(181, -57, 73, 222); 
     contentPane.add(Dealer1); 

     JLabel Dealer2 = new JLabel(""); 
     Dealer2.setIcon(new ImageIcon("E:\\Noahs Coding\\Blackjack\\Cards\\40.gif")); 
     Dealer2.setBounds(264, -57, 73, 222); 
     contentPane.add(Dealer2); 

     JLabel playerNumber = new JLabel("PLAYER HAND"); 
     playerNumber.setFont(new Font("Comic Sans MS", Font.BOLD, 14)); 
     playerNumber.setBounds(10, 295, 395, 35); 
     contentPane.add(playerNumber); 

     JButton hitButton = new JButton("Hit"); 
     hitButton.setFont(new Font("Yu Gothic UI Semibold", Font.BOLD, 20)); 
     hitButton.setBounds(428, 398, 140, 50); 
     contentPane.add(hitButton); 

     JButton standButton = new JButton("Stand"); 
     standButton.setFont(new Font("Yu Gothic UI Semibold", Font.BOLD, 20)); 
     standButton.setBounds(578, 398, 140, 50); 
     contentPane.add(standButton); 

     hitButton.addActionListener(new ActionListener(){ 


      public void actionPerformed(ActionEvent e) { 

       for(; increase < 5;){ 

        increase++; 

        System.out.println("slot is equal to: " + increase); 

        break; 

       }if (increase == 5){ 

        System.out.println("You cannot hit anymore"); 


        System.out.println("slot is equal to: " + increase); 


       } 

      } 



     }); 
    } 
} 

我很少看節目的樣子: Image

我將球員們開始和每次「打」按鈕被按下它會增加一個隨機卡時間空球員手中。我開始創建新的imageicons但是必須有不同的方式。

回答

0

所以我做了一些嘗試,我想我想出了一個解決方案!

我現在在一個隨機數使用電話:

    card = (int) (1 + Math.random() * 52); 

我也改變了我所有的JLabel到:

 holder1Card1.setIcon(new ImageIcon("resources\\" + cardNumber + ".gif")); 

cardNumber是一個字符串,我將設置所以當等於一個數字時,卡號將等於相同。