/** * This class represents a single six-sided die, possibly for * use in game programs such as Yahtzee, craps, etc. * * Modification history: * 1/15/02 (Jeff Ondich) Started * 1/16/02 (JO) Added drawing routines and javadoc comments. * * @author Jeff Ondich */ import javabook.*; import java.awt.*; class Die { private int nSides; private int value; private int edgeLength; private Color edgeColor; private Color dotColor; /** * Used for testing the other methods. */ public static void main( String[] args ) { DrawingBoard board = new DrawingBoard( "Die tester" ); board.setVisible( true ); Die die = new Die( 5, 300 ); die.roll(); die.draw( board, 30, 60 ); } /** * The default constructor sets up reasonable starting values. Note * in particular that the six-sidedness of the die is enforced * here. */ public Die() { nSides = 6; value = 1; edgeLength = 100; edgeColor = Color.green; dotColor = Color.black; } /** * This constructor that allows you to initialize the value and the * lengths of the edges of the square that is drawn by the * draw method. Note that nSides is set to 6. * Also, there's nothing really special about value * and edgeLength that they should be settable in * the constructor. I just wanted you to see that constructors * can be used to initialize some or all of the data values. * * @param val The initial value of the die. * @param edge The length, in pixels, of each of the die's square's sides. * */ public Die( int val, int edge ) { nSides = 6; if( val >= 1 && val <= nSides ) value = val; else value = 1; if( edge > 0 ) edgeLength = edge; else edgeLength = 100; edgeColor = Color.green; dotColor = Color.black; } /** * Returns the current value of the die. */ public int getValue() { return value; } /** * Sets the current value of the die if newValue is legal. */ public void setValue( int newValue ) { if( newValue >= 1 && newValue <= nSides ) value = newValue; } /** * Sets the color of the square drawn by the draw method. */ public void setColor( Color newColor ) { System.out.println( "Die.setColor stub" ); } /** * Sets the color of the dots drawn by the draw method. */ public void setDotColor( Color newColor ) { System.out.println( "Die.setDotColor stub" ); } /** * Sets value to a random integer between 1 and 6. */ public void roll() { System.out.println( "Die.roll stub" ); } /** * Draws the die on the given drawing board. * The colors of the die are specified by edgeColor * and dotColor. * * @param x The x-coordinate of the upper left corner of the die's square. * @param y The y-coordinate of the upper left corner of the die's square. */ public void draw( DrawingBoard board, int x, int y ) { System.out.println( "Die.draw stub" ); } }