Applet Class


  • Each Applet inherits from java.applet.Applet

  • This class provides the necessary structure to coordinate with a Web browser

  • Key methods to understand and potentially implement/override:
    • A no-argument (default) constructor
    • init()
    • start()
    • stop()
    • destroy()

  • If your Applet has any graphics, look at:
    • repaint()
    • update()
    • paint()


    Detailed Description:

           "Once someone has written a Java tag within their web page and loads the page, the browser executes the .class file that represents a Java applet. All Java applets must derive from or be a subclass of a class called Applet. This class is located in a package called java.applet. So the formal long name is java.applet.Applet. The first applet has a lower case 'a' and represents part of the package name, and the second Applet has an uppercase 'A' that is the class name.

           This class provides the appropriate structure that all Java applets must follow. And specifically, it has unique methods in it that specify an interface that the web browser assumes to be there. The key methods that one must implement or override are a zero-argument constructor, or no-argument constructor, which could be the default constructor, an init() method, a start() method, a stop() method, and a destroy() method. These methods are executed in a specific sequence.

           Let's say that the constructor creates whatever the applet needs to have as part of its initialization. The init() method typically is executed to set up the graphical user interface. The start() method is executed when the web age is shown. The stop() method is executed when the web page is no longer shown. And the destroy() method is executed when the applet is no longer needed and can be discarded.

           In addition, if your applet does graphics (draw lines and circles), you also need to understand the relationship between repaint(), update(), and paint(). Briefly, repaint() is executed by anything in your code if you wish to have the screen redrawn. Independent of that, repaint() invokes a system thread called the screen updater. The screen updater's job is to later call your update() method. The update() method's typical function is to clear the background and call paint(). The paint() function then draws the screen and does whatever graphics operations you wish to do for your applet. So realize that asynchronous threads are operating here. Again, the sequence for painting is repaint() starts up a secondary thread, the secondary thread calls update(), and update calls paint()."