Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Java Applet

Introduction
Java supports two kinds of programs. They are the normal standalone programs,
which are called Java applications and the programs that are to be embedded in
a Web page, called Java applets. It can display text, graphics, animation, etc,
which make Web pages attractive and interesting to view.

Applet Programming
Java is referred to as a powerful Internet programming language. Applets are the
Java programs that are executed by the Web browser. Java also provides a utility
called appletviewer to execute applets. Applet is a special class file written to
display graphics in a Web browser. Applets are embedded in Web pages using
the HTML tag <applet>.

When a Web page containing applet program is run, it is downloaded to the


memory automatically and executed by the Web browser. Applet gets displayed
in the specific space allocated in the Web page called Applet Window.

Using applets the following can be embedded in Web pages:


 Animated graphics
 Video games
 Advanced text displays and images
 Audio

Local and Remote Applets


There are two types of applets that can be embedded in a Web page. They are
local applet and remote applet.

If the applet that is embedded is stored in your own machine, it is called as local
applet.

The applets that are stored in some other computer can also be embedded in
Web pages. Such applets can be downloaded and embedded into Web pages
from the Internet. This is referred to as remote applet

Differences Between Applets and Applications


As you are already aware of, applets are Java programs embedded in Web
pages and applications are normal standalone Java programs.

1 Footnote by
Takada
Java Applet

Life Cycle of an Applet


The following four methods are executed during the life cycle of an applet
1. init()
2. start()
3. stop()
4. destroy( )

The paint() and repaint() Methods


The paint() method of the Applet class is executed automatically whenever the
applet is displayed in the Web page.

This method is used to draw graphics in the drawing area of the applet. The
repaint() method is used whenever you want to redraw the applet’s drawing area.

The repaint() method calls the update() method. The update() method clears the
applet area and calls the paint() method.

public void paint(Graphics g) {


<display statements>;
}

You don’t have to define the repaint() method. To call the repaint() method, just
type repaint() followed by a colon.

2 Footnote by
Takada
Java Applet

Viewing an Applet using Web Browser

The following is the code given in HTML file:

<HTML>
<body>
Save as
<applet code=HelloJava.class width=150 height=150>
HelloJava.html
</applet>
</body>
</HTML>

Step to compile and run the applet

javac HelloJava.java  compile java program

appletviewer HelloJava.html  run / execute the applet

3 Footnote by
Takada
Java Applet

/* Program to display a string using an applet in appletviewer */

import java.awt.*;
import java.applet.*;

public class AppletView extends Applet{


public void paint(Graphics g){
g.drawString("Selamat Datang ke Java Applet",10,50);
}
}

<HTML>
<body>
<applet code="AppletView.class" width= 200 height=100>
</body>
</HTML>

/* Program to calculate and print the sum of two given numbers */

import java.awt.*;
import java.applet.*;

public class Add extends Applet { public void paint(Graphics g) {


int value1= 10;
int value2= 20;

int sum = value1 + value2;


String s = "Sum:" + string.valueOf(sum);

g.drawString(s,100,100);
}
}

<HTML>
<body>
<applet code="Add.class" width= 200 height=100>
</body>
</HTML>

4 Footnote by
Takada

You might also like