Professional Documents
Culture Documents
Applets: Documents (I.e., Web Pages)
Applets: Documents (I.e., Web Pages)
Applets: Documents (I.e., Web Pages)
APPLETS
Applets are Java programs that can be embedded in Hypertext Markup Language (HTML)
documents (i.e., Web pages).
When a browser loads a Web page containing an applet, the applet downloads into the Web browser and
begins execution.
The browser that executes an applet is generically known as the applet container.
The Java 2 Software Development Kit (J2SDK) includes an applet container (called the
appletviewer) for testing applets before you embed them in a Web page.
Most Web browsers in use today do not support Java 2 directly.
// WelcomeApplet.java
// A first applet in Java.
Class Graphics enables a Java applet to draw graphics such as: lines, rectangles, ovals and strings of
characters and also enables applications to draw.
is an import statement that tells the compiler load class JApplet from package javax.swing.
When you create an applet in Java, you normally import the JApplet class.
every Java applet you create contains at least one class definition
Keyword extends indicates that class WelcomeApplet inherits existing pieces from another class.
The class from which WelcomeApplet inherits (JApplet) appears to the right of extends. In this inheritance
relationship,
Using inheritance here results in a WelcomeApplet class definition that has the attributes (data) and
behaviors (methods) of class JApplet,
as well as the new features we are adding in our WelcomeApplet class definition (specifically, the ability to
draw Welcome to Java Programming! on the applet).
To enable our applet to draw, class WelcomeApplet overrides (replaces or redefines) the default version of
paint by placing statements in the body of paint that draw a message on the screen.
When the applet container tells the applet to “draw itself” on the screen by calling method paint, our
message Welcome to Java Programming! appears rather than a blank screen.
super.paint( g );
calls the version of method paint inherited from superclass JApplet
instructs the computer to perform an action (or task), namely to draw the characters of the string
Welcome to Java Programming! on the applet.
This statement uses method drawString defined by class Graphics (this class defines all the drawing
capabilities of a Java program, including strings of characters and shapes such as rectangles, ovals
and lines).
The statement calls method drawString using the Graphics object g (in paint’s parameter list) followed by
a dot operator (.), and followed by the method name drawString.
The method name is followed by a set of parentheses containing the argument list drawString needs to
perform its task
Before you can execute the applet you must create an HTML (Hypertext Markup Language) file to load the
applet into the applet container (appletviewer or a browser).
To execute a Java applet, an HTML text file must indicate which applet the applet container should load and
execute.
WelcomeApplet.html that loads into the applet defined in Fig. 3.6 into an applet container
<html>
<applet code = "WelcomeApplet.class" width = "300" height = "45">
</applet>
</html
1 <html>
2 <applet code = "WelcomeApplet2.class" width = "300" height = "60">
3 </applet>
4 </html>
// process result
if ( result == 1 )
passes = passes + 1;
else
failures = failures + 1;
student = student + 1;
}
// termination phase
output = "Passed: " + passes +
"\nFailed: " + failures;
if ( passes > 8 )
output = output + "\nRaise Tuition";
// display results
JOptionPane.showMessageDialog( null, "The sum is " + sum,
"Sum Even Integers from 2 to 100",
JOptionPane.INFORMATION_MESSAGE );
case 1:
g.drawLine( 10, 10, 250, 10 + i * 10 );
break; // done processing case
case 2:
g.drawRect( 10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10 );
break; // done processing case
case 3:
g.drawOval( 10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10 );
break; // done processing case
default:
g.drawString( "Invalid value entered", 56 10, 20 + i * 15 );