Java Reference Index


  1. Introduction
  2. Data Types
  3. Arrays
  4. Operators
  5. Program Control
  6. Classes
  7. Objects
  8. Interfaces
  9. Containers
  10. Class Structure
  11. Error Handling

    Program Types

  12. Applications
  13. Applets

    How To

  14. Type Conversions

    Packages

  15. java.lang.reflect
  16. java.applet
  17. java.awt
  18. java.beans
  19. java.io
  20. java.lang
  21. java.math
  22. java.net
  23. java.rm
  24. java.security
  25. java.sa
  26. java.text
  27. java.util
  28. java.servlet

    Classes

  29. Object Class

    Appendices

  30. Terms
  31. Credits

Applets

The main applet class that begins the running of the applet must be declared public.

public class MyApplet extends java.applet.Applet {
    // Code here
}

"Instead of a main() method, applets have methods that are called when specific things occur as the applet runs." The Applet class provides the below methods, but they may be overridden in your applet. The following methods provide the main functionality for applets to be created and destroyed.

  • init() - Initialization is done when the applet is loaded.
  • start() - This starts the applet running and may be done several times. This method must be overridden to start some work or required threads.
  • stop() - Stopping the applet execution. It happens when the user exits the page. This should be overridden if the start method was overridden.
  • destroy() - Destruction allows the applet to complete any required cleanup before being removed from memory.

Applets do not have constructors since the init method is used to establish the applet.

    Drawing methods
  • paint(Graphics g) - This is "called whenever the applet's window needs to be displayed or redisplayed". The browser passes the graphics object to the paint method.
  • Update

Putting an Applet on a Web Page

<HTML>
<HEAD>
<TITLE>An Applet Example</TITLE>
</HEAD>
<BODY>
<P>The Applet example is: <BR>
<APPLET CODE=“MyApplet.class” WIDTH=200 HEIGHT=50>
<PARAM NAME=font VALUE=“TimesRoman”>
<PARAM NAME=size VALUE=“16”>
You see this if your browser does not include Java support.
</APPLET>
</P>
</BODY>
</HTML>

If the OBJECT tag is used rather than the APPLET tag, use CLASSID in place of CODE to specify the file name. A JAR (JAVA Archive) file is a package of the applet (or application) along with all required supporting class files. The "jar" command is used to create these JAR files from class and gif files.

Applet Attributes

  • CODE - The name of the applet main class file.
  • CODEBASE - Specifies a different folder for the browser to look for the applet file.
  • WIDTH
  • HEIGHT
  • ALIGN - ALIGN=TOP
    • LEFT
    • RIGHT
    • TOP - Align with the topmost item in the line.
    • BOTTOM
    • MIDDLE
    • ABSMIDDLE
    • ABSBOTTOM
    • BASELINE
    • TEXTTOP
  • PARAM - Used to pass parameters to applets. Parameters expected but not passed are passed as a NULL. Your program needs to test for NULL parameters and set a default for any expected parameters. The "getParameter" function is used to get the passed parameters in the applet.
  • HSPACE - Space in pixels between the applet and the text around it.
  • VSPACE

Other Applet Information

Applets inherit event related functionality from the Component class. The Component class is one of the superclasses of the Applet class.
Component:

  • addMouseListener
  • addKeyListener

Must implement MouseListener interface with the "implements" statement and "addMouseListener(this);" in the init() method.

UI - User interface component is used to interface to the user. Methods to manage UI components:

  • add() - Add a component
  • remove()
  • setLayout() - Set the layout manager manager. Controls the size and position of applet components.