Intro to Applet Programming:

  1. Consider the complete source code of the "Rectangle Drawing" applet program:

    import java.applet.Applet; <--- import this to write Applet ! import java.awt.Graphics; <--- import Graphics class import java.awt.Graphics2D;<--- import Graphics2D class import java.awt.Rectangle; <--- import Rectangle class import java.awt.Color; <--- import Color class public class RectangleApplet extends Applet <--- "extends" Applet { public void paint(Graphics g) <--- paint() { // recover Graphics2D Graphics2D g2 = (Graphics2D)g; // ******************************************** // Actual drawing instructions... // ******************************************** // construct a rectangle and draw it Rectangle cerealBox = new Rectangle(5, 10, 20, 30); g2.setColor(Color.red); // Pick drawing color g2.draw(cerealBox); // move rectangle 15 units sideways and 25 units down // and draw rectangle cerealBox.translate(15, 25); g2.setColor(Color.blue); g2.fill(cerealBox); } }

    • An applet program must "extend" the Java library class java.applet.Applet

    • The effect is an Applet object is also a Panel object !!!     See doc on Applet class: click here

    • The necessary action to display a drawing (put a Panel object in a Frame) is done by the browser


  2. Preparations done by the browser for Applet program:

    • Creates a unframe (window) to display the Applet (Panel)

    • Browser creates one OBJECT of the "Applet extending" class and add it to the frame
      (See: click here)

    So you can just concentrate on the drawing and interaction parts...

    You need to know how the browser interacts with an applet to program Applets effectively


  3. Interaction between browser and applet program:

    The main() method in the browser will invoke a method in the Applet depending on some events

  4. Operations performed by the browser:

    1. When the browser loads an applet for the first time, it invokes the applet's init() method

    2. When the browser needs to start the applet, it invokes the applet's start() method
      • It calls start() when the applet is loaded for the first time
      • It also calls start() when the browser reload the webpage containing the applet (because the applet will be stopped - see below)

    3. When the browser needs to redraw the applet, it calls paint(Graphics g)
      • You must never invoke paint(g).
      • You can request the browser to redraw by using the repaint() method.

    4. When the browser needs to stop the applet, it invokes the applet's stop() method
      • It calls stop() when the browser leaves the webpage containing the applet

    5. When the browser needs to remove the applet, it invokes the applet's destroy() method
      • It calls destroy() when the webpage with the applet is removed from the browser's "cache"

    THUS: you should define a paint(Graphics g) in your Applet program that "paints" the applet window.


    The Applet class documentation is here: click here

    (Look particular at the description for "init", "start", "stop", and "destroy")

  5. NOTE: you always need a graphical context to draw.

    The browser will call and pass a graphical context to the paint(Graphics g) method of an Applet.

    If you need to do drawing in other methods (e.g., init()), you can create a new graphical context holder by calling: getGraphic()