[go: up one dir, main page]

0% found this document useful (0 votes)
32 views30 pages

Applet

Uploaded by

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

Applet

Uploaded by

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

Chapter 2

Applet
❖An Applet is a program that runs in a web browser.
❖They can be transported over the internet from one computer to
another computer and run using the applet the appletviewer or any
web browser that supports java.
❖Applet can be used to perform arithmetic operations,display
graphics,play sounds,accept user input,create animation and play
interactive games like any other applications.
❖An Applet is a java class that extends the java.applet.Applet class.
Summary
• An applet is a program written in the java programming language that
can be included in ana HTML page,much in the same way an image is
included in a page. When you use a Java technology-enabled browser
to view a page that contains an applet, the applet’s code is
transferred to your system and executed by the browser’s JVm.
Difference between applet and Application
❖Applet don’t use the main method for initiating the execution of the
code.
❖When the applet is loaded, it automatically calls certain methods of
applet class to start and execute the applet code.
❖Unlike stand-alone application, applets cannot be run independently.
They run from inside a web page using a special features known as
HTML tag
❖A JVM is required to view an applet.
When to use Applet
• When we need something dynamic in the display of a web page.
• When we want to create a program and make it available on the
internet for use by others on their computers.
Applet hierarchy
Life cycle of Applet

❖Applet life cycle defined as how the object created, started, stopped and
destroyed during the entire execution of the application is said to applet
life cycle.
Methods of Applet Life Cycle
Applet life cycle has 5 methods.
❖ init()
❖start()
❖ paint()
❖ stop()
❖ destroy()
Continue..
Continue..
• When ever applet is loaded,first of all init() method is called,then
start() method,after that paint() method is called,the stop method()
and at last destroy method is called.
• All this methods are under Applet class.
• These methods are invoked by the browser to execute. Applet works
on the client side so, less processing time.
Continue…
❖init():
➢init() method is used to initialize
➢It is invoking only once at the time of initialization.
Syntax:
public void init()
{
//initialized objects
}
❖start():
➢start() method is used to start the applet.
➢It is invoking after the init()method invoked.
➢ It is invoking each time when browser loading or refreshing.
Syntax:
public void start()
{
//start the applet code
}
Continue…
❖paint():
➢paint() method is used for painting any shapes like square, rectangle,
trapezium, eclipse, etc.
➢paint() method has parameter as ClassGraphics.
➢This Graphics class gives painting features in an applet.
Syntax:
public void paint(Graphics g)
g.drawString(“hello”);
{
//any shapes code
}
❖Stop():
➢stop() method is used to stop the applet.
➢It is invoked every time when browser stopped or minimized or abrupt
failure of the application.
➢After stop()method called, we can also start() method whenever we want.
Syntax:
public void stop(){
//stop the applet code
}
❖destroy():
➢ destroy() method is used to destroy the application once we have
done with our applet work.
➢ It is invoked only once.
➢Once applet is destroyed we can’t start()the applet.
Syntax:
public void destroy(){
//destroy the applet code
}
Steps to create and use an Applet
• Building an applet code(.java file)
• Creating an executable applet(.class file)
• Designing a web page using HTML tags.
• Adding <APPLET> tag and attributes inside applet tag into the web
page.
• Creating html file or you can embed within java file also
• Testing the applet code.
Continue…
Applet can be embedded into web pages in two ways as:
• We can write our own applet and embed them into web pages.
• We can download an applet from a remote computer system and embed it
into the web pages.
Types of Applet:
Local Applet
Remote Applet
Local Applet
❖Applets developed in a local computer are known as local applet.
❖To run local applet in local computer, we don’t need internet.
❖It Simply searches the directories in the local system and locates and
loads the specified applet.
Remote Applet
❖Applets which are developed by someone else and stored on a
remote computer connected to the internet are known as remote
applet.
❖To run the remote applet to the local computer, the local system
must be connected with internet.
❖We can download remote applet onto the local computer via
internet and run it.
Example accessing local and remote applet from html file as shown below:

Applet program:
import java.awt.Graphics;
import java.applet.Applet;
public class HelloApplet extends Applet{
String msg=“Welcome to Applet programming”;
public void paint(Graphics g){
g.drawstring(msg,100,100);
}
}
Html tags for remote applet:

<html>
<head><title></title></head>
<body>
<applet name=“remoteApplet” codebase=“www.mysite.com.np\applet”
code=“HelloApplet.class” width=“400” height=“400”></applet>
</body>
</html>

Html tags for local applet:


<html>
<head><title></title></head>
<body>
<applet name=“localApplet” codebase=“D:\appletdemo\applet”
code=“HelloApplet.class” width=“400” height=“400”></applet>
</body>
</html>
Continue…
import java.applet.Applet;
import java.awt.Graphics;
// <applet code=“AppletLifeCycle.class" width="300" height="300">
// </applet>
public class AppletLifeCycle extends Applet
{
public void init()
{
System.out.println("1.I am init()");
}
public void start()
{
System.out.println("2.I am start()");
}
Continue…
public void paint(Graphics g)
{
System.out.println("3.I am paint()");
}
public void stop()
{
System.out.println("4.I am stop()");
}
public void destroy()
{
System.out.println("5.I am destroy()");
}
}
You can embed Applet in html file
<!DOCTYPE html>
<html>
<head>
<title>Applet Life Cycle</title>
</head>
<body>
<applet code="AppletLifeCycle.class" width="300" height="300"></applet>
</body>
</html>
Program to draw string on applet window
Applet program Applet Embedded in html file
import java.applet.*;
import java.awt.*; <html>
public class AppletDemo extends <head><title>AppletDemo</title>
Applet{ </head><body>
public void paint(Graphics g){ <applet code=“AppletDemo.class”
g.drawString(“hello kcc”,100,100); width=400 height=400></body>
} </html>
}
Using parameter in applet
Applet program Html file
import java.applet.Applet; <html>
import java.awt.Graphics; <body>
<applet code="UseParam.class" width="300" he
public class UseParam extends Applet{ ight="300">
<param name="msg" value="Welcome to applet
">
public void paint(Graphics g){ </applet>
String str=getParameter("msg"); ->welcome to </body>
applet
</html>
g.drawString(str,50, 50);
}

}
Passing Parameter to Applets
Example shows how the html file is sending parameter and how the java code handles these
parameter at runtime.

import java.applet.*; <html>


import java.awt.*; <head><title>Applet Program</title></head>
public class ParameterDemo extends Applet{ <body>
public void paint(Graphics g){ <applet code=“ParameterDemo.class”>
String myFont=getParameter(“font”); <param name=“font" value=“Dialog">
String myString=getParameter(“String”); <param name=“size” value=“24”>
int mySize=Integer.parseInt(getParameter(“size”)); <param name=“String” value=“Kcc”>
Font f=new Font(myFont,Font.BOLD,mySize); </applet>
g.setFont(f); </body>
g.setColor(Color.red); </html>
g.drawString(myString,20,20);
}
}
Addition Example
import java.awt.*; <html>
import java.applet.*; <head><title></title></head>
public class Addition extends Applet{ <body>
int x,y,z; <applet code=“Addition.class” width=400
String sum; height=400>
public void init(){ </body>
x=10; </html>
y=20;
z=x+y;
sum="sum of"+x+"and"+y+"is"+z;
}
public void paint(Graphics g){
g.drawString(sum,10,30);
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.
Continue…
❖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.
Write a program to show different Graphics class methods

import java.applet.Applet; g.drawOval(70,200,30,30);


import java.awt.*; g.setColor(Color.pink);
public class GraphicsDemo g.fillOval(170,200,30,30);
extends Applet{ g.drawArc(90,150,30,30,30,270);
public void paint(Graphics g){ g.fillArc(270,150,30,30,0,180);
g.setColor(Color.red); }
g.drawString("Welcome",50, 50); }
g.drawLine(40,30,40,300);
g.drawRect(70,100,30,30);
Continue…
<html>
<body>
<applet
code="GraphicsDemo.class"
width="300" height="300">
</applet>
</body>
</html>

You might also like