Ten Red Circles JApplet

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.

Ten Red Circles JApplet

Here is the completed applet. Look over how the named values (constants and variables) were used in computing the values for X and Y.

import javax.swing.JApplet;
import java.awt.*;

// Assume that the drawing area is 300 by 150.
// Draw ten red circles side-by-side across the drawing area.
public class TenCircles extends JApplet
{
  final int width = 300, height = 150;

  public void paint ( Graphics gr )
  { 
    gr.setColor( Color.red );

    int count =  0 ;
    while (  count < 10  )
    {
      int diameter = width/10;

      int X = count*diameter;           // the left edge of each square
      int Y = height/2 - diameter/2;    // the top edge of the squares

      gr.drawOval( X, Y, diameter, diameter );
      count = count + 1; 
    }

  }
}

Here is what the applet does:

Embedded Java applet removed: modern browsers do not support Java applets. The surrounding source code is retained for historical study.
Not all browsers can run applets. If you see this, yours can not. You should be able to continue reading these lessons, however.

The arithmetic we used is a bit tedious. If you got lost, go back and review. Or just move on. Although arithmetic like this is typical of graphics programming it is not important that you work through every detail of this example.

QUESTION 14:

Look at the statements inside of the loop body. Are there any that do exactly the same thing for each iteration of the loop?