2015-04-23 82 views
1

我想創建一個骰子滾動模擬器。這是迄今爲止我所得到的。JAVA BEGINNERS創建和滾動骰子

public class RollDice { 
    private static class rollDice extends JPanel { 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      // custom draw code goes here 
     } 
    } 

    public static void main(String[] args) { 
     JLabel message = new JLabel ("\nRoll the Die!\n", JLabel.CENTER); 
     message.setForeground(Color.BLACK); 
     message.setBackground(Color.WHITE); 
     message.setFont(new Font("Courier", Font.PLAIN, 25)); 
     message.setOpaque(true); 

     JButton roll = new JButton("ROLL"); 

     JPanel content = new JPanel(); 
     content.setLayout(new BorderLayout()); 
     content.add(message, BorderLayout.NORTH); 
     content.add(roll, BorderLayout.SOUTH); 

     JFrame window = new JFrame("Roll Dice"); 
     window.setContentPane(content); 
     window.setSize(250,300); 
     window.setLocation(300,300); 
     window.setVisible(true); 
    } 
} 

我已經得到了一個JFrame,JLabel和Button,它說roll,簡單的東西。

我想弄明白的是如何在JPanel中創建兩個骰子,以及如何在點擊「ROLL」按鈕時使用math.Random和Graphics來使它滾動。

如果它儘可能簡單,我將不勝感激,因爲我在編程領域並不是很先進,而且最近纔開始。如果您在給我答案之前儘可能詳細地解釋,我將不勝感激,以便我有機會事先嚐試自己弄清楚。

謝謝!

回答

0

如果我們忽略有某種自定義繪製/動畫骰子現在,那麼你的代碼缺少一些功能元件:

  • 實例化對象的UI組件的2個骰子
  • 將這些組件添加到您的佈局中(您可能還需要在這裏創建一個嵌套佈局對象,具體取決於您希望它們如何定位)
  • 定義一個ActionListener對象來生成骰子滾動的隨機數並更新骰子UI組件
  • ActionListener添加到您的滾動按鈕
+0

讓我知道如果您有任何問題(在這裏添加評論),我會改進我的答案。 –

1

像fd。說,你需要模具的組件。我們使用JLabel作爲組件。他們可以安排是這樣的:

+==============================+ 
|  Roll the Die!   | 
| +---------+ +---------+ | 
| |   | |   | | 
| | dice1 | | dice2 | | 
| |   | |   | | 
| +---------+ +---------+ | 
| +--------------------------+ | 
| |   ROLL   | | 
| +--------------------------+ | 
+==============================+ 

您可以設置標籤上ImageIcon(查看:Java: how to add image to Jlabel?),所以創建骰子的不同位置的6幅圖像。按下按鈕時,將生成一個隨機數(1到6之間)(使用Math.random)。每個數字代表一個圖像。根據這個數字設置JLabel的圖像。

要發生這種情況,您需要一個ActionListener。創建自定義ActionListener像下面(注意我是做一個管芯):

public class RollDiceActionListener implements ActionListener { 

    private JLabel dice; 

    public RollDiceActionListener(JLabel dice) { 
     this.dice = dice; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     int rnd = (int)(Math.random() * 6) + 1; 

     switch (rnd) { 
     case 1: 
      dice.setIcon(new ImageIcon("/path/to/dice_1.png")); 
      break; 
     case 2: 
      dice.setIcon(new ImageIcon("/path/to/dice_2.png")); 
      break; 
     case 3: 
      dice.setIcon(new ImageIcon("/path/to/dice_3.png")); 
      break; 
     case 4: 
      dice.setIcon(new ImageIcon("/path/to/dice_4.png")); 
      break; 
     case 5: 
      dice.setIcon(new ImageIcon("/path/to/dice_5.png")); 
      break; 
     case 6: 
      dice.setIcon(new ImageIcon("/path/to/dice_6.png")); 
      break; 
     } 
    } 
} 

每個按鈕被按下時,ActionPerformed方法將被調用和隨機地改變每個JLabel的圖標時,模擬的一個輥死。

爲了將定製ActionListener添加到按鈕:

roll.addActionListener(new RollDiceActionListener(die)); 

ActionListener需要修改表示骰子的Jlabel S,所以不要忘了將它作爲一個參數添加到您監聽器的構造。

希望這會有所幫助。祝你好運!