package rolldice;

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

public class Die extends JComponent {
    private static final int SPOT_DIAM = 9;

    private int _faceValue;

    public Die() {
        setPreferredSize(new Dimension(60,60));
        roll();
    }

    public int roll() {
        int val = (int)(6*Math.random() + 1);
        setValue(val);
        return val;
    }

    public int getValue() {
        return _faceValue;
    }

    public void setValue(int spots) {
        _faceValue = spots;
        repaint();
    }

    @Override public void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getHeight();

        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setColor(Color.BLUE);
        g2.fillRect(0, 0, w, h);
        g2.setColor(Color.GREEN);

        g2.drawRect(0, 0, w-1, h-1);

        switch (_faceValue) {
            case 1:
                drawSpot(g2, w/2, h/2);
                break;
            case 3:
                drawSpot(g2, w/2, h/2);
            case 2:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                break;
            case 5:
                drawSpot(g2, w/2, h/2);
            case 4:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                drawSpot(g2, 3*w/4, h/4);
                drawSpot(g2, w/4, 3*h/4);
                break;
            case 6:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                drawSpot(g2, 3*w/4, h/4);
                drawSpot(g2, w/4, 3*h/4);
                drawSpot(g2, w/4, h/2);
                drawSpot(g2, 3*w/4, h/2);
                break;
        }
    }

    private void drawSpot(Graphics2D g2, int x, int y) {
        g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
    }
}

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

the code must keep the die keeps on rolling when the roll has been click and it will just stop if the stopb button i s clicked.

but my code just roll the dice when it is clicked.
please modify it. thank you for your help.

package rolldice;

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

public class RollDice extends JApplet {

    public RollDice() {
        this.setContentPane(new RollDicePanel());
    }

    public static void main(String[] args) {
        JFrame window = new JFrame("-Dice-");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(new RollDicePanel());

        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }
}
package rolldice;

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

public class RollDicePanel extends JPanel {
    private Die _leftDie;
    private Die _rightDie;

    RollDicePanel() {
        _leftDie  = new Die();
        _rightDie = new Die();

        JButton rollButton = new JButton("Roll");
        rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24));

        JButton stopButton = new JButton ("Stop");
        stopButton.setFont(new Font("Sansserif", Font.PLAIN, 24));




        rollButton.addActionListener(new RollListener());

        this.setLayout(new BorderLayout(10, 10));
        this.add(stopButton,BorderLayout.SOUTH);
        this.add(rollButton, BorderLayout.NORTH);
        this.add(_leftDie , BorderLayout.WEST);
        this.add(_rightDie, BorderLayout.EAST);

        this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    }


    private class RollListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            _leftDie.roll();
            _rightDie.roll();

        }
    }
}
package rolldice;

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

public class Die extends JComponent {
    private static final int SPOT_DIAM = 9;

    private int _faceValue;

    public Die() {
        setPreferredSize(new Dimension(60,60));
        roll();
    }

    public int roll() {
        int val = (int)(6*Math.random() + 1);
        setValue(val);
        return val;
    }

    public int getValue() {
        return _faceValue;
    }

    public void setValue(int spots) {
        _faceValue = spots;
        repaint();
    }

    @Override public void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getHeight();

        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setColor(Color.BLUE);
        g2.fillRect(0, 0, w, h);
        g2.setColor(Color.GREEN);

        g2.drawRect(0, 0, w-1, h-1);

        switch (_faceValue) {
            case 1:
                drawSpot(g2, w/2, h/2);
                break;
            case 3:
                drawSpot(g2, w/2, h/2);
            case 2:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                break;
            case 5:
                drawSpot(g2, w/2, h/2);
            case 4:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                drawSpot(g2, 3*w/4, h/4);
                drawSpot(g2, w/4, 3*h/4);
                break;
            case 6:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                drawSpot(g2, 3*w/4, h/4);
                drawSpot(g2, w/4, 3*h/4);
                drawSpot(g2, w/4, h/2);
                drawSpot(g2, 3*w/4, h/2);
                break;
        }
    }

    private void drawSpot(Graphics2D g2, int x, int y) {
        g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
    }
}

please modify it. thank you for your help.

No, that's not how it works here. Nobody is going to do your homework for you.
I gave you some pointers in my last post. Now its up to you to do some work. Check out the timer class I told you about. Read the tutorial I linked to. Learn how to code this for yourself.

No James, you may be misunderstanding. That's not what it should be. The person wants a user to click on "roll" button to start rolling and then click on "stop" button to stop the roll right away. So Timer should not be used in here.

The problem is in RollDicePanel's actionPerformed() method content. Currently, you just call the "roll()" regardless which button is clicked. You need to modify it by checking which button (source) is being called. Read this tutorial http://download.oracle.com/javase/tutorial/uiswing/events/intro.html and try to understand how to work with events from actionPerformed().

Edited 5 Years Ago by Taywin: n/a

OK, but what code needs to be executing between the roll and the stop button events? That's an animation and it needs a timer. You can't just leave it to run at whatever speed your CPU goes at. The roll button just starts the timer and the stop button stops it.

I think the OP got the code from his/her instructor. The Timer would definitely enhance the display, but that is up to the OP. If the requirement doesn't require the enhancement, then it would not be necessary for now. :)

So what are you suggesting to achieve "the die keeps on rolling when the roll has been click"?
You can't call roll() in a loop without starting a new thread. You can't use a simple loop without some kind of timing because it will run at a frenzied and unpredictable speed. You shouldn't put sleep calls in to slow down the loop because that's just bad Swing practice.
There's really one one sensible way to do this and that's for the roll button to start a timer that calls roll() regularly until the stop button is pressed.
... but seriously, if you have another solution in mind it would be interesting to share it.

Oh, you are right. I thought the person has already gotten it rolling and wants to stop it. Sorry.

PS: The OP could implement his/her own thread inside to display animation, but that would be too difficult for now.

Edited 5 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.