public class Main extends JFrame { private Shape[] shapes; // the array of shapes to be randomly filled public static void main(String[] args) { new Main(); } // end main() public Main() { // TODO: Create an array of 100 Shape elements. // TODO: insert code here shapes = new Shape[100]; for (int i = 0; i < 100; i++) { Color color = new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256)); int typeOfObject = (Math.random() < 0.5) ? 0 : 1; int isFilledNum = (Math.random() < 0.5) ? 0 : 1; boolean isFilled; if (isFilledNum == 0) { isFilled = false; } else { isFilled = true; } if (typeOfObject == 0) { shapes[i] = new Rectangle((int) (Math.random() * 800), (int) (Math.random() * 600), (int) (Math.random() * 100), (int) (Math.random() * 100), color, isFilled); } else { shapes[i] = new Circle((int) (Math.random() * 800), (int) (Math.random() * 600), color, isFilled, (int) (Math.random() * 50)); } } setTitle("Abstract Class Demo"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); } // end Main() @Override public void paint(Graphics g) { // Uses a fast enumeration to draw out all the shapes. for (Object s : shapes) { ((Shape) s).draw(g); } } // end paint() } // end Main
Make sure you put parenthesis like this on your typecasted randoms: (int) (Math.random() * 800), otherwise it rounds the random down to 0 before you even multiply.