Documente Academic
Documente Profesional
Documente Cultură
Attitude is a
small
BIG
difference.
Introduction
Applets are small Java programs
Used in Internet computing
Can be transported over Internet
Run using AppletViewer or Web Browser
Difference between Applications and Applets
1. Applets can be embedded in HTML pages
and downloaded over the Internet whereas
Applications have no special support in HTML
for embedding or downloading.
2. Applets can only be executed inside a java
compatible container, such as a browser or
appletviewer whereas Applications are
executed at command line by java.exe.
3. Applets execute under strict security
limitations that disallow certain operations
whereas Applications have no inherent
security restrictions.
4. Applets don’t have the main() method as in
applications. Instead they operate on an
entirely different mechanism where they are
initialized by init(),started by start(),stopped
by stop() or destroyed by destroy().
5. Applets can not read from or write to the
files in the local computer.
6. Applets can not communicate with the
other servers on the network.
7. Applets can not run any program from the
local computer.
Steps for writing and Testing Applets
1. Building an Applet code(.java file)
2. Creating an executable applet(.class file)
3. Designing a web page using HTML tags
4. Preparing <APPLET…> Tag
5. Incorporating <APPLET…> Tag into web
page
6. Creating a HTML file
7. Testing the applet code
Building an Applet code
Applet code uses methods from two classes:
Applet and Graphics
The Applet class contained in java.applet
package includes init(), start() and paint()
methods. Java automatically calls a series
of Applet class methods for starting, running
and stopping the applet code.
The Applet class maintains the lifecycle of
an applet.
The paint() method, when called, displays
the result of the applet on the screen. The
output may be text, graphics or sound. The
paint() method requires a graphics object as
an argument-
public void paint( Graphics g)
This requires the import of java.awt
package that contains the Graphics class.
All output operations of an applet are
performed using methods defined in the
Graphics class.
General form of Applet code
import java.applet.*;
import java.awt.*;
…………..;
…………..;
public class Appletclassname extends Applet
{
…………..;
…………..;
public void paint (Graphics g)
{
……………….;
}
}
The following is a simple applet named
WelcomeJavaApplet.java:
import java.applet.*;
import java.awt.*;
public class WelcomeJavaApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString (“Welcome to Java", 25, 50);
}
}
Graphics.drawString() :
The drawSring() method draws the given string.
Here is the syntax of the drawString() method :
Graphics.drawLine() :
The drawLine() method is used to draw the line
in the applet. Here is the syntax for the
drawLine() method :
Graphics.drawRect() :
The drawRect() method draws the rectangle. Here
is the syntax of the drawRect() method:
g.setColor(Color.color_name);
Graphics.fillOval() :
This is the fillOval() method used to fill the color
inside the oval by specified color. Here is the
syntax of the fillColor() method :
g.fillOval(Color.color_name);
fillRect() :
This is the fillRect() method used to fill the color
inside the rectangle by specified color. Here is
the syntax of the fillRect() method :
drawArc():
Foreground color:
void setForeground(Color.white);
import java.awt.*;
import java.applet.*;
/* <applet code="WelcomeApplet" width=200
height=200> </applet> */
public class WelcomeApplet extends Applet {
public void paint(Graphics g) {
g.setColor(Color.yellow);
setBackground(Color.blue);
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("Welcome to Java Applets", 70, 70);
}
}
Applet Life Cycle
Every applet inherits set of default behavior
from the Applet class.
When an applet is loaded it undergoes
series of changes in its state.
The applet states include-
Born or Initialization state
Running state
Idle state
Dead or Destroyed state
Applet’s state transition diagram
Begin Born Initialization
(Load Applet)
start()
stop()
start()
paint() destroy()
Exit of Browser
Initialization state
Applets enter initialization state when it is
first loaded, by calling init() method of
Applet class. The applet is born. At this
stage, we can
Create objects needed by applet
Set initial values
Load images or fonts
Set colours
The initialization occurs only once.
To provide any of the behaviours listed
above, init() method must be overridden.
public void init()
{
----------------;
----------------;//action statements
----------------;
}
Running state
Applet enters running state when system
calls start() method of Applet class.
This happens automatically after the
applet is initialized.
The start() method can be called more
than once. It may be overridden.
public void start()
{
----------------;
----------------;//action statements
----------------;
}
Idle or Stopped state
An applet becomes idle when it is stopped
from running.
The stop() method can be overridden.