如何"冻结"滚动的某些骰子?

时间:2014-10-27 19:22:46

标签: java checkbox dice

我正在创建一个骰子程序,它已经完成了。我唯一要做的就是能够点击相应骰子的复选框,这将骰子免于滚动,所以我可以滚动那些没有检查。我该怎么做呢?这是我的代码:

主要模具类:



public class Die {

  public static void main(String[] args) {}
  protected int face = 1;

  void roll() {
    face = (int)(Math.random() * 6 + 1);
  }

  public int getFace() {
    return face;
  }

  public int setFace() {
    return face;
  }




我在其中绘制所有内容并设置方法的图形类:



public class Graphics extends Die {
  private int x, y;
  boolean locked; {

    locked = !locked;
  }


  public Graphics(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public void draw(DrawingKit dk) {
    Rectangle2D.Float die1 = new Rectangle2D.Float(x, y, 80, 80);

    Ellipse2D.Float topleft = new Ellipse2D.Float(x + 3, y + 3, 18, 18);
    Ellipse2D.Float topright = new Ellipse2D.Float(x + 55, y + 3, 18, 18);
    Ellipse2D.Float middleleft = new Ellipse2D.Float(x + 3, y + 28, 18, 18);
    Ellipse2D.Float middle = new Ellipse2D.Float(x + 28, y + 28, 18, 18);
    Ellipse2D.Float middleright = new Ellipse2D.Float(x + 55, y + 28, 18, 18);
    Ellipse2D.Float bottomleft = new Ellipse2D.Float(x + 3, y + 53, 18, 18);
    Ellipse2D.Float bottomright = new Ellipse2D.Float(x + 55, y + 53, 18, 18);
    dk.setPaint(Color.red);
    dk.fill(die1);
    if (face > 1) {
      dk.draw(topleft);
      dk.setPaint(Color.black);
      dk.fill(topleft);
    }
    if (face > 3) {
      dk.draw(topright);
      dk.setPaint(Color.black);
      dk.fill(topright);
    }
    if (face == 6) {
      dk.draw(middleleft);
      dk.setPaint(Color.black);
      dk.fill(middleleft);
    }
    if (face % 2 == 1) {
      dk.draw(middle);
      dk.setPaint(Color.black);
      dk.fill(middle);
    }
    if (face == 6) {
      dk.draw(middleright);
      dk.setPaint(Color.black);
      dk.fill(middleright);
    }
    if (face > 3) {
      dk.draw(bottomleft);
      dk.setPaint(Color.black);
      dk.fill(bottomleft);
    }
    if (face > 1) {
      dk.draw(bottomright);
      dk.setPaint(Color.black);
      dk.fill(bottomright);
    }
  }



  public static void main(String[] args) {

  }
}




滚动类,其中包含复选框和所有内容:



public class Roll {


  public static void main(String[] args) {

    JPanel topPanel = new JPanel();
    Icon Dice = new ImageIcon("button.png");
    final JButton button1 = new JButton(Dice);
    JCheckBox Dice1 = new JCheckBox("Dice 1");
    Dice1.setBounds(100, 100, 100, 100);
    JCheckBox Dice2 = new JCheckBox("Dice 2");
    JCheckBox Dice3 = new JCheckBox("Dice 3");
    JCheckBox Dice4 = new JCheckBox("Dice 4");
    JCheckBox Dice5 = new JCheckBox("Dice 5");

    int cnt1 = 1, cnt2 = 2, cnt3 = 3, cnt4 = 4, cnt5 = 5, cnt6 = 6;

    final DrawingKit dk = new DrawingKit("Dice Game");
    dk.addPanel(topPanel);
    dk.setBackground(Color.blue);
    topPanel.setBackground(Color.red);
    topPanel.setSize(500, 500);

    final Graphics die1 = new Graphics(0, 45);
    die1.roll();
    die1.draw(dk);
    final Graphics die2 = new Graphics(100, 45);
    die2.roll();
    die2.draw(dk);
    final Graphics die3 = new Graphics(200, 45);
    die3.roll();
    die3.draw(dk);
    final Graphics die4 = new Graphics(300, 45);
    die4.roll();
    die4.draw(dk);
    final Graphics die5 = new Graphics(400, 45);
    die5.roll();
    die5.draw(dk);

    topPanel.add(button1);
    button1.setToolTipText("Click this button to roll the dice.");
    button1.setForeground(Color.red);
    button1.setContentAreaFilled(false);
    button1.setFocusPainted(false);
    button1.setBorderPainted(false);
    button1.setFont(new Font("Arial", Font.BOLD, 15));
    button1.setPreferredSize(new Dimension(40, 25));
    button1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        die1.roll();
        die1.draw(dk);
        die2.roll();
        die2.draw(dk);
        die3.roll();
        die3.draw(dk);
        die4.roll();
        die4.draw(dk);
        die5.roll();
        die5.draw(dk);


      }


    });

    topPanel.add(Dice1);

    Dice1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {




      }


    });
    topPanel.add(Dice2);
    topPanel.add(Dice3);
    topPanel.add(Dice4);
    topPanel.add(Dice5);

  }
}




正如您所看到的,我已经为复选框1准备了动作监听器,我只是不知道放在括号内的内容。我知道它与我在图形类中的锁定布尔方法有关,但我无法弄清楚要做什么,我需要一些帮助。

由于

1 个答案:

答案 0 :(得分:0)

一种可能的方法是在代表锁定的类Die中添加一个新字段:

public class Die {
    // omitted existing fields
    private boolean locked = false;

    void roll() {
        if (!locked) {
            face = (int)(Math.random() * 6 + 1);
        }
    }
    // omitted existing getter and setter

    public void setLocked(final boolean locked) {
        this.locked = locked;
    }

    public boolean isLocked() { // this getter is maybe not necessary
        return locked;
    }
}

此锁定会阻止roll方法更改当前值。

第一个复选框的听众可能如下所示:

Dice1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        die1.setLocked(Dice1.isSelected());
    }
});

此侦听器将复选框的当前状态设置为locked字段。因此,如果选中该复选框,则locked字段设置为true,方法调用die1.roll()不会更改其当前值。

这样您就不必更改当前的roll()draw(dk)来电。

请勿忘记将您的Dice1组件声明为最终组件,否则无法在侦听器中使用它:

final JCheckBox Dice1 = new JCheckBox("Dice 1");

也可能需要将die1声明为final