Applet in Java
Applet in Java
·
Applets are small Java applications
that can be accessed on an Internet server, transported over Internet, and can
be automatically installed and run as a part of a web document.
·
After a user receives an applet, the
applet can produce a graphical user interface. It has limited access to
resources so that it can run complex computations without introducing the risk
of viruses or breaching data integrity.
·
Any applet in Java is a class that
extends the java.applet.Applet class.
·
An Applet class does not have any
main() method. It is viewed using JVM. The JVM can use either a plug-in of the
Web browser or a separate runtime environment to run an applet application.
·
JVM creates an instance of the
applet class and invokes init() method to initialize an
Applet.
A Simple Applet
import java.awt.*;
import java.applet.*;
/*
<applet code="myApp.class"
width="300" height="300">
</applet>
*/
public class myApp extends Applet
{
public void
paint(Graphics g)
{
g.drawString("Piyush Patel,
Rajkot", 20, 20);
}
}
Compile: javac Simple.java
Run :
appletviewer Simple.java
Every Applet application must import two packages
-
java.awt
and java.applet
.java.awt.*
imports the Abstract Window Toolkit (AWT)
classes. Applets interact with the user (either directly or indirectly) through
the AWT. The AWT contains support for a window-based, graphical user
interface. java.applet.*
imports
the applet package, which contains the class Applet. Every applet that you
create must be a subclass of Applet class.
The class in the program must be declared as public,
because it will be accessed by code that is outside the program. Every Applet
application must declare a paint() method. This method is defined by AWT class
and must be overridden by the applet. The paint() method is called each time
when an applet needs to redisplay its output. Another important thing to notice
about applet application is that, execution of an applet does not begin at
main() method. In fact an applet application does not have any main() method.
Advantages of Applets
1.
It
takes very less response time as it works on the client side.
2.
It
can be run on any browser which has JVM running in it.
How to run an Applet Program?
An Applet program is compiled in the same way as you
have been compiling your console programs. However there are two ways to run an
applet.
·
Executing
the Applet within Java-compatible web browser.
·
Using
an Applet viewer, such as the standard tool, applet viewer. An applet viewer
executes your applet in a window
For executing an Applet in a web browser, create
short HTML file in the same directory. Inside body tag
of the file, include the following code. (applet tag loads the
Applet class)
< applet code = "MyApplet" width=400
height=400 >
< /applet >
Applet Life
Cycle
1.
init(): The applet's voyage starts here. In this method, the
applet object is created by the browser. Because this method is called before
all the other methods, programmer can utilize this method to instantiate
objects, initialize variables, setting background and foreground colors in GUI
etc.; the place of a constructor in an application. It is equivalent to born state of a
thread.
2.
start(): In init() method, even though applet object is
created, it is in inactive state. An inactive
applet is not eligible for microprocessor time even though the microprocessor
is idle. To make the applet active, the init() method calls start() method. In
start() method, applet becomes active and thereby eligible for processor time.
3.
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 the place where the programmer can write his code of
what he expects from applet like animation etc. This is equivalent to runnable state of
thread.
4.
stop(): In this method the applet becomes temporarily
inactive. An applet can come any number of times into this method in its life
cycle and can go back to the active state (paint() method) whenever would like.
It is the best place to have cleanup code. It is equivalent to the blocked state of
the thread.
5.
destroy(): This method is called just before an applet object is
garbage collected. This is the end of the life cycle of applet. It is the best
place to have cleanup code. It is equivalent to the dead state of
the thread.
Displaying Graphics in Applet
java.awt.Graphics class provides many methods for
graphics programming.
Commonly used methods of Graphics class:
1. public abstract void
drawString(String str, int x, int y): is used to
draw the specified string.
2. public void drawRect(int x, int y,
int width, int height): draws a rectangle
with the specified width and height.
3. 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.
4. public abstract void drawOval(int x,
int y, int width, int height): is used to draw
oval with the specified width and height.
5. 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.
6. 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).
7. public abstract boolean
drawImage(Image img, int x, int y, ImageObserver observer): is
used draw the specified image.
8. public abstract void drawArc(int x,
int y, int width, int height, int startAngle, int arcAngle): is
used draw a circular or elliptical arc.
9. 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.
10.
public abstract void setColor(Color c): is
used to set the graphics current color to the specified color.
11.
public abstract void setFont(Font font): is
used to set the graphics current font to the specified font.
Example of Graphics in applet:
GraphicsDemo.java
import java.applet.Applet;
import java.awt.*;
/*
<applet code="GraphicsDemo.class" width="300" height="300">
</applet>
*/
public class GraphicsDemo extends Applet
{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
Parameter in Applet
We can get any information from the HTML file as a
parameter. For this purpose, Applet class provides a method named
getParameter(). Syntax:
public String getParameter(String parameterName)
Example of using parameter in Applet:
import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet{
public void paint(Graphics g){
String str=getParameter("msg");
g.drawString(str,50, 50);
}
}
myapplet.html
<html>
<body>
<applet code="UseParam.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>
Comments
Post a Comment