AOOP-Chapter 7- Java Applet
AOOP-Chapter 7- Java Applet
1
APPLET
An Applet is a small Internet-based program that
has the Graphical User Interface (GUI), written in the
Java programming language.
Applets are designed to run inside a web browser or in
applet viewer to facilitate the user to animate the
graphics, play sound, and design the GUI components
such as text box, button, and radio button.
An applet represents byte code embedded in a html
page. (applet = bytecode + html) and run with the help
of Java enabled browsers such as Internet Explorer.
Applets are compiled using javac compiler and it can
be executed by using an appletviewer or by embedding
the class file in the HTML (Hyper Text Markup
Languege) file. 2
Unlike Java applications applets do not have a main
() method.
To create applet we can use java.applet.Applet or
javax.swing.JApplet class
All applets inherit the super class ‘Applet’. An
Applet class contains several methods that helps to
control the execution of an applet.
APPLET LIFECYCLE
when an applet is loaded , it undergoes a series of
changes in its state.
Following are the states in applets lifecycle.
}
4) Dead State:
Finally, if the user decides to quit the web browser,
the web browser will free up system resources by
killing the applet before it closes.
To do so, it will call the applets destroy() method
public void destroy()
{
// Code
}
Display State :
Applet moves to the display state whenever it has to
perform the output operations on the screen
6
paint(): This method takes a
java.awt.Graphics object as parameter. This
class includes many methods of drawing
necessary to draw on the applet window.
• This is equivalent to runnable state of
thread.
7
The paint() method is called to accomplish this task.
public void paint(Graphics g)
{
//Display Statements
}
Init() Initialization
Start State
Start()
Stop()
Running Stopped
State state
Start()
Destroy()
Dead state 8
Example:
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("My First Applet",40,40);
}
}
Save the file as SimpleApplet.java
9
Compile the file using javac SimpleApplet.java
How to run an Applet?
There are two ways in which one can run an applet,
as follows
1) Executing the applet within a java-compatible web
browser.
2) Using an applet viewer, such as the standard JDK
tool, “appletviewer”. An applet viewer executes your
applet in a window.
To execute an applet in a web browser, you need to
write a short HTML text file that contains the
appropriate APPLET tag.
10
For above example it is
<html>
<body>
<applet code="SimpleApplet.class" width=200 height=100>
</applet>
</body>
</html>
Save this code in text file with extension .html say
Myapplet.html.
Compile the file using javac SimpleApplet.java
11
On successful compilation of SimpleApplet.java file,
execute the this file using appletviewer
Myapplet.html or just open this html file directly.
Building an applet code:
Applet code uses the series of two classes, namely
Applet and Graphics from java class library.
Applet class which is contained in the java.applet
package provides life and behavior to the applet
through its methods such as init(), start(), and
paint().
12
When an applet is loaded, java automatically calls a
series of applet class methods for starting, running
and stopping the applet code.
The applet class therefore maintains the lifecycle of
an applet.
The paint() method of the applet class, when it is
called, actually display the result of applet code on
the screen.
The output may be text, graphics or sound
14
import java.awt.*;
import java.applet.*;
/*
<applet code="Sample.class" width=300 height=50>
</applet>
*/
public class Sample extends Applet{
String msg;
// set the foreground and background colors.
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
msg = "Inside init( ) --";
} 15
// Initialize the string to be displayed.
public void start() {
msg += " Inside start( ) --";
}
// Display msg in applet window.
public void paint(Graphics g) {
msg += " Inside paint( ).";
g.drawString(msg, 10, 30);
}
}
16
A program that accepts two numbers as inputs ,
calculate sum and display it
import java.awt.*;
import java.applet.*;
public class userin extends Applet
{
TextField t1,t2;
public void init()
{
t1=new TextField(8);
t2=new TextField(8);
add(t1);
add(t2);
17
t1.setText("0");
t2.setText("0");
}
public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;
g.drawString("Input a number in each box" ,10,50);
try{
s1=t1.getText();
x=Integer.parseInt(s1);
s2=t2.getText();
y=Integer.parseInt(s2); 18
}catch(Exception e){}
z=x+y;
s=String.valueOf(z);
g.drawString("The sum is:",10,75);
g.drawString(s,100,75);
}
public boolean action(Event e,Object o)
{
repaint();
return true;
}
}
19
The paint() methods where all the action take
places.
getText() method- retrieves values in the form of
string from text boxes
parseInt() method- converts sting form data into
numeric .
valueOf() method –converts numerical values into
string before display using String class.
20