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

Java Applet.pptx

The document provides an overview of Java Applets, highlighting their advantages such as client-side execution and cross-platform compatibility, as well as drawbacks like the need for a browser plugin. It details the applet lifecycle, including initialization, starting, painting, stopping, and destruction, along with the relevant lifecycle methods. Additionally, it explains how to run an applet using an HTML file or the appletViewer tool, and lists common graphics methods available in the java.awt.Graphics class.

Uploaded by

santar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views12 pages

Java Applet.pptx

The document provides an overview of Java Applets, highlighting their advantages such as client-side execution and cross-platform compatibility, as well as drawbacks like the need for a browser plugin. It details the applet lifecycle, including initialization, starting, painting, stopping, and destruction, along with the relevant lifecycle methods. Additionally, it explains how to run an applet using an HTML file or the appletViewer tool, and lists common graphics methods available in the java.awt.Graphics class.

Uploaded by

santar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Java Applet

Introduction
Applet is a special type of program that is embedded in the
webpage to generate the dynamic content. It runs inside the
browser and works at client side.
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 plateforms,
including Linux, Windows, Mac Os etc.
Drawback of Applet
• Plugin is required at client browser to execute applet.
Hierarchy of Applet

As displayed in the above diagram,


Applet class extends Panel. Panel
class extends Container which is the
subclass of Component.
Lifecycle of Java Applet

• Applet is initialized.
• Applet is started.
• Applet is painted.
• Applet is stopped.
• Applet is destroyed.
Lifecycle methods for Applet:

• The java.applet.Applet class 4 life cycle methods and java.awt.Component


class provides 1 life cycle methods for an applet.
java.applet.Applet class
• For creating any applet java.applet.Applet class must be inherited.
• It provides 4 life cycle methods of applet.
✔ public void init(): is used to initialized the Applet. It is invoked only once.
✔ public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
✔ public void stop(): is used to stop the Applet. It is invoked when Applet is
stop or browser is minimized.
✔ public void destroy(): is used to destroy the Applet. It is invoked only once.
java.awt.Component class

• 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


• By html file.
• By appletViewer tool (for testing purpose).
Simple example of Applet by html file:

• To execute the applet by First.java


html file, create an applet import java.applet.Applet;
and compile it. After that import java.awt.Graphics;
create an html file and public class First extends Applet
place the applet code in {
html file. Now click the
html file
public void paint(Graphics g){
g.drawString("welcome",150,150);
}

}
myapplet.html

<html>
<body>
<applet code="First.class" width="300" height="30
0">
</applet>
</body>
</html>
To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java
c:\>appletviewer First.java
Displaying Graphics in Applet
1.public abstract void drawString(String str, int x, int y): is
used to draw the specified string.
• java.awt.Graphics 2.public void drawRect(int x, int y, int width, int height): draws
class provides many a rectangle with the specified width and height.
methods for graphics 3.public abstract void fillRect(int x, int y, int width, int
programming. height): is used to fill rectangle with the default color and
• Commonly used specified width and height.
methods of Graphics 4.public abstract void drawOval(int x, int y, int width, int
class: 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
1.public abstract boolean drawImage(Image img, int x, int y,
ImageObserver observer): is used draw the specified image.
2.public abstract void drawArc(int x, int y, int width, int height, int
startAngle, int arcAngle): is used draw a circular or elliptical arc.
3.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.
4.public abstract void setColor(Color c): is used to set the graphics current
color to the specified color.
5.public abstract void setFont(Font font): is used to set the graphics
current font to the specified font.
Syllabus completed…..!!!

You might also like