Applet Programming Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Applet Programming

• Applets are small java programs that are


primarily used in Internet computing. They
can be transported over internet from one
computer to another and run using Applet
Viewer or any web browser that supports
Java.
• In other words, 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.
How applets differ from standalone applications

There are some important differences between an applet and

a standalone Java application, including the following −

• An applet is a Java class that extends

the java.applet.Applet class.

• A main() method is not invoked on an applet. Applets, when

loaded, automatically call certain methods of Applet class to

start and execute applet code.

• 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 downloaded 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.


• The JVM on the user's machine creates an instance of the applet class

and invokes various methods during the applet's lifetime.


• Applets can not read from or write to the files in the local computer.
Preparing to write applets
Before we try to write applets, we must make sure that Java is installed

properly and also ensure that either Java appletviewer or a Java enabled

browser is available. The steps in involved in developing and testing in

applet are:

• Building an applet code(.java file)

• Creating an executable applet(.class file)

• Designing a web page using HTML tags.

• Preparing <applet> tag

• Incorporating <applet> tag into the web page.

• Testing the applet code


• Applet code uses two classes, namely Applet and
Graphics from the java class library. Applet class is
contained in java.applet package having methods
init(), start(), paint() etc. When applet is loaded, java
automatically calls a series of applet class methods
for starting, running and stopping the applet code.
The applet class therefore maintains the lifecycle of
an applet.
• The paint() method of the applet class displays the result
of the applet code on the screen. The output may be text,
graphics or sound.

• The paint() method, which requires a Graphics object as


an argument, is defined as follows:

public void paint(Graphics g)

This requires that the applet code imports the java.awt


package that contains the Graphics class.
• The general form of the applet code:
• import java.awt.*;
• import java.applet.*
• …………
• …………
• public class appletclassname extends Applet
• {
• ……..
• ………
• public void paint(Graphics g)
• {
• ……………..
• ……………..
• ……………..
• }
• ………………….
• ………………….
• }

• The HelloJava applet(example)
import java.awt.*;
import java.applet.*
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello Java”, 100, 200);
}
}
• The applet life cycle can be defined as the process
of how the object is created, started, stopped, and
destroyed during the entire execution of its
application. It basically has five core methods
namely init(), start(), stop(), paint() and
destroy().These methods are invoked by the
browser to execute.
• init(): The init() method is the first method to run that initializes

the applet. It can be invoked only once at the time of initialization.

• start(): The start() method contains the actual code of the applet

and starts the applet. It is invoked immediately after the init()

method is invoked. Every time the browser is loaded or refreshed,

the start() method is invoked. It is also invoked whenever the

applet is maximized, restored, or moving from one tab to another

in the browser. It is in an inactive state until the init() method is

invoked.
• stop(): The stop() method stops the execution of the applet. The stop ()

method is invoked whenever the applet is stopped, minimized, or moving from

one tab to another in the browser. When we go back to that page, the start()

method is invoked again.

• destroy(): The destroy() method destroys the applet after its work is done. It is

invoked when the applet window is closed or when the tab containing the

webpage is closed. It removes the applet object from memory and is executed

only once. We cannot start the applet once it is destroyed.

• paint(): The paint() method belongs to the Graphics class in Java. It is used to

draw shapes like circle, square, trapezium, etc., in the applet. It is executed

after the start() method and when the browser or applet windows are resized.
Applet Tag
• The <applet>….</applet> tag is used inside the <body>….</body> tag of the

HTML document that supplies the name of the applet to be loaded.

• For example

• <applet

code = helloJava.class

width=400

height=200>

</applet>

This HTML code tells the browser to load the compiled java applet

helloJava.class, which is in the same directory as the HTML file. It specifies the

display are of the applet output as 400 pixels width and 200 pixels height.
• //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);
• }

• }
myapplet.html
• <html>
• <head>
• <title> Welcome to java applets</title>
• </head>
• <body>
• <applet code="First.class" width="300" height="300">
• </applet>
• </body>
• </html>
Running the applet
• To run an applet, we require one of the following tools:
– Java enabled web browser
– Java appletviewer
• If we use a java enabled web browser, we will be able to see the
entire web page containing the applet. If we use the
appletviewer tool, we will see the applet output. The
appletviewer is available as part of the JDK. We can use it to run
the applet as follows:
appletviewer myapplet.html
Different attributes of the applet tag
Attribute Values Description

align Specifies the alignment of an applet.

alt Specifies an alternate text for an applet.

archive Specifies the location of an archive file.

Specifies the border around the applet


border
panel.

Specifies a relative base URL for applets


codebase
specified in the code attribute.

height Specifies the height of an applet.

Defines the horizontal spacing around


hspace
an applet.
Indicates whether the Java
applet is allowed to access the
mayscript scripting objects of the web
page.
Defines the name for an applet
name (to use in scripts).
Defines the vertical spacing
vspace around an applet.
Specifies the width of an
width applet.
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 specified 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.

• 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.
Example of Graphics in applet:

• 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);

• }
• }
myapplet.html

• <html>
• <body>
• <applet code="GraphicsDemo.class" width="300"
height="300">
• </applet>
• </body>
• </html>

You might also like