Thousands of Circles

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.

Answer:

Use a counting loop with a counter from 1 to 1000. The loop body would use a single Random object to pick random X and Y locations for the center of the circles.

Thousands of Circles

Here is a start on this idea:

// assume that the drawing area is 400 by 400
public class ThousandCircles extends JApplet
{
  Circle circ = new Circle();
  Random rand =  ;
  
  public void paint ( Graphics gr )
  { 

    circ.setRadius(  5 );
    circ.setColor( Color.red );
    int count = 1;
    while ( count <= 1000 )
    {

      int x =  ;
      int y =  ;

      circ.setPosition( x, y );
      circ.draw( gr );

      count = count + 1;
    }
  }
}

To make sure that each circle completely fits within the applet drawing area, make sure that each center's x and y is at least a distance of 5 (the radius) away from an edge. But let us not do that.

QUESTION 16:

Complete the applet by filling in the blanks. The drawing area is going to be 400 by 400.