在高低游戏中重新绘制骰子

时间:2016-10-27 12:31:47

标签: java

我正在设置一个高低的骰子游戏,当我按下应该重新掷骰子的按钮时,我无法让我的骰子重绘或重绘。

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和Dice类构建骰子。我知道我需要在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关闭。这允许我重新掷骰子一次,但输出显然是两组六。我不确定如何将随机骰子带入框架。请帮忙。

1 个答案:

答案 0 :(得分:0)

首先,您应该将数据模型与可视化模型分开。

其次,只在private内使用的方法应该是createControlPanel(),就像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); } } 请求时,您基本上不断更新数据模型。尝试这样的事情:

{{1}}

我希望我有所帮助。

祝你有个愉快的一天。 :)