Completed Applet

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:

The completed applet is given below.

Completed Applet

The only tricky thing is to get the details right for selecting the random integers you want.

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

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

      int x = Math.abs( rand.nextInt() )% 400;
      int y = Math.abs( rand.nextInt() )% 400;

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

      count = count + 1;
    }
  }
}

Here is what the applet draws:

Embedded Java applet removed: modern browsers do not support Java applets. The surrounding source code is retained for historical study.


QUESTION 17:

Click the "refresh" button on the Web browser a few times. Is the drawing always the same?