go to previous page   go to home page   go to next page

Answer:

Just one:

public void paint ( Graphics gr )

(However, the class inherits many more methods from class JApplet.)


Extending the JApplet Class

//         X -- notice the 'x' in javax
import javax.swing.JApplet;
import java.awt.*;

public class JustOneCircle extends JApplet
{
  final int radius = 25;

  public void paint ( Graphics gr )
  { 
    gr.setColor( Color.white );
    gr.fillRect( 0, 0, 150, 150 );
    gr.setColor( Color.black );

    gr.drawOval( (150/2 - radius), (150/2 - radius), radius*2, radius*2 );
   }
}

The definition of JApplet provides a framework for building an applet. By itself, the class JApplet does little that is visible in the Web browser. (It does a great many things behind the scenes, however.) To build upon this framework, you import javax.swing.JApplet and extend the JApplet class.

When you extend a class, you are making a new class by building upon a base class. This example defines a new class called JustOneCircle . The new class has everything in it that the class JApplet has. (This is called inheritance. Inheritance is discussed at greater length in chapter 50.)

The class JApplet has a paint() method, but that method does little. Objects of class JustOneCircle have their own paint() method because the definition in JustOneCircle.java overrides the one in JApplet.

The Web browser calls the paint() method when it needs to "paint" the section of the monitor screen devoted to an applet. Each applet that you write has its own paint() method.


QUESTION 3:

The paint() method contains the statement

gr.drawOval( (150/2 - radius), (150/2 - radius), radius*2, radius*2 );

What do you suppose that this does? (Ignore the details.)