Java Reference
In-Depth Information
usually the point of making a Java applet is to provide a link to it on a Web page and
allow it to be retrieved and executed by Web users anywhere in the world.
Java bytecode (not Java source code) is linked to an HTML document and sent
across the Web. A version of the Java interpreter embedded in a Web browser is
used to execute the applet once it reaches its destination. A Java applet must be
compiled into bytecode format before it can be used with the Web.
There are some important differences between the structure of a Java applet
and the structure of a Java application. Because the Web browser that executes
an applet is already running, applets can be thought of as a part of a larger pro-
gram. As such they do not have a main method where execution starts. The paint
method in an applet is automatically invoked by the applet. Consider the program
in Listing 2.10, in which the paint method is used to draw a few shapes and write
a quotation by Albert Einstein to the screen.
The two import statements at the beginning of the program explicitly indicate
the packages that are used in the program. In this example, we need the JApplet
LISTING 2.10
//********************************************************************
// Einstein.java Author: Lewis/Loftus
//
// Demonstrates a basic applet.
//********************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Einstein extends JApplet
{
//-----------------------------------------------------------------
// Draws a quotation by Albert Einstein among some shapes.
//-----------------------------------------------------------------
public void paint (Graphics page)
{
page.drawRect (50, 50, 40, 40); // square
page.drawRect (60, 80, 225, 30); // rectangle
page.drawOval (75, 65, 20, 20); // circle
page.drawLine (35, 60, 100, 120); // line
page.drawString ("Out of clutter, find simplicity.", 110, 70);
page.drawString ("-- Albert Einstein", 130, 100);
}
}
 
Search WWH ::




Custom Search