Chapter 2 - Applates
Chapter 2 - Applates
Chapter 2 - Applates
An applet is a Java program that runs in a Web browser. It can be a fully functional Java
application because it has the entire Java API at its disposal. Applet is a special type of program
that is embedded in the webpage to generate the dynamic content. It runs inside the browser
and works at client side.
There are some important differences between an applet and a standalone Java application,
including the following: -
− An applet is a Java class that extends the java.applet.Applet class.
− A main () method is not invoked on an applet, and an applet class will not define main().
− Applets are designed to be embedded within an HTML page.
− When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
− A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser
or a separate runtime environment.
− The JVM on the user's machine creates an instance of the applet class and invokes various
methods during the applet's lifetime.
− Applets have strict security rules that are enforced by the Web browser. The security of
an applet is often referred to as sandbox security, comparing the applet to a child playing
in a sandbox with various rules that must be followed.
− Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
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 platforms, including Linux,
Windows, Mac Operating System etc.
Drawback of Applet
• Plugin is required at client browser to execute applet.
The AWT allows us to use various graphical components. When we start writing any applet
program we essentially import two packages namely – java.awt and java.applet.
The java.applet package contains a class Applet which uses various interfaces such as
AppletContext, AppletStub and AudioCIip. The applet class is an extension of Panel class
belonging to java.awt package.
Applet class extends Panel. Panel class extends Container which is the subclass of Component.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Life Cycle of an Applet፡ 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.
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle
methods of applet.
− public void init(): is used to initialized the Applet. It is invoked only once.
Java Programming Chapter 2: Java Applet
− public void start(): is invoked after the init() method or browser is maximized. It is
used to start the Applet.
− public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
− public void destroy(): is used to destroy the Applet. It is invoked only once.
➢ 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.
Note: class must be public because its object is created by Java Plugin software that resides on
the browser.
− public URL getDocumentBase(): is used to return the URL of the document in which
applet is embedded.
− public URL getCodeBase(): is used to return the base URL.
Animation in Applet
− Applet is mostly used in games and animation. For this purpose, image is required to be
moved.
Example of animation in applet:
import java.awt.*;
import java.applet.*;
public class AnimationExample extends Applet {
Image picture;
try{Thread.sleep(100);}catch(Exception e){}
}
}
}
EVENTHANDLING IN APPLET
As we perform event handling in AWT or Swing, we can perform it in applet also. Let's see the simple
example of event handling in applet that prints a message by click on the button.
b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);
add(tf);
b.addActionListener(this);
setLayout(null);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
}
To run the above applet code we can use the following Html cod
<html>
<body>
<applet code="EventApplet.class" width="300" height="300">
</applet>
</body>
</html>
tf=new JTextField();
tf.setBounds(30,40,150,20);
b=new JButton("Click");
b.setBounds(80,150,70,40);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
}
Painting in Applet
We can perform painting operation in applet by the mouseDragged() method of
MouseMotionListener.
In the above example, getX() and getY() method of MouseEvent is used to get the current x-axis
and y-axis. The getGraphics() method of Component class returns the object of Graphics.
myapplet.html
<html>
<body>
<applet code="MouseDrag.class" width="300" height="300">
</applet>
</body>
</html>
myapplet.html
<html>
<body>
<applet code="UseParam.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>
APPLET COMMUNICATION
java.applet.AppletContext class provides the facility of communication between applets. We provide the
name of applet through the HTML file. It provides getApplet() method that returns the object of Applet.
Syntax:
public Applet getApplet(String name){}
add(b);
b.addActionListener(this);
}
AppletContext ctx=getAppletContext();
Applet a=ctx.getApplet("app2");
a.setBackground(Color.yellow);
}
}
myapplet.html
<html>
<body>
<applet code="ContextApplet.class" width="150" height="150" name="app1">
</applet>
<applet code="First.class" width="150" height="150" name="app2">
</applet>
</body>
</html>