1

I want to create a Dice Rolling Simulator. This is what I've gotten so far.

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

I have gotten a JFrame, JLabel, and the Button which says roll, the simple things.

What I am trying to figure out is how to create two dice in the JPanel, and also how to make it roll when the button "ROLL" is clicked, using math.Random and Graphics.

I would appreciate if it was as simple as possible, since I am not very advanced in the programming world and recently started. I would appreciate it if you would try to explain as detailed as possible before you give me the answer, so that I have a chance to try and figure it out myself beforehand.

Thanks!

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
Baelynn
  • 11
  • 4

2 Answers2

1

Like fd. said, you need components for the die. Let's use JLabel as the components. They can be arranged like this:

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

You can set an ImageIcon on the label (Check out: Java: how to add image to Jlabel?), so create 6 images of the different positions of the dice. When the button is pushed, a random number (between 1 and 6) will be generated (using Math.random). Each number represents an image. Set the image of the JLabel based on this number.

For this to happen you need a ActionListener. Create a custom ActionListener like below (Note I did it for one die):

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

Each time the button is pressed, the ActionPerformed method will be invoked and randomly change the icons of each JLabel, simulating a roll of the die.

To add your custom ActionListener to the button:

roll.addActionListener(new RollDiceActionListener(die));

The ActionListener needs to modify the Jlabels representing the dice, so don't forget to add it as a parameter to the constructor of you listener.

Hope this helps. Good luck!

Community
  • 1
  • 1
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
0

If we ignore having some kind of custom draw/animation for the dice for now, then your code is missing a few functional elements:

  • instantiating objects for the UI components for the 2 dice
  • adding those components to your layout (you may need to create a nested layout object here as well, depending on how you'd like them positioned)
  • defining an ActionListener object to generate the random numbers for your dice rolls and update the dice UI components
  • adding the ActionListener to your roll button
Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46