Search code examples
javacheckboxdice

How to "Freeze" certain dice from rolling?


I am creating a dice program, and it is done. The only thing I have left to do is being able to click the check box to the corresponding dice which will FREEZE the dice from being rolled, so I can just roll the ones NOT CHECKED. How would I go about doing this? Here is my code:

Main Die Class:

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;
  }

Graphics class where I drew everything and set methods:

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) {

  }
}

Roll Class where checkboxes and everything is ran:

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);

  }
}

As you can see, I have the action listener ready for check box 1, I just don't know what to put inside of the brackets. I know it has something to do with my locked boolean methods in the graphics class, but I can't figure out what to do and I need some help.

Thanks


Solution

  • A possible way would be adding a new field to the class Die that will represent the lock:

    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;
        }
    }
    

    This lock prevents the roll method from changing the current value.

    And your listener for the first checkbox could look like this:

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

    This listener set the current state of the checkbox to the locked field. Therefore, if the checkbox is selected, then the locked field is set to true and method call die1.roll() won't change it's current value.

    That way you don't have to change your current roll() and draw(dk) calls.

    Don't forget to declare your Dice1 component as final, or else it won't be possible to use it inside the listener:

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

    It might also be necessary to declare die1 as final, as well.