Applet
Applet
Applet
APPLETS
Introduction: 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.It Secured and can be
executed by browsers running under many platforms, including Linux, Windows, Mac Os
etc.Plug-in is required at client browser to execute applet
Java Plug-in is required in the browser at client side to execute applet.Java Plug-in is a
software product that serves as a bridge between a browser and an external Java Runtime
Environment(JRE).
import java.applet.Applet;
import java.awt.Graphics;
3. Create a html file,example “HelloWorldApplet.html” and place the applet code in html
file.
<html>
<body>
<applet code="HelloWorldApplet.class" width="300" height="300">
</applet>
</body>
</html>
4. There are two ways to run an applet.
➢ Executing the Applet within Java-compatible web browser.For executing an
Applet in an web browser,create short HTML file in the same directory and Run
the HTML file.
➢ Using an Applet viewer,such as the standard tool,applet viewer.An applet viewer
executes your applet in a window.
5. Execute the applet using applet viewer tool at command prompt as shown below-
appletviewer HelloWorldApplet.html
Applet Viewer:HelloWorldApplet.class - X
Applet
Hello World
Applet started
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.
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Whenever an applet is created,it undergoes a series of changes from initialization to
destruction.
Various stages of an applet life cycle are depicted in the figure above.
Applet Class:Applet class provides all necessary support for applet execution,such as
initializing and destroying of applet.It also provide methods that load and display images
and methods that load and play audio clips.
Most applets override these four methods.These four methods forms Applet
lifecycle.
public void init(): init() is the first method to be called. This is where variable are initialized.
This method is called only once during the runtime of applet.
public void start(): start( ) method is called after init( ).This method is called to restart an applet
after it has been stopped.
public void stop(): stop( ) method is called to suspend thread that does not need to run when
applet is not visible.
public void destroy(): destroy( ) method is called when your applet needs to be removed
completely from memory.
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
public void init()
{
setName("MyApplet");
}
public void paint(Graphics g)
{
g.drawRoundRect(10,30,120,120,2,3);
}
}
2. Compile the above MyApplet.java applet.
Javac MyApplet.java
3. Create the html file,example “MyApplet.html” and place the applet code in html file.
<html>
<body>
<applet code="MyApplet.class" width="300" height="300">
</applet>
</body>
</html>
4. Execute the applet using applet viewer tool at command prompt as shown below:
Appletviewer MyApplet.html
Applet Viewer.MyApplet.class - X
Applet
Applet started.
Common Methods Used in Displaying the output:
Applets are displayed in a window and they use the AWT to perform input and output functions.
Below are the common methods used for displaying the output-
➢ To output a string to an applet, use drawstring( ),which is a member of the Graphics
class. Typically, it is called from within either update( ) or paint( ).It has the following
general form:
void drawString(String message,int x,int y)
Here, message is the string to be output beginning at x,y.In a Java window,the upper-left
corner is location 0,0.The drawString( ) method will not recognize newline characters.If we want
to start a line of text in another line,we must do it manually,specifying the X,Y location where
we want the line to begin.
➢ To set the background color of an applets window,we use setBackground( )
To set the foreground color,we use setForeground( ).These methods are defined y
Component,and have the general forms:
void setBackground(Color newColor)
void setForeground(Color newColor)
Here, newColor specifies the new color.The class Color defines the following values that can be
used to specify colors:
✓ Color.black
✓ Color.magenta
✓ Color.blue
✓ Color.orange
✓ Color.cyan
✓ Color.pink
✓ Color.darkGray
✓ Color.red
✓ Color.gray
✓ Color.white
✓ Color.green
✓ Color.yellow
✓ Color.lightGray
For example,this sets the background color to blue and the text color to yellow:
setBackground(Color.blue);
setBackground(Color.yellow);
we can change these colors as and when required during the execution of the applet.The default
foreground color is black.The default background color is lightgray.
➢ We can obtain the current settings for background and foreground colors by calling
getBackground( ) and getForeground( ),respectively.They are also defined by Component
and bear the general form:
Color getBackground( )
Color getForeground( )
<html>
<body>
<applet code="MyApplet.class" width="250" height="250">
</applet>
</body>
</html>
4. Execute the applet using applet viewer tool at command prompt as shown below:
appletviewer MyApplet.html
AppletViewer:MyApplet.class - X
Applet
Applet started.