Applets tutorials

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

After an applet arrives on the client, it
has limited access to resources so that it can produce a graphical user
interface and run complex computations without introducing the risk of viruses
or breaching data integrity.

Below is the simple applet demo:

Example: Java Simple Applet
Demo for drawing a string

//Program name AppletDemo.java

//Java Simple Applet Demo for drawing a
string

import java.awt.*;

import java.applet.*;

public class AppletDemo extends Applet

{

public void paint(Graphics g)

{

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

}

}

Output

To execute an applet in a web browser, you need to write a short HTML text
file that contains a tag that loads the applet. Currently, Sun recommends using
the APPLET tag for this purpose. Here is the HTML file that executes Applet
Demo:

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

</applet>

The width and height statements specify the dimensions of the display area
used by the applet.

After you create this file, you can execute your browser and then load
this file, which causes AppletDemo to be executed.

To execute AppletDemo with an applet viewer, you may also execute the HTML
file. For example, if the preceding HTML file is called RunDemo.html, then the
following command line will run AppletDemo:

C:\>appletviewer RunDemo.html

However, a more convenient method exists that you can use to speed up
testing. Simply include a comment at the head of your Java source code file
that contains the APPLET tag.

By doing this, your code is documented with a prototype of the necessary
HTML statements, and you can test your compiled applet merely by starting the
applet viewer with your Java source code file. If you use this method, the
AppletDemo source file looks like this:

import java.awt.*;

import java.applet.*;

/*

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

</applet>

*/

public class AppletDemo extends Applet {

public void paint(Graphics g) {

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

}

}

Structure of Applets

All but the most trivial applets override a set of methods that provides
the basic mechanism by which the browser or applet viewer interfaces to the
applet and controls its execution.

Four of these methods, init( ), start( ), stop( ), and destroy( ), apply to
allapplets and are defined by Applet Class.

AWT-based applets will also override the paint( ) method, which is defined
by the AWT Component class.

Below is the simple structure of Applets:

// An Applet Structure.

import java.awt.*;

import java.applet.*;

/*

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

</applet>

*/

public class AppletName extends Applet {

// Called first.

public void init() {

// initialization

}

/* Called second, after init(). Also called whenever

the applet is restarted. */

public void start() {

// start or resume execution

}

// Called when the applet is stopped.

public void stop() {

// suspends execution

}

/* Called when applet is terminated. This is the last

method executed. */

public void destroy() {

// perform shutdown activities

}

// Called when an applet's window must be restored.

public void paint(Graphics g) {

// redisplay contents of window

}

}

Example: Java Applet for
drawing a line

//Program name LineDemo.java

//Java Applet for drawing a line

import java.applet.*;

import java.awt.*;

public class LineDemo extends Applet

{

/*

<applet code="LineDemo.class"
width=300 height=250></applet>

*/

public void paint(Graphics gp)

{

gp.drawLine(0, 0, getSize().width,
getSize().height);

}

}

Output

Example: Java programs for
drawing several lines on an applets

//Program name DrawingLines.java

//Java programs for drawing several lines on an
applets

import java.applet.*;

import java.awt.*;

public class DrawingLines extends Applet {

/*

<applet code="DrawingLines"
width=350 height=200>

</applet>

*/

int width, height;

public void init() {

width = getSize().width;

height = getSize().height;

setBackground( Color.black);

}

public void paint( Graphics g ) {

g.setColor( Color.green);

for ( int c = 0; c < 5; ++c ) {

g.drawLine( width, height, c * width /
10, 0 );

}

}

}

Output

Example: Java Applet
program for demonstrating the life cycle of Applet

//Program name LifeCycle.java

//Java Applet program for demonstrating
the life cycle of Applet

import java.awt.*;

public class LifeCycle extends
java.applet.Applet

{

/*

<applet code="LifeCycle.class"
width=260 height=250></applet>

*/

String message[] = new String[20];

int i = 0;

public void init()

{

message[i++] = "I am in
init()";

message[i++] = "I am leaving
init()";

}

public void start()

{

message[i++] = "I am in
start()";

message[i++] = "I am leaving
start()";

}

public void stop()

{

message[i++] = "I am in
stop()";

message[i++] = "I am leaving
stop()";

}

public void destroy()

{

message[i++] = "I am in
destroy()";

message[i++] = "I am leaving
destroy()";

}

public void paint(Graphics g)

{

for(int j = 0; j < i; j++)

g.drawString(message[j], 50,
(j+1)*25);

}

}

Output

Example: Java Applet
program for getting the size of Applet

//Program name SizeApplet.java

//Java Applet program for getting the
size of Applet

import java.applet.*;

import java.awt.*;

public class SizeApplet extends Applet

{

/*

<applet code="SizeApplet.class"
width=350 height=200></applet>

*/

public void paint(Graphics gp)

{

Dimension appletSize = this.getSize();

int appletHeight = appletSize.height;

int appletWidth = appletSize.width;

gp.drawString("This applet is " +
appletHeight +

" pixels high by
" + appletWidth +

" pixels wide.",
15, appletHeight/2);

showStatus(appletHeight+" x
"+appletWidth);

}

}

Output

Now change the size of applet by dragging from
edge of applet window and see the size change as below:

Thank you for visiting www.cbtSAM.com. This site is dream of Samir Patel, when most people dream when in sleep, my dream doesn't let me sleep. cbtSAM Web site is provided as a service and learning tool to the public. While the information contained within the site is periodically updated,no guarantee is given that the iormation provided in this Web site is correct, complete, and up-to-date. We are not responsible for... Read More .