Start of Tutorial > Start of Trail |
Search
Feedback Form |
Now that you've seen a Java applet, you're probably wondering how it works. Remember that a Java applet is a program that adheres to a set of conventions that allows it to run within a Java-compatible browser.Here again is the code for the "Hello World" applet.
import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } }
The code above starts off with twoimport
statements. By importing classes or packages, a class can more easily refer to classes in other packages. In the Java language, packages are used to group classes, similar to the way libraries are used to group C functions. Importing Classes and Packages gives you more information about packages and theimport
statement.
Every applet must define a subclass of theApplet
class. In the "Hello World" applet, this subclass is called HelloWorld. Applets inherit a great deal of functionality from theApplet
class, ranging from communication with the browser to the ability to present a graphical user interface (GUI). Defining anApplet
Subclass tells you how.
The HelloWorld applet implements just one method, thepaint
method. Every applet must implement at least one of the following methods:init
,start
, orpaint
. Unlike Java applications, applets do not need to implement amain
method. ImplementingApplet
Methods talks about thepaint
method, how the "Hello World" applet implements it, and the other methods applets commonly implement.
Applets are meant to be included in HTML pages. Using the<APPLET>
tag, you specify (at a minimum) the location of theApplet
subclass and the dimensions of the applet's onscreen display area. When a Java-capable browser encounters an<APPLET>
tag, it reserves onscreen space for the applet, loads theApplet
subclass onto the computer the browser is executing on, and creates an instance of theApplet
subclass. Running an Applet gives more details.
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.