2016-10-27 13 views
0

ハイローのダイスゲームを設定しています。ダイスをリロールするボタンを押したときに、自分のダイスを再描画したり再描画したりするのに問題があります。ハイローゲームでダイスを再ペイントする

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class DieFrame extends JFrame { 

private DiePanel diePanel; 
private int die1; 
private int die2; 
private JRadioButton high; 
private JRadioButton low; 
private JRadioButton sevens; 
private JComboBox amountCombo; 
private JLabel balance; 
private JButton throwDice; 

public DieFrame() { 

    diePanel = new DiePanel(); 
    add(diePanel, BorderLayout.CENTER); 
    createControlPanel(); 
    pack(); 
} 

public void createControlPanel() { 

    JPanel betType = createRadioButtons(); 
    JPanel betAmount = createCheckBox(); 
    JPanel balanceAmount = createBalanceLabel(); 
    JPanel diceThrow = createThrowButton(); 

    class ButtonListener implements ActionListener { 

     //@Override 
     public void actionPerformed(ActionEvent e) { 
      //setRollDice(); 
      if (throwDice.isSelected()) { 
       //diePanel.removeAll(); 
       diePanel.rolling(); 
       diePanel.repaint(); 

      } 
     } 
    } 
    JPanel controlPanel = new JPanel(); 

    ActionListener listener = new ButtonListener(); 
    throwDice.addActionListener(listener); 
    controlPanel.setLayout(new FlowLayout()); 
    controlPanel.add(betType); 
    controlPanel.add(betAmount); 
    controlPanel.add(balanceAmount); 
    controlPanel.add(diceThrow); 

    add(controlPanel, BorderLayout.SOUTH); 
} 

public JPanel createRadioButtons() { 
    high = new JRadioButton("High"); 
    low = new JRadioButton("Low"); 
    sevens = new JRadioButton("Sevens"); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(high); 
    group.add(low); 
    group.add(sevens); 

    JPanel panel = new JPanel(); 
    panel.setLayout(new GridLayout(3, 1)); 
    panel.add(high); 
    panel.add(low); 
    panel.add(sevens); 

    return panel; 
} 

public JPanel createCheckBox() { 
    amountCombo = new JComboBox(); 
    amountCombo.addItem("£1"); 
    amountCombo.addItem("£5"); 
    amountCombo.addItem("£10"); 
    amountCombo.setEditable(false); 

    JPanel panel = new JPanel(); 
    panel.add(amountCombo); 
    return panel; 

} 

public JPanel createBalanceLabel() { 
    balance = new JLabel("Balance="); 

    JPanel panel = new JPanel(); 
    panel.add(balance); 
    return panel; 
} 

public JPanel createThrowButton() { 
    throwDice = new JButton("Throw Dice"); 
    //throwDice.addActionListener(listener); 
    JPanel panel = new JPanel(); 
    panel.add(throwDice); 
    return panel; 
} 
} 

フレームクラスでボタンが表示されます。 actionlistenerセクションにはさまざまな実験が行われているため、いくつかのコメントがコメントアウトされているのか、まったく間違っているのでしょう。

public class DiePanel extends JPanel { 

private int value1; 
private int value2; 
private static final int PANEL_WIDTH = 400; 
private static final int PANEL_HEIGHT = 200; 

public DiePanel() { 
    value1 = new Dice(6).getFaceValue(); 
    value2 = new Dice(6).getFaceValue(); 
    setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); 
} 

public void rolling() { 
    removeAll(); 
    repaint(); 
} 

public void paintComponent(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 
    DiceFace dice1 = new DiceFace(75, 30, 30, value1); // s, x, y, v 
    dice1.draw(g2); 
    DiceFace dice2 = new DiceFace(75, 140, 30, value2); // s, x, y, v 
    dice2.draw(g2); 

} 

} 

DiePanelはDiceFrameとダイスクラスからサイコロを構築します。私はDiePanelで再ペイント方法を働かせる必要があることを知っている、私はそれについてどうやって行くのか分からない。私は似たような質問があることを知っていますが、まだ役に立たないものは何も見つかりませんでした。 すべて正常に動作します。ダイスが表示され、すべてのボタンが表示され、再ペイントは何もしません。 私は完全な間違った方向にあるかもしれませんが、どんな助けも大いに評価されるでしょう。

小さなアップデート

void rolling(int dice1, int dice2) { 
     value1 = dice1; 
     value2 = dice2; 
     repaint(); 
} 

私は上記のコードにローリング方式を変更しました。

public void actionPerformed(ActionEvent event) { 
      //setRollDice(); 
      throwDice.isSelected(); 
       //diePanel.removeAll(); 
       diePanel.rolling(6, 6); 

これはDieFrameクローズです。これにより私はダイスを一度巻き戻すことができますが、出力は明らかに6組2組です。私はどのようにフレームにランダムなダイを持って来るか分からない。助けてください。

答えて

0

まず、ビジュアルモデルからデータモデルを分離する必要があります。

第2に、createControlPanel()メソッドのように、クラス内でのみ使用されるメソッドはprivateである必要があります。

第3に、データモデルから、必要な情報をビジュアルモデルに抽出します。 repaint()リクエストがある場合、基本的にはデータモデルを常に更新しています。

public class DiePanel extends JPanel { 

    private static final int PANEL_WIDTH = 400; 
    private static final int PANEL_HEIGHT = 200; 

    private int value1; 
    private int value2; 

    private DiceFace dice1; 
    private DiceFace dice2; 

    public DiePanel() { 
     value1 = new Dice(6).getFaceValue(); 
     value2 = new Dice(6).getFaceValue(); 
     setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); 
    } 

    public void rolling() { 
     dice1 = new DiceFace(75, 30, 30, value1); // s, x, y, v 
     dice2 = new DiceFace(75, 140, 30, value2); // s, x, y, v 
     repaint(); 
    } 

    public void paintComponent(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     dice1.draw(g2); 
     dice2.draw(g2); 
    } 

} 

私は助けてくれることを願っています。

よろしくお願いいたします。 :)

+0

おかげさまで、ありがとうございました。出力の一部の要素が消えたばかりです。私はその間に若干の成功を収めましたが、もう一度立ち往生しました。 – JimmyPop13

関連する問題