The applet class
This presentation is the property of its rightful owner.
Sponsored Links
1 / 50

The Applet Class PowerPoint PPT Presentation


  • 55 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

The Applet Class

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.


The applet class

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


The applet class

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

}

}


The applet class

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


The applet class

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


The applet class

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


The applet class

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.


The applet class

HTML file that executesSimpleApplet:

<applet code="SimpleApplet" width=200 height=60>

</applet>

- width and height statements specify the

dimensions of the display area used by the

applet.

C:\>appletviewer RunApp.html


The applet class

The Applet Class

The Methods Defined by Applet


The applet class

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.


The applet class

// An Applet skeleton.

import java.awt.*;

import java.applet.*;

/*

<applet code="AppletSkel" width=300 height=100>

</applet>

*/

public class AppletSkel extends Applet {

public void init() {

}


The applet class

public void start() {

}

public void stop() {

}

public void destroy() {

}

public void paint(Graphics g) {

}

}


The applet class

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


The applet class

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.


The applet class

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


The applet class

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.


The applet class

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.


The applet class

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


The applet class

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.


The applet class

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)


The applet class

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


The applet class

  • Methods are defined as:

    void setBackground(Color newColor)

    void setForeground(Color newColor)


The applet class

The class Color defines the constants

Eg:

setBackground(Color.green);

setForeground(Color.red);


The applet class

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

import java.awt.*;

import java.applet.*;

/*

<applet code="Sample" width=300 height=50>

</applet>

*/

public class Sample extends Applet{

String msg;

public void init() {

setBackground(Color.cyan);

setForeground(Color.red);


The applet class

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

}

}


The applet class

This applet generates the window :


The applet class

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


The applet class

  • 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:


The applet class

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.


The applet class

  • 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.*;

    /*

    <applet code="SimpleBanner" width=300 height=50>

    </applet>

    */


The applet class

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

}


The applet class

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


The applet class

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;

}


The applet class

// Display the banner.

public void paint(Graphics g) {

g.drawString(msg, 50, 30);

}

}


The applet class

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.


The applet class

// Using the Status Window.

import java.awt.*;

import java.applet.*;

/*

<applet code="StatusWindow" width=300 height=50>

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

}

}


The applet class

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.


The applet class

// Use Parameters

import java.awt.*;

import java.applet.*;

/*

<applet code="ParamDemo" width=300 height=80>

<param name=fontName value=Courier>

<param name=fontSize value=14>

<param name=leading value=2>

<param name=accountEnabled value=true>

</applet>

*/


The applet class

public class ParamDemo extends Applet{

String fontName;

int fontSize;

float leading;

boolean active;

// Initialize the string to be displayed.

public void start() {

String param;


The applet class

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;

}


The applet class

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

}


The applet class

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

}

}


  • Login