Applets and Advanced Applets
Applets and Advanced Applets
What is Applet?
An applet is a Java program that can be embedded into a web page. It runs
inside the web browser and works at client side. An applet is embedded in
an HTML page using the APPLET or OBJECT tag and hosted on a web
server.
Applets are used to make the website more dynamic and entertaining.
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().
Java applets are one of three kinds of Java programs:
An application is a standalone program that can be invoked from the
command line.
An applet is a program that runs in the context of a browser session.
A servlet is a program that is invoked on a server program, and it runs
in the context of a web server process.
Life cycle of an applet :
When an applet begins, the following methods are called, in this
sequence:
5acXjzUk
1. init( )
2. start( )
3. paint( )
When an applet is terminated, the following sequence of method
calls takes place:
1. stop( )
2. destroy( )
1. init( ) : The init( ) method is the first method to be called. This is where
you should initialize variables. This method is called only once during the
run time of your applet.
2. start( ) : The start( ) method is called after init( ). It is also called to
restart an applet after it has been stopped. Note that init( ) is called once
i.e. when the first time an applet is loaded whereas start( ) is called each
time an applet’s HTML document is displayed onscreen.
3. paint( ) : The paint( ) method is called each time an AWT-based applet’s
output must be redrawn. This situation can occur for several reasons.
The paint( ) method has one parameter of type Graphics. This parameter
will contain the graphics context, which describes the graphics
environment in which the applet is running.
4. stop( ) : The stop( ) method is called when a web browser leaves the
HTML document containing the applet—when it goes to another page, for
example. When stop( ) is called, the applet is probably running.
5. destroy( ) : The destroy( ) method is called when the environment
determines that your applet needs to be removed completely from memory.
At this point, you should free up any resources the applet may be using.
The stop( ) method is always called before destroy( ).
import java.applet.Applet;
import java.awt.Graphics;
5acXjzUk
// Overriding paint() method
@Override
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}
}
There are two standard ways in which you can run an
applet :
1. Executing the applet within a Java-compatible web browser.
2. Using an applet viewer, such as the standard tool, applet-
viewer. An applet viewer executes your applet in a window.
This is generally the fastest and easiest way to test your
applet.
Using java enabled web browser : To execute an applet in a web
browser we have to write a short HTML text file that contains a tag
that loads the applet. We can use APPLET or OBJECT tag for this
purpose. Using APPLET, here is the HTML file that executes
HelloWorld :
<applet code="HelloWorld" width=200 height=60>
</applet>
NOTE : Chrome and Firefox no longer supports NPAPI (technology
required for Java applets).
Using appletviewer : This is the easiest way to run an applet. To
execute HelloWorld with an applet viewer, you may also execute the
HTML file shown earlier. For example, if the preceding HTML file
is saved with
RunHelloWorld.html, then the following command line will run
HelloWorld :
appletviewer RunHelloWorld.html
5acXjzUk
Restrictions imposed on Java applets
Due to security reasons, the following restrictions are imposed on Java applets:
1. An applet cannot load libraries or define native methods.
2. An applet cannot ordinarily read or write files on the execution host.
3. An applet cannot read certain system properties.
4. An applet cannot make network connections except to the host that it
came from.
5. An applet cannot start any program on the host that’s executing it.
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.
5acXjzUk
Displaying Image in Applet
Applet is mostly used in games and animation. For this purpose image is required
to be displayed. The java.awt.Graphics class provide a method drawImage() to
display the image.
The java.applet.Applet class provides getImage() method that returns the object of Image. Syntax
1. public Image getImage(URL u, String image){}
import java.awt.*;
import java.applet.*;
public class AnimationExample extends Applet {
Image picture;
5acXjzUk
public void paint(Graphics g) {
for(int i=0;i<500;i++){
g.drawImage(picture, i,30, this);
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 th
simple example of event handling in applet that prints a message by click on the button.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;
b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
5acXjzUk
}
myapplet.html
<html>
<body>
<applet code="EventApplet.class" width="300" height="300">
</applet>
</body>
</html>
5acXjzUk