0% found this document useful (0 votes)
18 views25 pages

Java Chapter Two

note

Uploaded by

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

Java Chapter Two

note

Uploaded by

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

CHAPTER TWO

JAVA APPLET
JAVA APPLET

• Applet is a special type of program that is embedded in the webpage to


generate the dynamic content.
• It runs inside the web browser and works at client side.
• Applet is embedded in a HTML page using the APPLET or OBJECT
tag and hosted on a web server.
• Applets are used to make the web site more dynamic and entertaining.
cont.…
• 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
• In general, execution of an applet does not begin at main() method.
• Output of an applet window is not performed by System.out.println().
Rather it is handled with various AWT methods, such as drawString().
• Some important points:
• All applets are sub-classes (either directly or indirectly) of
java.applet.Applet class.
Cont.…
• 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 Os etc.
Applet class

 Every applet is implemented by creating a subclass of the Applet class.


An applet program is a written as a inheritance of the java.Applet class
There is no main() method in an Applet.
An applet uses AWT for graphics
Cont.….
Hierarchy of applet  The Applet class extends the AWT Panel class,
which extends the AWT Container class, which
extends the AWT Component class.
 From Component, an applet inherits the ability to
draw and handle events.
 From Container, an applet inherits the ability to
include other components and to have a layout
manager control the size and position of those
components.
 From Applet, an applet inherits several
capabilities, including the ability to respond to
major milestones, such as loading and unloading
life cycle of an applet

• When an applet begins, the following methods


are called, in this sequence.
• init( )
• start( )
• paint( )
• When an applet is terminated, the following
sequence of method calls takes place.
– stop( )
– destroy( )
init( )
Initialization occurs when the applet is first loaded (or reloaded),
similarly to the main() method in applications.
It is the first of your methods to be executed
 Typically this method is used to initialize instance fields, create GUI
components, load sound to play, load image to display.
Applet does not have constructor. Instead, we override the init()
method to initialize the GUI display when the applet is loaded into the
web browser.
It is only executed once
Cont.….
• To provide behavior for the initialization of your applet, override
the init() method in your applet class.
String s= " ";
public void init()
{
s=s+ " int ";
}
Start( )
The start( ) method is called after init( ).
it can happen many different times during an applet's lifetime
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 on screen.
Starting can also occur if the applet was previously stopped.

 To start the applet's execution, such as when the applet's loaded or when the
user revisits a page that contains the applet.
.
paint()
• This method is called by applet container after init() and start()
methods.
• Painting is how an applet actually draws something on the screen, be
it text, a line, a colored background, or an image.
• You override the paint() method if your applet needs to have an
actual appearance on the screen (that is, most of the time).
• The paint method is where the real work of this applet (what little
work goes on) really occurs.
Cont.…
• 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.
• This context is used whenever output to the applet is required.
• called back when the applet drawing area must be refreshed, e.g.,
another window partially covers the applet
cont.….
Example
public void paint(Graphics g) {
g.drawString("Hello again!", 5, 40);
}
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 .


• You should use stop( ) to suspend threads that don’t need to run when the
applet is not visible.
• You can restart them when start( ) is called if the user returns to the page.
• To stop the applet's execution, such as when the user leaves the applet's page or
quits the browser.
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( ).
CONT....
cont.…
 The ways to run an applet
By appletViewer tool (for testing purpose).

//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("welcome to applet",150,150);
} }
java applet vs java application
Java application Java Applet
• Java Application is a type of • A Java Applet is a small program that
program that can get independently makes use of another application
executed on a computer. program so that we can execute it.
• The execution of the Java application • The Java applet initializes through the
begins with the main() method. The init(). It does not require the usage of
usage of the main() is a prerequisite any main() method.
here. • It cannot run independently but
• It cannot run alone, but it requires requires APIs for its execution (Ex.
JRE for its execution. APIs like Web API).
Cont.….

Java application Java Applet


• One needs to install a Java • A Java applet does not require any prior
application priorly and explicitly on a installation.
local computer.
• A Java applet cannot perform these
• The Java applications are capable of applications on any local computer.
performing the read and write
operations on various files present in
a local computer.
displaying graphics in applet
• java.awt.Graphics class provides many methods for graphics programming.
Commonly used methods of Graphics class:
• public 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 specified width and height.
• public 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 void drawOval(int x, int y, int width, int height): is used to draw oval
with the specified width and height.
cont.…

• public 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 void drawLine(int x1, int y1, int x2, int y2): is used to draw line
between the points(x1, y1) and (x2, y2).
• public boolean drawImage(Image img, int x, int y, ImageObserver
observer): is used draw the specified image.
cont.….
• 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.
Eg. Drawing Lines and Rectagles
import java.awt.*;
import java.applet.*;
public class LineRect extends Applet {
public void paint(Graphics g) {
g.drawstring(“Draw line and rectangle”,5,5);
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30); }}
Eg. Drawing fill rectangle and
drawRoundRect
import java.awt.*;
import java.applet.*;
public class LineRect extends Applet {
public void paint(Graphics g) {
g.fillRect(60,10,30,80);
g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(20,110,60,30,5,5);
}}
THE END

You might also like