import java.awt.*; class Die { private int value; private int edgeLength; private Color edgeColor; private Color dotColor; public Die() { value = 1; edgeLength = 100; edgeColor = Color.green; dotColor = Color.black; } /** * @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 eLength, Color eColor, Color dColor ) { if( val >= 1 && val <= 6 ) value = val; else value = 1; if( eLength > 0 ) edgeLength = eLength; else edgeLength = 100; edgeColor = eColor; dotColor = dColor; } /** * Sets the current value of the die if newValue is legal. */ public void setValue( int newValue ) { if( newValue >= 1 && newValue <= 6 ) value = newValue; } /** * Sets the value to a random integer between 1 and 6. */ public void roll() { value = (int)(1 + Math.floor( 6 * Math.random() )); } /** * Draws the die on the given Graphics object. * 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( Graphics g, int x, int y ) { g.setColor( edgeColor ); g.drawRect( x, y, edgeLength, edgeLength ); g.setColor( dotColor ); int diameter = edgeLength / 7; // Upper left dot if( value == 4 || value == 5 || value == 6 ) g.drawOval( x + diameter, y + diameter, diameter, diameter ); // Upper right dot if( value == 2 || value == 3 || value == 4 || value == 5 || value == 6 ) g.drawOval( x + 5*diameter , y + diameter, diameter, diameter ); // Left & right dots if( value == 6 ) { g.drawOval( x + diameter , y + 3*diameter, diameter, diameter ); g.drawOval( x + 5*diameter, y + 3*diameter, diameter, diameter ); } // Middle dot if( value == 1 || value == 3 || value == 5 ) g.drawOval( x + 3*diameter, y + 3*diameter, diameter, diameter ); // Lower left dot if( value == 2 || value == 3 || value == 4 || value == 5 || value == 6 ) g.drawOval( x + diameter , y + 5*diameter, diameter, diameter ); // Lower right dot if( value == 4 || value == 5 || value == 6 ) g.drawOval( x + 5*diameter, y + 5*diameter, diameter, diameter ); } }