Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

What is applet?

• Small Java programs that are embedded in Web pages.


• Transported over the Internet from server to client computers.
• Run using the Applet Viewer or any Web browser that supports Java.

What an Applet can do?


• Perform arithmetic operations.
• Display graphics
• Play sound
• Accept user input
• Create animation
• Play interactive games
How Applets Differ from Applications
• Applets don’t use the main() method, but when they are loaded,
automatically call certain methods (init, start, paint, stop, destroy).
• They are embedded inside a web page and executed in browsers.

• They cannot read from or write to the files on local computer.

• They cannot communicate with other servers on the network.

• They cannot run any programs from the local computer.

• They are restricted from using libraries from other languages.


Example
//HelloWorldApplet.java

import java.applet.*;

import java.awt.*;

public class HelloWorldApplet extends Applet {

public void paint(Graphics g) {

g.drawString ("Hello World of Java!",25, 25);

}
Embedding Applet in Web Page
<HTML>

<HEAD><TITLE>Hello World Applet</TITLE>

</HEAD>

<BODY>

<h1>Hi, This is My First Java Applet on the Web!</h1>

<APPLET CODE=HelloWorldApplet.class width=500 height=400>

</APPLET>

</BODY>

</HTML>
Applet Life Cycle
• Every applet inherits a set of default behaviours from the Applet
class. As a result, when an applet is loaded, it undergoes a series of
changes in its state.
• The applet states include:
– Initialisation – invokes init()
– Running – invokes start()
– Display – invokes paint()
– Idle – invokes stop()
– Dead – invokes destroy()
Applet States
• Initialisation – invokes init() – only once
– Invoked when applet is first loaded.
• Running – invokes start() – more than once
– For the first time, it is called automatically by the system after init()
method execution.
– It is also invoked when applet moves from idle/stop() state to active
state. For example, when we return back to the Web page after
temporary visiting other pages.
• Display – invokes paint() - more than once
– It happens immediately after the applet enters into the running
state. It is responsible for displaying output.
• Idle – invokes stop() - more than once
– It is invoked when the applet is stopped from running. For example,
it occurs when we leave a web page.
• Dead/Destroyed State – invokes destroy() - only once
– This occurs automatically by invoking destroy() method when we
quite the browser.
Applet Life Cycle Diagram

init()
Begin Born
stop()
start()

Running Idle

destroy()
paint() start()

Dead End
Passing Parameters to Applet
:

<APPLET CODE=HelloAppletMsg.class width=500 height=400>

<PARAM NAME="Greetings" VALUE="Hello Friend, How are you?">

</APPLET>

:
Applet Program Accepting Parameters
//HelloAppletMsg.java
import java.applet.Applet;
import java.awt.*;

public class HelloAppletMsg extends Applet {

String msg;

public void init()


{
msg = getParameter("Greetings");
if( msg == null)
msg = "Hello";
}
public void paint(Graphics g) {
g.drawString (msg,10, 100);
}
} This is name of parameter specified in PARAM tag;
This method returns the value of parameter.
Graphics Class: Methods
• drawArc(int x, int y, int width, int height,
int startAngle, int sweepAngle)
• drawLine(x1,y1,x2,y2)
• drawOval(x,y,width,height)
• drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
• drawRect(x,y,width,height)
• drawRoundRect(x,y,width,height,cornerWidth,cornerHeight)
• drawString(String s, int x, int y)
• fillArc()
• fillOval()
• fillPolygon()
• fillRect()
• fillRoundRect()
• setColor(Color c)
• setFont(Font f)
Drawing Lines and Rectangles
import java.awt.*;
import java.applet.*;

public class LineRect extends Applet


{
public void paint(Graphics g)
{
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30);
g.fillRect(60,10,30,80);
g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(20,110,60,30,5,5);
g.drawLine(100,10,230,140);
g.drawLine(100,140,230,10);
}
}
Happy Face Applet
import java.awt.*;
import java.applet.*;

public class Face extends Applet


{
public void paint(Graphics g)
{
g.drawOval(40,40,120,150); //Head
g.drawOval(57,75,30,20); //Left eye
g.drawOval(110,75,30,20); //Right eye
g.fillOval(68,81,10,10); //Pupil (left)
g.fillOval(121,81,10,10); //Pupil (right)
g.drawOval(85,100,30,30); //Nose
g.fillArc(60,125,80,40,180,180); //Mouth
g.drawOval(25,92,15,30); //Left ear
g.drawOval(160,92,15,30); //Right ear
}
}
Polygon example
import java.awt.*;
import java.applet.*;

public class Poly extends Applet


{
int x1[]={20,120,220,20};
int y1[]={20,120,20,20};
int x2[]={120,220,220,120};
int y2[]={120,20,220,120};

public void paint(Graphics g)


{
g.drawPolygon(x1,y1,4);
g.fillPolygon(x2,y2,4);
}
}

You might also like