Java Applet Basics
Applet is a
Java program that can be embedded into a web page. It runs inside the web
browser and works at client side. Applet is embedded in a HTML page using the
APPLET or OBJECT tag and hosted on a web server.
Applets are
used to make the web site more dynamic and entertaining.
Some important points :
1.
All applets are sub-classes (either directly or indirectly)
of java.applet.Applet class.
2.
Applets are not stand-alone programs. Instead, they run within
either a web browser or an applet viewer. JDK provides a standard applet viewer
tool called applet viewer.
3.
In general, execution of an applet does not begin at main()
method.
4.
Output of an applet window is not performed by System.out.println().
Rather it is handled with various AWT methods, such as drawString().
Types of applets
there are two varieties of applets based
on Applet. The first are those based directly on the Applet class
described in this chapter. These applets use the Abstract Window Toolkit (AWT)
to provide the graphical user interface (or use no GUI at all).
Second one is Swing class JApplet,
which inherits Applet. Swing applets use the Swing classes to
provide the GUI. Swing offers a richer and often easier-to-use user
interface than does the AWT
Advantage of Applet
There are many advantages of applet. They are as follows:
- It works at client side so
less response time.
- Secured
- It can be executed by
browsers running under many plateforms, including Linux, Windows, Mac Os
etc.
Drawback of Applet
- Plugin is required at client
browser to execute applet.
Life
Cycle of an Applet (Applet Skeleton)
Four methods in the Applet class gives you the framework on which
you build any serious applet −
·
init − This method is intended for whatever initialization is
needed for your applet. It is called after the param tags inside the applet tag
have been processed.
·
start − This method is automatically called after the browser
calls the init method. It is also called whenever the user returns to the page
containing the applet after having gone off to other pages.
·
stop − This method is automatically called when the user moves
off the page on which the applet sits. It can, therefore, be called repeatedly
in the same applet.
·
destroy − This method is only called when the browser shuts down
normally. Because applets are meant to live on an HTML page, you should not
normally leave resources behind after a user leaves the page that contains the
applet.
·
paint − Invoked immediately after the start() method, and also any
time the applet needs to repaint itself in the browser. The paint() method is
actually inherited from the java.awt.
Example of an
Applet Skeleton
import
java
.awt
.*
;
import
java
.applet
.*
;
public
class
AppletTest
extends
Applet
{
public
void
init()
{
//initialization
}
public
void start
()
{
//start or resume execution
}
public
void
stop()
{
//suspend execution
{
public
void
destroy()
{
//perform shutdown activity
}
public
void paint
(Graphics
g
)
{
//display the content of window
}
}
A Simple Applet
import java.awt.*;
import java.applet.*;
public class Simple extends Applet
{
public void paint(Graphics
g)
{
g.drawString("A
simple Applet", 20, 20);
}
}
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.
import java.applet.Applet;
import java.awt.*;
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);
}
}
GetCodeBase and GetDocumentBase
You
will create applets that will need to explicitly load media and text.
Java will allow the applet to load data from the directory holding the html file that
started the applet (the document base) and the directory from which the applet’s class file was
loaded (the code base). These directories are returned by getDocumentBase(
) and getCodeBase(
).
Example code:
AppletContext
and showDocument()
AppletContext is an interface which helps us
to get the required information from the environment in which the applet is
running and getting executed. ü This
information is derived by getAppletContext( ) method which is defined by
Applet. Once we get the information with the above mentioned method, we can
easily bring another document into view by calling
showDocument(
) method. The basic functionality of this method is that it returns no value
and never throw any exception even if it fails hence needed to be implemented
with utmost care and caution. There are two showDocument( )
methods.
1. The method showDocument(URL) displays the document at the specified URL.
The method 2 showDocument(URL, where) displays
the specified document at the specified location within the browser window.
An applet code to demonstrate the use of
AppletContext and showDocument ().
import java.applet.Applet;
import java.applet.AppletContext;
import java.net.*;
/*
<applet code="LoadHTMLFileSample"
width="700" height="500"></applet>
*/
public class LoadHTMLFileSample extends Applet
{
public void start()
{
AppletContext context= getAppletContext();
//get
AppletContext
URL
codeBase = getCodeBase(); //get Applet code base
try{
URL url = new URL(codeBase + "Test.html");
context.showDocument(url,"_blank");
repaint();
}catch(MalformedURLException mfe) {
mfe.printStackTrace();
}
}
}
The AudioClip Interface;
The AudioClip interface is a simple
abstraction for playing a sound clip. Multiple AudioClip items can be playing
at the same time, and the resulting sound is mixed together to produce a
composite. It contains three methods: ü Play(): Starts playing
this audio clip. ü Stop(): Stops playing
this audio clip. ü Loop():Starts playing
this audio clip in a loop. After you have loaded an audio clip using
getAudioClip(), you can use these methods to play it.
example demonstrates how to play a sound using an applet image
using getAudioClip(), play() & stop() methods of AudioClip() class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class PlaySoundApplet extends Applet implements ActionListener {
Button play,stop;
AudioClip audioClip;
public void init() {
play = new Button(" Play in Loop ");
add(play);
play.addActionListener(this);
stop = new Button(" Stop ");
add(stop);
stop.addActionListener(this);
audioClip = getAudioClip(getCodeBase(), "Sound.wav");
}
public void actionPerformed(ActionEvent ae) {
Button source = (Button)ae.getSource();
if (source.getLabel() == " Play in Loop ") {
audioClip.play();
} else if(source.getLabel() == " Stop
"){
audioClip.stop();
}
} }
No comments:
Post a Comment