I have some code to draw shapes, and I want to check that the number of shapes drawn are not over the amount of room in the array.
Screenshot of the freezing if necessary: image
Code:
// In the constructor:
// shapeObjects = new Shape[ 10 ]; // Example length of array contains ten shapes
// numOfShapes = 0;
public void paintComponent( Graphics g ) {
// Inherit the paintComponent method from the superclass
super.paintComponent( g );
try {
// Check whether or not the shape array is already full
if( numOfShapes == shapeObjects.length ) {
throw new ArrayIndexOutOfBoundsException();
} else {
// If shape is null, do nothing
if ( shape == null ) {
return;
} else {
// Otherwise, draw the shapes
for( int i = 0; i < numOfShapes; i++ ) {
shapeObjects[i].draw( g );
}
shape.draw( g );
}
}
} catch( ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException ) {
JOptionPane.showMessageDialog( null, "Number of shapes drawn cannot exceed 100.", "Error", JOptionPane.ERROR_MESSAGE );
}
}
Comment