the applet class
Download
Skip this Video
Download Presentation
The Applet Class

Loading in 2 Seconds...

play fullscreen
1 / 50

The Applet Class - PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on

The Applet Class. Applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as a part of a Web document. Applet is just a class like any other class. We write an applet by extending the class Applet.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'The Applet Class' - miriam


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
the applet class
TheApplet Class

Appletsare small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as a part of a Web document.

Applet is just a class like any other class.

We write an applet by extending the class Applet.

slide2
When we write an applet, we are only writing part of a program
  • The browser supplies the main method
types of applets
Types of Applets:
  • Based on the Applet class (AWT based Applets)

- These applets use the Abstract Window

Toolkit (AWT) to provide the graphic user interface

  • Based on the Swing class (Swing based Applets)

- Swing applets use the Swing classes to provide the GUI.

slide4
Eg:

//SimpleApplet.java

import java.awt.*;

import java.applet.*;

public class SimpleApplet extends Applet {

public void paint(Graphics g) {

g.drawString("A Simple Applet", 20, 20);

}

}

slide5
The first imports the Abstract Window Toolkit (AWT) classes. AWT contains support for a window-based, graphical user interface.
  • The second import statement imports the applet package, which contains the class Applet.
  • Inside SimpleApplet, paint( ) is declared. This method is defined by the AWT and must be overridden by the applet.
  • The paint( ) method has one parameter of type Graphics.
slide6
Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
  • This method outputs a string beginning at the specified X,Y location.

General form:

void drawString(String message, int x, int y)

- message is the string to be output

beginning at x,y.

slide7
The applet does not have a main( ) method. So Applets do not begin execution at main( ).
  • An applet begins execution when the name of its class is passed to an applet viewer or to a network browser.
slide8
Two ways of running an applet:
  • Executing the applet within a Java-compatible web browser.
  • Using an applet viewer, such as the standard tool, appletviewer.
slide9
HTML file that executesSimpleApplet:

- width and height statements specify the

dimensions of the display area used by the

applet.

C:\>appletviewer RunApp.html

slide11
The Applet Class

The Methods Defined by Applet

slide14
Applet Skeleton
  • Most of the applets override a set of methods that controls its execution.
  • Four of these methods—init( ), start( ),

stop( ), and destroy( )—are defined by Applet.

  • paint( ), is defined by the AWT Component

class.

slide15
// An Applet skeleton.

import java.awt.*;

import java.applet.*;

/*

*/

public class AppletSkel extends Applet {

public void init() {

}

slide16
public void start() {

}

public void stop() {

}

public void destroy() {

}

public void paint(Graphics g) {

}

}

slide18
Applet Initialization and Termination
  • When an applet begins, sequence of

methods are:

1. init( )

2. start( )

3. paint( )

  • When an applet is terminated, the sequence

is:

1. stop( )

2. destroy( )

slide19
init( ) :
  • The init( ) method is the first method to be

called.

  • Initializing the variables.
  • This method is called only once during the

run time of the applet.

slide20
start( ) :
  • Called after init( ).
  • Also called to restart an applet after it has

been stopped.

  • Called each time an applet’s HTML document is displayed onscreen
slide21
paint( ) :
  • Called each time an applet’s output must be

redrawn.

  • Also called when the applet begins execution.
  • The method has one parameter of type Graphics.
  • This parameter will contain the graphics context, which describes the graphics environment in which the applet is running.
slide22
stop( ) :
  • Called when a web browser leaves the HTML document containing the applet.
  • Used to suspend threads that don’t need to run when the applet is not visible.
  • We can restart them when start( ) is called.
slide23
destroy( ) :
  • is called when the environment determines that the applet needs to be removed completely from memory.
  • The stop( ) method is always called before destroy( ).
slide24
Overriding update( ) :
  • This method is called when the applet has requested that a portion of its window be redrawn.
  • The default version of update( ) simply calls paint( ).
  • We can override the update( ) method.
slide25
Simple Applet Display Methods
  • AWT-based applets use the AWT to perform input and output.
  • To output a string to an applet, use drawString( ), which is a member of the Graphics class.

General form:

void drawString(String message, int x, int y)

slide26
message is the string to be output beginning at x,y.
  • drawString( ) method will not recognize newline characters.
  • To set the background color of an applet’s window - setBackground( ).
  • To set the foreground color - setForeground( ).
slide27
Methods are defined as:

void setBackground(Color newColor)

void setForeground(Color newColor)

slide28
The class Color defines the constants

Eg:

setBackground(Color.green);

setForeground(Color.red);

slide29
/* A simple applet that sets the foreground and background colors and outputs a string. */

import java.awt.*;

import java.applet.*;

/*

*/

public class Sample extends Applet{

String msg;

public void init() {

setBackground(Color.cyan);

setForeground(Color.red);

slide30
msg = "Inside init() method +";

}

// Initialize the string to be displayed.

public void start() {

msg += " Inside start() method +";

}

// Display msg in applet window.

public void paint(Graphics g) {

msg += " Inside paint( ) method.";

g.drawString(msg, 10, 30);

}

}

slide32
Requesting Repainting :
  • Whenever an applet needs to update the information displayed in its window, it simply calls repaint( ).
  • repaint( ) method is defined by the AWT.
  • It causes the AWT run-time system to execute a call to your applet’s update( ) method, which, in turn calls paint( ).
slide33
if part of the applet needs to output a string, it can store this string in a String variable and then call repaint( ).
  • repaint( ) method has four forms:

void repaint( ) => causes the entire window to be repainted.

void repaint(int left, int top, int width, int height)

=> specifies a region to be repainted:

slide34
void repaint(long maxDelay)

void repaint(long maxDelay, int x, int y, int width, int height) => requests for repainting where update time is necessary.

- maxDelay specifies the maximum number of milliseconds that can elapse before update( ) is called.

slide35
A Simple Banner Applet-To demonstrate repaint( ) : This applet scrolls a message, from right to left.

/* A simple banner applet. This applet creates a thread that scrolls the message contained in msg right to left across the applet's window.

*/

import java.awt.*;

import java.applet.*;

/*

*/

slide36
public class SimpleBanner extends Applet

implements Runnable {

String msg = " A Simple Moving Banner.";

Thread t = null;

int state;

boolean stopFlag;

// Set colors and initialize thread.

public void init() {

setBackground(Color.cyan);

setForeground(Color.red);

}

slide37
// Start thread

public void start() {

t = new Thread(this);

stopFlag = false;

t.start();

}

// Entry point for the thread that runs the banner.

public void run() {

char ch;

// Display banner

for( ; ; ) {

try {

repaint();

Thread.sleep(250);

slide38
ch = msg.charAt(0);

msg = msg.substring(1, msg.length());

msg += ch;

if(stopFlag)

break;

} catch(InterruptedException e) {}

}

}

// Pause the banner.

public void stop() {

stopFlag = true;

t = null;

}

slide39
// Display the banner.

public void paint(Graphics g) {

g.drawString(msg, 50, 30);

}

}

slide41
Using the Status Window :
  • An applet can also output a message to the status window of the browser or applet viewer on which it is running.
  • showStatus( ) is the method to be called.
slide42
// Using the Status Window.

import java.awt.*;

import java.applet.*;

/*

*/

public class StatusWindow extends Applet{

public void init() {

setBackground(Color.cyan);

}

// Display msg in applet window.

public void paint(Graphics g) {

g.drawString("This is in the applet window.", 10, 20);

showStatus("This is shown in the status window.");

}

}

slide44
Passing Parameters to Applets :
  • To retrieve a parameter, use the getParameter( ) method.
  • It returns the value of the specified parameter in the form of a String object.
slide45
// Use Parameters

import java.awt.*;

import java.applet.*;

/*

*/

slide46
public class ParamDemo extends Applet{

String fontName;

int fontSize;

float leading;

boolean active;

// Initialize the string to be displayed.

public void start() {

String param;

slide47
if(fontName == null)

fontName = "Not Found";

param = getParameter("fontSize");

try {

if(param != null) // if not found

fontSize = Integer.parseInt(param);

else

fontSize = 0;

} catch(NumberFormatException e) {

fontSize = -1;

}

slide48
param = getParameter("leading");

try {

if(param != null) // if not found

leading =

Float.valueOf(param).floatValue();

else

leading = 0;

} catch(NumberFormatException e) {

leading = -1;

}

param = getParameter("accountEnabled");

if(param != null)

active = Boolean.valueOf(param).booleanValue();

}

slide49
// Display parameters.

public void paint(Graphics g) {

g.drawString("Font name: " + fontName, 0, 10);

g.drawString("Font size: " + fontSize, 0, 26);

g.drawString("Leading: " + leading, 0, 42);

g.drawString("Account Active: " + active, 0, 58);

}

}

ad