Java Applet Review

Give examples of all the required Java elements for the project and explain how they work.

Basic Applet Functions

init (required)
This function is called when an applet is first loaded or reloaded.
public class Graph extends Applet {

        static final String message = "Hello World";

        private Font font;

        // one time initialization of the the applet
        public void init(){

                font = new Font("Helvetica",Font.BOLD, 48);

        }
...
paint (optional)
Anytime repaint is called this method is called. Netscape will call this method when it refreshes the screen. Paint is the basic display method which many applets implement to draw the applet's representation within the browser page.
          public class HelloWorld extends Applet {
              public void paint(Graphics g) {
                  g.drawString("Hello world!", 50, 25);
              }
          }
stop (optional)
This function is called to start the applet's execution, such as when the applet's loaded or when the user revisits a page that contains the applet.
stop (optional)
This function is called to stop the applet's execution, such as when the user leaves the applet's page or quits the browser.
destroy (optional)
This function is called to perform a final cleanup in preparation for unloading. An applet is unloaded before it is reloaded with shift-reload.

Writing Text to the Screen

The use of at least one of these is required in the project.
drawString
A function which draws the given string to the given X,Y coordinates. Remember that X goes from left to right and Y goes from top to bottom. The coordinates are in terms of pixels.
Note: same example as with paint above
          public class HelloWorld extends Applet {
              public void paint(Graphics g) {
                  g.drawString("Hello world!", 50, 25);
              }
          }
Label
A class which puts text onto screen.
public void init() {
...
     this.add(new Label("Color: "));
...
}
The add function makes it so the applet will tell the Label to draw itself upon a refresh.

Responding to a button push or mousedown

action
To respond to a button push the applet must have the action method defined.
public class Scribble extends Applet {
   private Button clear_button;               // The clear button.
 
   // This method is called when the user clicks the button or chooses a color.
   public boolean action(Event event, Object arg) {
     // If the Clear button was clicked on, handle it.
     if (event.target == clear_button) {
       // code to handle button push
       return true;
     }
     return fals;
   }
}
mouseDown
Is called when the user clicks on the mouse within the applet window. It is passed the X and Y coordinates of the mouse click.
        public boolean mouseDown(Event event, int x, int y) {
                // the subtractions are because the upper left
                // corner of the gif is put on the catx and caty
                catx = x-50;
                caty = y-50;
                repaint();
                return true;
        }

mstrout@cs.ucsd.edu .... May 21, 2000