OOP Through Java Unit-5 Material
OOP Through Java Unit-5 Material
Applets
An applet is a program that comes from server into a client and gets executed at client side
and displays the result 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 An
applet is a Java program that runs in a browser Unlike Java applications applets do not have a
main () method To create applet we can use javaappletApplet or javaxswingJApplet class All
applets inherit the super class “Applet”. An Applet class contains several methods that helps to
control the execution of an applet
Advantages:
➢ Applets provide dynamic nature for a webpage
➢ Applets are used in developing games and animations
Drawback of Applet
➢ Plugin is required at client browser to execute applet.
Hierarchy of Applet:
As displayed in the above diagram, Applet class extends Panel. Panel class extends
Container which is the subclass of Component
Lifecycle of Java Applet
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Applet Lifecycle
Creating an applet:
Let the Applet class extends Applet or JApplet class
Overide the following methods of Applet class
➢ public void init (): This method is used for initializing variables, parameters to create
components This method is executed only once at the time of applet loaded into memory
➢ public void start (): After init() method is executed, the start method is executed automatically
Start method is executed as long as applet gains focus In this method code related to opening
files and connecting to database and retrieving the data and processing the data is written
➢ public void stop (): This mehtod is executed when the applet loses focus Code related to
closing the files and database, stopping threads and performing clean up operations are written
in this stop method
➢ public void destroy (): This method is exexuted only once when the applet is terminated from
the memory
java.awt.Component class
The Component class provides 1 life cycle method of applet.
➢ public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object
that can be used for drawing oval, rectangle, arc etc.
After writing an applet, an applet is compiled in the same way as Java application but running
of an applet is different There are two ways to run an applet
Executing an applet within a Java compatible web browser
Executing an applet using “appletviewer” This executes the applet in a window
To execute an applet using web browser, we must write a small HTML file which contains the
appropriate “APPLET” tag <APPLET> tag is useful to embed an applet into an HTML page It
has the following form:
<APPLET CODE=”name of the applet class file” CODEBASE=”path of the applet class file”
HEIGHT = maximum height of applet in pixels WIDTH = maximum width of
applet in pixels ALIGN = alignment (LEFT, RIGHT, MIDDLE, TOP, BOTTOM)
ALT = alternate text to be displayed>
<PARAM NAME = parameter name VALUE = itsvalue> </APPLET>
The <PARAM> tag useful to define a variable (parameter) and its value inside the HTML
page which can be passed to the applet The applet can access the parameter value using
getParameter () method, as:
String value = getParameter (“pname”);
Where pname is the parameter name and its value is retrieved
The HTML file must be saved with html extension After creating this file, open the Java
compatible browser (Internet Explorer) and then load this file by specifying the complete path,
then Applet program will get executed
In order to execute applet program with an applet viewer, simply include a comment at the
head of Java Source code file that contains the ‘APPLET’ tagThus, our code is documented
with a prototype of the necessary HTML statements and we can test out compiled applet by
starting the
appletviewer with the Java file as:
appletviewer programname.java
or
appletviewer programname.html
Program 1: Write an applet program with a message and display the message in paint ()
method
/* <applet code="MyAppletclass" width = 600 height= 450>
</applet> */
import java.applet.Applet;
import java.awt.*;
public class MyApplet extends Applet
{
String msg="";
public void init()
{
msg += "init";
}
public void start()
{
msg +=" start";
}
public void paint(Graphics g)
{
g.drawString(msg,10,100);
}
public void stop()
{
msg += " stop";
}
public void destroy()
{
msg+= " destroy";
}
}
Output:
Program 2: Write a program to move an image from left to right in an Applet To load an
image use Image class of javaawt
/*<applet code="MyApplet1class" width = 600 height= 450></applet> */
import java.applet.Applet;
import java.awt.*;
public class MyApplet1 extends Applet
{
public void paint (Graphics g)
{
Image i = getImage (getDocumentBase (),"planegif");
for (int x= 0 ; x<=800 ; x++)
{
g.drawImage (i, x, 0, null);
try
{
Thread.sleep (20);
}
catch(InterruptedException ie) { }
}
}
}
Output:
Program 3: Write a program to pass employ name and id number to an applet
/*<applet code="MyApplet2class" width = 600 height= 450>
<param name = "t1" value="Hari Prasad">
<param name = "t2" value ="101">
</applet> */
import java.applet.*;
import java.awt.*;
public class MyApplet2 extends Applet
{
String n;
String id;
public void init()
{
n = getParameter("t1");
id = getParameter("t2");
}
public void paint(Graphics g)
{
g.drawString("Name is : " + n, 100,100);
g.drawString("Id is : "+ id, 100,150);
}
}
Output:
Commonly used methods of Graphics class:
public abstract void drawString(String str, int x, int y): is used to draw the specified string.
public void drawRect(int x, int y, int width, int height): draws a rectangle with the
specified width and height.
public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with
the default color and specified width and height.
public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the
specified width and height.
public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the
default color and specified width and height.
public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the
points(x1, y1) and (x2, y2).
public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer):
is used draw the specified image.
public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle):
is used draw a circular or elliptical arc.
public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle):
is used to fill a circular or elliptical arc.
public abstract void setColor(Color c): is used to set the graphics current color to the
specified color.
public abstract void setFont(Font font): is used to set the graphics current font to the
specified font.
b=new Button("Click");
b.setBackground(Color.cyan);
b.setBounds(80,150,60,50);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
}
public void paint(Graphics g)
{
Font f=new Font("Times New Roman",4,24);
g.setFont(f);
}
public void actionPerformed(ActionEvent e) //when u click on button this is performed
{
tf.setText("Welcome");
}
}
Output: