Three Circles Drawn with One Object

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 method is given below:

Three Circles Drawn with One Object

Access methods often are simple to write.

  // change the center of the circle to a new X and Y
  void setPosition( int newX, int newY )
  {
    x = newX ;
    y = newY ;
  }

  // chage the radius of the circle
  void setRadius( int newR )
  {
    radius = newR ;
  }

Here is a new test applet, that draws concentric circles in the center of the drawing area:

// assume that the drawing area is 200 by 200
public class Test1Circle extends JApplet
{
  Circle circ = new Circle();
  
  public void paint ( Graphics gr )
  { 
    circ.setPosition( 100, 100 );

    circ.setRadius( 10 );
    circ.draw( gr );

    circ.setRadius( 20 );
    circ.draw( gr );

    circ.setRadius( 30 );
    circ.draw( gr );
  }
}

Here is what it draws:


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

This is the same picture as previously, but because of the increased "object-ness" of the program is (perhaps) easier to understand.

QUESTION 10:

Propose a way to draw 10 concentric circles.