Você está na página 1de 14

Applets

Prepared by: Rupal Parekh


Shree M & N Virani Science College

What is an Applet?
An applet is a Java program that runs on a web
page
Applets can be run within any modern browser
To run modern Java applets, old browsers need an upto-date Java plugin
appletviewer is a program that can run applet.

An applet must be a subclass of the


java.applet.Applet class. The Applet class
provides the standard interface between the
applet and the browser environment.
The browser's Java Plug-in software manages the
lifecycle of an applet.

Applets
An applet is a Java class that extends the
java.applet.Applet class.
A main() method is not invoked on an applet,
and an applet class will not define main().
Applets are designed to be embedded within
an HTML page.
When a user views an HTML page that contains
an applet, the code for the applet is download
to the user's machine.
A JVM is required to view an applet. The JVM
can be either a plug-in of the Web browser or a
separate runtime environment.

Life cycle of an Applet

Methods of java.applet.Applet class


void init()

It is called when an applet is initialized. It is called first


for any applet.

void start()

This method is automatically called after the browser


calls the init method. It is also called whenever the user
starts or resumes an applet.

void stop()

This method is automatically called when the user wants


to suspend the execution of an applet and restarted
with start method.

void destroy()

This method is only called when the browser shuts down


normally.

AppletContext
getAppletContext()

Determines this applet's context, which allows the


applet to query and affect the environment in which it
runs.

String getAppletInfo()

Returns information about this applet.

Methods.
URL getCodeBase()

Gets the base URL.

URL getDocumentBase()

Gets the URL of the document in which this


applet is embedded.

String
getParameter(String name)

Returns the value of the named parameter in


the HTML tag.

boolean isActive()

Determines if this applet is active.

void resize(Dimension d)

Requests that this applet be resized.

void resize(int width, int height) Requests that this applet be resized.

void showStatus(String msg)

Requests that the argument string be displayed


in the "status window".

Paint method
This method is of java.awt.Graphics class.
Which is used to display context into the
applet.
You have implement paint( ) into every applet.
This method is called by start( ) method.
Simple example of an applet.
Example for lifecycle of an applet.

Graphics class
The graphics class is member of java.awt
package.
Contains several methods that can help to
perform graphics related operations.

Methods of Graphics class


void drawString(String, int, int)

Draws the specified String using the


current font and color.

drawLine(int x1, int y1, int x2,


int y2)

Draws a line between the coordinates


(x1,y1) and (x2,y2).

drawRect(int x, int y, int width,


int height)

Draws the outline of the specified


rectangle using the current color.

fillRect(int x, int y, int width, int


height)

Fills the specified rectangle with the


current color.

drawOval(int x, int y, int width,


int height)

Draws an oval inside the specified


rectangle using the current color.

fillOval(int x, int y, int width, int


height)

Fills an oval inside the specified rectangle


using the current color.

Methods.
void drawArc(int x, int y, int
width, int height, int startangle,
int endangle)

Draws an arc bounded by the specified


rectangle from startAngle to endAngle.

void fillArc(int x, int y, int width,


int height, int startangle, int
endangle)

Fills an arc using the current color.

void drawPolygon(int x[], int y[],


int numofpoints)

Draws a polygon defined by an array of x


points and y points.

void fillPolygon(int x[], int y[], int Fills a polygon using the current color.
numofpoints)

Rectangle
drawRect(int x, int y, int width, int height)
drawRect(10,10, 100, 90)

fillRect(int x, int y, int width, int height)


fillRect(150,10, 100, 90)

Oval
drawOval(int x, int y, int width, int height)
drawOval(10,10, 100, 90)

fillOval(int x, int y, int width, int height)


fillOval(150,10, 100, 90)

Arc
drawArc(int x, int y, int width, int height, int
startAngel, int endAngel)
drawArc(10,10, 100, 90)
fillArc(int x, int y, int width, int height, int startAngel,
int endAngel)
fillArc(150,10, 100, 90)

Arc
drawArc(int x, int y, int width, int height, int
startAngel, int endAngel)
drawArc(10,10, 100, 90)
fillArc(int x, int y, int width, int height, int startAngel,
int endAngel)
fillArc(150,10, 100, 90)

Você também pode gostar