Applet
Applet
HOOGHLY
LEARNING MATERIALS
Department : Computer Science & Technology Semester : FIFTH(5th )
UNIT : (5)Java Applets and Graphics Programming Course Code : CST/5/502Contact Periods : 06 Hrs
Teacher : Debasish Hati Date : ____________________
[Instructions : At the end of this lesson, teacher is asked to give MCQ / Short type questions / Broad type
Questions related to this Unit]
Learning objectives: After completing this topic, the students will be able to:
ILO-5.1.1: Examine web based application using Applet.(AN/PK)
ILO-5.2.1: Classify different graph using applet. (AN/PK)
ILO-5.2.2: Examine Lines and rectangle, Circle.(AN/PK)
_________________________________________________________________________
Content:
5.1 Applet Programming Local and remote applets, How applet differ from application, Preparing to write applets, Building applet code, Applet
life cycle,Creating an Executable Applet, Designing a Web page, Applet tag,Adding Applet to HTML file, Running the Applet,
Passing parameter to applet
5.2 Graphics Programming The Graphics Class, Lines and rectangle, Circle and Ellipse, DrawingArcs, Drawing Polygons, Line Graphs, Using
control loops in Applets,Drawing Bar charts
Introduction
An Applet is the special type of Java program that is run on web browser. The Applet class
provides the standard interface between applet and browser.
An applet class does not have main method and it extends the java.applet.Applet class.
An applet program runs through applet viewer.
Advantages of Applets
2. start ( )
It is invoked after the initialization state and applet entered into running state. It starts
the execution of Applet and the applet becomes an active state.
Syntax
3. paint( )
This method is used to display output on screen. It takes java.awt.Graphics object as a
parameter. It helps to create Applet GUI such as a colored background, drawing etc.
Syntax
4. stop( )
This method is responsible to stop the execution and applet becomes temporarily inactive
state. It is invoked when Applet is stopped.
Syntax
5. destroy( )
It destroys and removes the applet from the memory. It is invoked only once. This is end
of the life cycle of applet and it becomes dead.
import java.awt.*;
import java.applet.*;
public class AppletDemo
{
public void paint(Graphics g)
{
g.drawString("Welcome to TutorialRide", 50, 50);
}
}
//AppletDemo.html
<html>
<head>
<title> AppletDemo </title>
</head>
NOTE: To execute the applet program through appletviewer tool, write it on command
prompt.
C :\> javac AppletDemo.java
C :\> appletviewer AppletDemo.java
Applications Applets
An application runs stand-alone. An applet program runs under the control of browser.
It can access any data or software available on the It cannot access anything on the system except browser’s
system. service.
Execution start from the init() method because main() is not
Execution start from the main() method.
available.
It can read and write to the file system. Cannot read and write to the file systems.
Does not provide any security. An applet provides the security features.
Application run using Java interpreter. It runs using applet viewer or on web browser.
Parameter in Applet
Example:
name = color
Value = red
Syntax:
Copy
In an applet code, applet can refer to a parameter by its name and
then find its value.
The two most important thing to handle and set up the parameter is
the <PARAM> tag in the HTML document and an applet code to
parse this parameter.
init() method is used to get hold of the parameters which is defined
in the <PARAM> tags. And getParameter() method is used for
getting the parameters.
In Applet, Parameters are passed on applet when it is loaded.
Example:
param.java
import java.awt.*;
String str;
str=getParameter("pname");
if (str == null)
Copy
param.html
<html>
</applet>
</html>
Copy
f:/>appletviewer run.htm
Copy
Example:
GraphicsDemo1.java
import java.applet.Applet;
import java.awt.*;
g.setColor(Color.black);
g.setColor(Color.blue);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
g.drawLine(21,31,20,300);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
GraphicsDemo1.html
<html>
<body>
</applet>
</body>
</html>
Loading an image
Displaying an image
Example:
Aimage.java
import java.awt.*;
import java.applet.*;
Image img1;
img1=getImage(getDocumentBase(),"icon.png");
g.drawImage(img1,100,100,this);
Aimage.html
<html>
</applet>
</html>
Copy
Example:
EventAppletDemo.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
Button b1;
TextField tf1;
tf1=new TextField();
tf1.setBounds(30,40,200,20);
b1=new Button("Click");
b1.setBounds(80,150,60,50);
add(b1);
add(tf1);
b1.addActionListener(this);
setLayout(null);
tf1.setText("Welcome to studytonight");
Myapplet.html
<html>
<body>
</applet>
</body>
</html>
Example:
AnimationDemo.java
Java Programming | UNIT-5 | Prepared By: Debasish Hati, Incharge, DCST
import java.awt.*;
import java.applet.*;
Image p;
p = getImage(getDocumentBase(),"ball.gif");
for(inti=0;i<500;i++)
try
{
Java Programming | UNIT-5 | Prepared By: Debasish Hati, Incharge, DCST
Thread.sleep(100);
catch(Exception e)
{}
AnimationDemo.html
<html>
<body>
</applet>
</body>
</html>
Example:
JAppletDemo.java
import java.applet.*;
import javax.swing.*;
Java Programming | UNIT-5 | Prepared By: Debasish Hati, Incharge, DCST
import java.awt.event.*;
JButton b;
JTextField t;
t=new JTextField();
t.setBounds(30,40,220,20);
b=new JButton("Click");
b.setBounds(80,150,70,40);
add(b);
add(t);
b.addActionListener(this);
setLayout(null);
t.setText("Welcome to studytonight.com");
JAppletDemo.html
<html>
<body>
</applet>
</body>
</html>
Example:
PaintingDemo.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
addMouseMotionListener(this);
setBackground(Color.white);
Graphics g=getGraphics();
g.setColor(Color.black);
g.fillOval(me.getX(),me.getY(),5,5);
{}
<html>
<body>
</applet>
</body>
</html>
Example:
AnalogDemo1.java
import java.applet.*;
import java.awt.*;
import java.text.*;
Thread t = null;
booleanthreadSuspended;
width = getSize().width;
height = getSize().height;
setBackground( Color.black );
if ( t == null )
t.setPriority( Thread.MIN_PRIORITY );
threadSuspended = false;
t.start();
else
if ( threadSuspended )
threadSuspended = false;
synchronized( this )
threadSuspended = true;
try {
while (true) {
if ( threadSuspended ) {
synchronized( this ) {
while ( threadSuspended ) {
wait();
repaint();
t.sleep( 1000 );
catch (Exception e) { }
angle += 2*Math.PI/3;
angle += 2*Math.PI/3;
g.setColor( Color.pink );
g.setColor( Color.white );
Copy
AnalogueDemo1.html
<html>
<body>
</applet>
</body>
</html>
Example:
DigitalClockDemo1.java
import java.applet.*;
import java.util.*;
import java.text.*;
Thread t = null;
setBackground( Color.black);
t.start();
try
while (true)
h = cal.get( Calendar.HOUR_OF_DAY );
if ( h> 12 ) h -= 12;
m = cal.get( Calendar.MINUTE );
s = cal.get( Calendar.SECOND );
SimpleDateFormat f = new
SimpleDateFormat("hh:mm:ss");
repaint();
catch (Exception e) { }
g.setColor( Color.white );
Copy
DigitalClockDemo1.html
<html>
<body>
</applet>
</body>
</html>