// Make it easy to access classes and methods of selected packages. import java.awt.*; import java.awt.event.*; import java.applet.*; /** * This class subclasses Applet to define an applet for a virtual die. It * implements the ActionListener interface to respond to action events * for the die (which is a Button extension). * * Written by: Jon Huhtala */ public class App extends Applet implements ActionListener { // Instance variables for referencing the die. private Die die; // This method defines initial (one-time) applet processing. public void init() { // Specify that a border layout is to be used. setSize(100, 105); setLayout(new BorderLayout()); // Instantiate a blue die, register its listener, and add it to the // center of the applet frame. die = new Die(Color.blue); die.addActionListener(this); add(die, BorderLayout.CENTER); } // This implements the abstract event handler method of the ActionListener // interface to handle ActionEvents (button clicks). When the user clicks // the die, it constitutes a roll of the die. public void actionPerformed(ActionEvent e) { die.roll(); } } // This class subclasses Button to encapsulate the processing and // displaying of a single die having Button characteristics. class Die extends Button { // Instance variables for holding the die's value and color. private byte dieValue; private Color dieColor; // This constructor instantiates a die object in the specified color. public Die(Color initColor) { // Call the superclass (Button) constructor, set the die color, and // give the die its first roll. super(""); dieColor = initColor; roll(); } // This method simulates rolling the die. public void roll() { // Randomly determine a new value for the die and redraw it. dieValue = randomValue(); repaint(); } // This method may be called to change the color of the die. public void setColor(Color newColor) { dieColor = newColor; } // This method may be called to get the color of the die. public Color getColor() { return dieColor; } // This method may be called to get the current value of the die. public byte getValue() { return dieValue; } // This method can ONLY be called from within this class. It returns a // random number from 1 to 6 to simulate rolling the die. private byte randomValue() { // Calling the random() method of the Math class returns a real number // greater than zero and less than one. Multiplying by 1000 and taking // the remainder from division by six produces a random integer from // zero to five. Adding one results in the desired value but a cast is // needed to convert it to a byte. return (byte) (((1000 * Math.random()) % 6) + 1); } // This method is automatically called whenever the die needs to be // rendered. It receives a Graphics object (its context) as a parameter. public void paint(Graphics g) { // Determine the size of the die. Dimension dieSize = this.getSize(); // Draw the blank die. g.setColor(dieColor); g.fill3DRect(0, 0, dieSize.width, dieSize.height, true); // Set the spot color. g.setColor(Color.white); // Calculate the spot size. This makes the spots "scaleable". int spotHeight = dieSize.height / 12; int spotWidth = dieSize.width / 12; // Draw the spots based upon the die's value. switch(dieValue) { // Draw the spot pattern for a one. case 1: g.fillOval((dieSize.width/2)-spotWidth, (dieSize.height/2)-spotHeight, spotWidth*2, spotHeight*2); break; // Draw the spot pattern for a two. case 2: g.fillOval((dieSize.width/4)-spotWidth, (dieSize.height/4)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, ((dieSize.height/4)*3)-spotHeight, spotWidth*2, spotHeight*2); break; // Draw the spot pattern for a three. case 3: g.fillOval((dieSize.width/4)-spotWidth, (dieSize.height/4)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval((dieSize.width/2)-spotWidth, (dieSize.height/2)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, ((dieSize.height/4)*3)-spotHeight, spotWidth*2, spotHeight*2); break; // Draw the spot pattern for a four. case 4: g.fillOval((dieSize.width/4)-spotWidth, (dieSize.height/4)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, (dieSize.height/4)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval((dieSize.width/4)-spotWidth, ((dieSize.height/4)*3)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, ((dieSize.height/4)*3)-spotHeight, spotWidth*2, spotHeight*2); break; // Draw the spot pattern for a five. case 5: g.fillOval((dieSize.width/4)-spotWidth, (dieSize.height/4)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, (dieSize.height/4)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval((dieSize.width/2)-spotWidth, (dieSize.height/2)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval((dieSize.width/4)-spotWidth, ((dieSize.height/4)*3)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, ((dieSize.height/4)*3)-spotHeight, spotWidth*2, spotHeight*2); break; // Draw the spot pattern for a six. case 6: g.fillOval((dieSize.width/4)-spotWidth, (dieSize.height/4)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, (dieSize.height/4)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval((dieSize.width/4)-spotWidth, (dieSize.height/2)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, (dieSize.height/2)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval((dieSize.width/4)-spotWidth, ((dieSize.height/4)*3)-spotHeight, spotWidth*2, spotHeight*2); g.fillOval(((dieSize.width/4)*3)-spotWidth, ((dieSize.height/4)*3)-spotHeight, spotWidth*2, spotHeight*2); break; } } }