[go: up one dir, main page]

0% found this document useful (0 votes)
2 views7 pages

Chapter 14

Applet 14.1 introduces Java applets as small client-side applications designed for embedding in HTML pages, highlighting their advantages such as cross-platform compatibility and security features, along with disadvantages like the need for a Java plug-in. It contrasts applets with applications, detailing their lifecycle methods (init, start, stop, destroy) and how to run them using HTML or appletviewer. The document also provides examples of applet programs and graphics methods available in the java.awt.Graphics class.

Uploaded by

zohosaravanan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Chapter 14

Applet 14.1 introduces Java applets as small client-side applications designed for embedding in HTML pages, highlighting their advantages such as cross-platform compatibility and security features, along with disadvantages like the need for a Java plug-in. It contrasts applets with applications, detailing their lifecycle methods (init, start, stop, destroy) and how to run them using HTML or appletviewer. The document also provides examples of applet programs and graphics methods available in the java.awt.Graphics class.

Uploaded by

zohosaravanan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Applet 14.

14 APPLET

14.1 Introduction to Applet

Applets are small, client-side Java applications, specifically designed to be embedded in


HTML pages. Java applets run on the java enables web browsers such as mozilla and internet
explorer. Applet is designed to run remotely on the client browser, so there are some restrictions
on it. Applet can't access system resources on the local computer. Applets are used to make the
web site more dynamic and entertaining.

Advantages of Applet:

 Applets are cross platform and can run on Windows, Mac OS and Linux platform
 Applets can work all the version of Java Plugin
 Applets runs in a sandbox, so the user does not need to trust the code, so it can work
without security approval
 Applets are supported by most web browsers
 Applets are cached in most web browsers, so will be quick to load when returning to a
web page
 User can also have full access to the machine if user allows

Disadvantages of Java Applet:

 Java plug-in is required to run applet


 Java applet requires JVM so first time it takes significant startup time
 If applet is not already cached in the machine, it will be downloaded from internet and
will take time
 Its difficult to design and build good user interface in applets compared to HTML
technology

14.2 Applet versus Application

Applet Application
Applets are the small programs applications are larger programs.
Applets don't have the main method Applications must have a main().
Java Applets are also compiled using the javac Java applications are compiled using the javac
command, but are run either with a browser or command and run using the java command.
with the appletviewer command.
An Applet also requires an HTML file before it Requires JRE
can be executed. And it need web browser to
execute the file.
Requires highest security for the system as Does not require any security
they are untrusted
cannot access anything on the system except Can access any data or software available on
browser’s services the system
14.2 Java Programming Paradigm

14.3 Applet Hierarchy

Applet program is extended from java.applet.Applet namespace.

java.lang.Object

java.awt.Component

java.awt.Panel

java.applet.Applet

Fig. No: 14.1 The Class hierarchy of applet

14.4 Applet Life Cycle

 Applet is initialized.
 Applet is started.
 Applet is painted.
 Applet is stopped.
 Applet is destroyed.

init() method

start() method

stop() method

destroy() method
Fig. No: 14.2 Applet Life Cycle

init() method: The life cycle of an applet is begin on that time when the applet is first loaded
into the browser and called the init() method. The init() method is called only one time in the life
cycle on an applet.

start() method: The start method of an applet is called after the initialization method init(). This
method may be called multiples time when the Applet needs to be started or restarted.
Applet 14.3

stop() method: The stop() method can be called multiple times in the life cycle of applet like the
start () method. Or should be called at least one time. There is only miner difference between the
start() method and stop () method. For example the stop() method is called by the web browser
on that time When the user leaves one applet to go another applet and the start() method is called
on that time when the user wants to go back into the first program or Applet.

destroy() method: The destroy() method is called only one time in the life cycle of Applet like
init() method. This method is called only on that time when the browser needs to shut down.

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.

How to run an Applet?


There are two ways to run an applet
1. By html file.
2. By appletViewer tool (for testing purpose).

14.5 Applet Programs

Simple example of Applet by html file:


To execute the applet by html file, create an applet and compile it. After that create an
html file and place the applet code in html file. Now click the html file.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome",150,150);
}

}
Example 14.1 Applet program by html file

Note: class must be public because its object is created by Java Plugin software that resides on
the browser.

myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
Example 14.1(Continue) Applet program by html file
14.4 Java Programming Paradigm

Simple example of Applet by appletviewer tool:


To execute the applet by appletviewer tool, create an applet that contains applet tag in
comment and compile it. After that run it by: appletviewer First.java. Now Html file is not
required but it is for testing purpose only.

//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet&gt;
*/
Example 14.2 Applet Program by appletviewer tool
To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java
c:\>appletviewer First.java

Displaying Graphics in Applet

java.awt.Graphics class provides many methods for graphics programming.

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 specifed
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.
Applet 14.5

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);
}
}
Example 14.3 Graphics Program in applet:
myapplet.html

<html>
<body>
<applet code=" GraphicsDemo.class " width="300" height="300">
</applet>
</body>
</html>
Example 14.3(Continue) Graphics Program support by html

/*
<applet code="LifeCycle.class" width=200 height=200></applet>
*/
import java.applet.Applet;
import java.awt.*;
public class LifeCycle extends Applet
{
14.6 Java Programming Paradigm

String output = "";


String event;
public void init()
{
event = "Initializing...";
printOutput();
}
public void start()
{
event = "Starting...";
printOutput();
}
public void stop()
{
event = "Stopping...";
printOutput();
}
public void destroy()
{
event = "Destroying...";
printOutput();
}
private void printOutput()
{
System.out.println(event);
output += event;
repaint();
}
public void paint(Graphics g)
{
g.drawString(output, 10, 10);
}
}
Example 14.4 Applet Life Cycel Example Program

I:\java\graphics>appletviewer LifeCycle.java
Initializing...
Starting...
Stopping...
Starting...
Stopping...
Starting...
Stopping...
Destroying...
Applet 14.7

Output:

Review Questions
1. Which classes can an applet extend?
2. What is the difference between applet and application program?
3. What is the order of method invocation in an applet?

You might also like