Applets: Documents (I.e., Web Pages)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1

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.

// Java core packages


import java.awt.Graphics; // import class Graphics

//Java extension packages


import javax.swing.JApplet; // import class JApplet

public class WelcomeApplet extends JApplet {

// draw text on applet’s background


public void paint( Graphics g )
{
// call inherited version of method paint
super.paint( g );

// draw a String at x-coordinate 25 and y-coordinate 25


g.drawString( "Welcome to Java Programming!", 25, 25 );

} // end method paint

} // end class WelcomeApplet

import java.awt.Graphics; // import class Graphics

Class Graphics enables a Java applet to draw graphics such as: lines, rectangles, ovals and strings of
characters and also enables applications to draw.

import javax.swing.JApplet; // import class JApplet

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

public class WelcomeApplet extends JApplet

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,

Java Week 03: Struktur Kontrol


2

JApplet is called the superclass or base class


and
WelcomeApplet is called the subclass or derived class.

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).

public void paint( Graphics g )

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

g.drawString( "Welcome to Java Programming!", 25, 25 );

 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

Java Week 03: Struktur Kontrol


3

// Fig. 3.8: WelcomeApplet2.java


// Displaying multiple strings in an applet.

// Java core packages


import java.awt.Graphics; // import class Graphics

// Java extension packages


import javax.swing.JApplet; // import class JApplet

public class WelcomeApplet2 extends JApplet {

// draw text on applet’s background


public void paint( Graphics g )
{
// call inherited version of method paint
super.paint( g );

// draw two Strings at different locations


g.drawString( "Welcome to", 25, 25 );
g.drawString( "Java Programming!", 25, 40 );

} // end method paint

} // end class WelcomeApplet2

1 <html>
2 <applet code = "WelcomeApplet2.class" width = "300" height = "60">
3 </applet>
4 </html>

Java Week 03: Struktur Kontrol


4

// Fig. 4.11: Analysis.java


// Analysis of examination results.

// Java extension packages


import javax.swing.JOptionPane;

public class Analysis {

// main method begins execution of Java application


public static void main( String args[] )
{
// initializing variables in declarations
int passes = 0, // number of passes
failures = 0, // number of failures
student = 1, // student counter
result; // one exam result
String input, // user-entered value
output; // output string

// process 10 students; counter-controlled loop


while ( student <= 10 ) {

// obtain result from user


input = JOptionPane.showInputDialog(
"Enter result (1=pass,2=fail)" );

// convert result to int


result = Integer.parseInt( input );

// 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";

JOptionPane.showMessageDialog( null, output,


"Analysis of Examination Results",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 ); // terminate application

} // end method main

} // end class Analysis

Java Week 03: Struktur Kontrol


5

// Fig. 5.5: Sum.java


// Counter-controlled repetition with the for structure

// Java extension packages


import javax.swing.JOptionPane;

public class Sum {

// main method begins execution of Java application


public static void main( String args[] )
{
int sum = 0;

// sum even integers from 2 through 100


for ( int number = 2; number <= 100; number += 2 )
sum += number;

// display results
JOptionPane.showMessageDialog( null, "The sum is " + sum,
"Sum Even Integers from 2 to 100",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 ); // terminate the application

} // end method main

} // end class Sum

Java Week 03: Struktur Kontrol


6

// Fig. 5.7: SwitchTest.java


// Drawing lines, rectangles or ovals based on user input.

// Java core packages


import java.awt.Graphics;

// Java extension packages


import javax.swing.*;

public class SwitchTest extends JApplet {


int choice; // user's choice of which shape to draw

// initialize applet by obtaining user's choice


public void init()
{
String input; // user's input

// obtain user’s choice


input = JOptionPane.showInputDialog(
"Enter 1 to draw lines\n" +
"Enter 2 to draw rectangles\n" +
"Enter 3 to draw ovals\n" );

// convert user's input to an int


choice = Integer.parseInt( input );
}

// draw shapes on applet's background


public void paint( Graphics g )
{
// call inherited version of method paint
super.paint( g );

// loop 10 times, counting from 0 through 9


for ( int i = 0; i < 10; i++ ) {

// determine shape to draw based on user's choice


switch ( choice ) {

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 );

} // end switch structure


} // end for structure

} // end paint method

} // end class SwitchTest

Java Week 03: Struktur Kontrol

You might also like