FileDocCategorySizeDatePackage
DiceRoll.javaAPI DocExample1461Fri Oct 12 10:58:28 BST 2001None

DiceRoll.java

import java.awt.*;		// make the graphics libraries available
import java.applet.*;	// make the applet libraries available
import java.util.*;		// make the random number library available

								// this applet uses a random number generator 
								// to get values for two dice. It could then compare 
								// the dice to see which is the greater.
								// The random number generator returns a value
								// in the range 0.00 - 0.999. We need to convert this
								// into an integer value in the range 1 - 6


public class DiceRoll extends Applet // make a new applet
{
	private int firstDie;		// to hold the value for the first die
	private int secondDie;		// to hold the value for the second die
	
	public void paint(Graphics g)
	{
		firstDie  = (int) (Math.random() * 6) + 1; // get a value for the first dice
		secondDie = (int) (Math.random() * 6) + 1; //get a value for the second dice
										// draw the dice squares
		g.setColor(Color.blue);
		g.fillRect(20, 20, 60, 60);
		g.setColor(Color.magenta);
		g.fillRect(120, 20, 60, 60);
										// put the values on the dice
		g.setColor(Color.white);
		g.drawString(" " + firstDie, 43, 54);
		g.drawString(" " + secondDie, 143, 54);
										// write the values under the dice
		g.setColor(Color.black);
		g.drawString("First dice = " + firstDie, 20, 100); 
		g.drawString("Second Dice = " + secondDie, 120, 100); // print out the dice values
	}
}