Finishing the 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.

What is the radius of each circle?

The drawing area has a height of 150. What is the Y coordinate of the top of all the squares?

Answer:

The radius of each circle is half of its diameter.

The horizontal line across the center of the area is at Y=150/2. The top of the squares should be radius pixels above that, or at 150/2 - diameter/2 (remember that Y increases going down.)

Finishing the JApplet

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 =  ;

      int X =  ;    // the left edge of each square
      int Y =  ;    // the top edge of the squares

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

  }
}

So now we have the (X, Y) coordinates of the upper left corner of each of the ten squares: The left edges of the squares are at

X=0, diameter, 2*diameter, 3*diameter, . . . . 9*diameter

and all the Y values are at 150/2 - radius.

The X and Y in the above are local variables for exclusive use of the paint method. It is correct to declare them as is done here. The blanks should be filled in with arithmetic expressions that use identifiers, not integer literals like 10, 150, 300 and so on.

QUESTION 13:

Fill in the blanks. (Hint: make use of count in computing a new X for each loop iteration.)