Die.java

public class Die
{
    private int value;

    public Die()
    {
        this.rollDie();
    }

    public void rollDie()
    {
        value = (int)(Math.random()*6)+1;
    }

    public int getValue()
    {
        return(value);
    }

    public String toString()
    {
        return(""+value);
    }
}

Dice.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
    
/**
 * Applet class Dice displays a pair of dice.
 *
 * @author William Austad 
 * @version 10/23/02
 */
public class Dice extends Applet 
{
    // instance variables - replace the example below with your own
    private Die die;
    private Button rollButton;
    
    /**
     * Called by the browser or applet viewer to inform this Applet that it
     * has been loaded into the system. It is always called before the first 
     * time that the start method is called.
     */
    public void init()
    {
        die = new Die();
        rollButton = new Button("Roll");
        add(rollButton);
        rollButton.addActionListener(new RollListener());
    }

    /**
     * Here, the drawing of the applet gets done. 
     * "paint" gets called everytime the applet should be 
     * drawn on the screen.
     *
     * @param  g  the Graphics object for this applet
     */
    public void paint(Graphics g)
    {
        // simple text displayed on applet
        g.setColor(Color.white);
        g.fillRect(0, 0, 300, 300);
        g.setColor(Color.black);
        paintDie(g, 20);
        paintDie(g, 150);
    }

    /**
     * displays a Die on the screen
     * @param g the Graphics object for this applet
     * @param xOffset The X-coordinate of the upper left
     * hand corner of the Die 
     */
    private void paintDie(Graphics g, int xOffset)
    {
        g.drawRect(xOffset, 60, 120, 120);
        switch(die.getValue()) {
        case 1:
            g.fillOval(xOffset + 50, 110, 20, 20);
            break;
        case 2:
            g.fillOval(xOffset + 30, 110, 20, 20);
            g.fillOval(xOffset + 70, 110, 20, 20);
            break;
        case 3:
            g.fillOval(xOffset + 20, 110, 20, 20);
            g.fillOval(xOffset + 50, 110, 20, 20);
            g.fillOval(xOffset + 80, 110, 20, 20);
            break;
        case 4:
            g.fillOval(xOffset + 30, 85, 20, 20);
            g.fillOval(xOffset + 70, 85, 20, 20);           
            g.fillOval(xOffset + 30, 135, 20, 20);
            g.fillOval(xOffset + 70, 135, 20, 20);           
            break;
        case 5:
            g.fillOval(xOffset + 50, 110, 20, 20);
            g.fillOval(xOffset + 30, 85, 20, 20);
            g.fillOval(xOffset + 70, 85, 20, 20);           
            g.fillOval(xOffset + 30, 135, 20, 20);
            g.fillOval(xOffset + 70, 135, 20, 20);    
            break;            
        case 6:
            g.fillOval(xOffset + 20, 85, 20, 20);
            g.fillOval(xOffset + 50, 85, 20, 20);
            g.fillOval(xOffset + 80, 85, 20, 20);
            g.fillOval(xOffset + 20, 135, 20, 20);
            g.fillOval(xOffset + 50, 135, 20, 20);
            g.fillOval(xOffset + 80, 135, 20, 20);
            break;
        }
        die.rollDie(); // update value after displaying
    }

    /**
     * Returns information about this applet. 
     * 
     * @return a String representation of information about this Applet
     */
    public String getAppletInfo()
    {
        // replace this with your own info
        return "Title: Dice Applet Demo\n" + 
               "Author: William Austad\n" +
               "An applet rolls two dice.";
    }
    
    private class RollListener implements ActionListener
    {
        /**
         * repaints the screen whenever the button is pushed
         * @param event the ActionEvent
         */    
        public void actionPerformed(ActionEvent event)
        {
            // since only one widget this should be true
            if(event.getSource() == rollButton) {
                repaint();
            }
        }
    }
}

Last modified: 12/22/03, WKA