Java Programming (22412)
Unit No.5
Java Applets and Graphics Programming
CO e. Develop programs using graphics and applet.
Applet
⚫ An applet is a Java program that runs on a web page. It is embedded in a web page to
generate the dynamic contents.
⚫ Applet can accessed on an Internet Server, transported over Internet & can be
automatically installed & run as a part of web document
⚫ Applets can be run
⚫ within any modern browser that support Java
⚫ can run using the AppletViewer
⚫ To run Applet, it must be included in HTML tag for web page
⚫ Java Applet can propduce graphics, sounds & moving images(so it has significant impact
on WWW).
⚫ Every Applet is created by sub-classing Applet Class
Advatanges of Applet
It work at client side so require very less time
It is very secured
It can be executed by browser running under many platforms, including Linux, MacOS,
win etc.
Disadvatanges of Applet
Plugin is required at client side to execute applet
Notes By – Rajan Shukla. 1
Java Programming (22412)
Applet class Hierarchy
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
Java.awt.component – this class encapsulate all attributes of visual components. This is
responsible for remembering background & foreground color& font.
Java.awt.conatiner – it contain components like button, text field, labels etc.
Java.awt.panel – it is concentrate sub class of container
Applet Type
1. Local Applet- an Applet is developed locally & stored in a local system is known as
Local Applet
2. Remote Applet- an Applet is designed by someone else on a remote computer is known
as Remote Applet. To access remote applet Internet connection is required
Difference between Applet & Application
Applet Application
Not fully-featured application fully-featured application
Notes By – Rajan Shukla. 2
Java Programming (22412)
Are designed for internet Not specific
Have limitations & restrictions No limitation & restrictions
Does not contain main() Contain main()
Can not run independently Can run independantly
Need HTML tag to run Not required any specific tag
Can not communicate with other Can communication
server directly
Syntax –
Import java.awt.*;
Import java.applet.*;
public class appletname extends Applet
public void paint(Graphics g)
Notes By – Rajan Shukla. 3
Java Programming (22412)
Applet Life Cycle –
States are –
1. Born on initialization state
2. Running state
3. Idle state
4. Dead or destroyed state
When the applet begins, Thejava.applet.Applet class is having following 4 life cycle methods –
1. init() - when applet is first loaded, it enters into initialization state by calling init(). When
applet is born it can do –
o create objects needed by Applet
o Set up initial values
o Load images or fonts
o Set up colors
Notes By – Rajan Shukla. 4
Java Programming (22412)
The initialization of an applet occurs only once in the applet’s life cycle.
Syntax – public void init()
2. start() –when system calls the start(), the applet enters into the running state. This
method is automatically called after the browser calls the init () method. It is also called
whenever the user returns to the page containing the applet after having gone off to other
pages.
The start method may be called more than once.
Syntax – public void start()
3. stop() - This method is automatically called when the user moves off the page on which
the applet is running. If thread is used to run the applet then stop() is used to terminate the
thread.
Syntax – public void stop()
4. destroy () - This method is only called when the browser shuts down normally. When an
applet is removed from memory, then it is dead. This method can be override to clean up
the resources created by applet.
Syntax – public void destroy()
Notes By – Rajan Shukla. 5
Java Programming (22412)
The java.awt.Components class provide paint() methods for applet -
5. paint() - Invoked immediately after the start() method, and also any time the applet needs
to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
Syntax – public void paint(Graphics g)
<Applet>&<Param>Tag –
The <Applet> tag embeds the applet in your HTML page. The following attributes can be
set for the <Applet> tag:
Attribute Explanation Example
Code Name of class file Code="myapplet.class"
Width=n n=Width of applet Width=200
Height=n n=Height of applet Height=100
Codebase Library where the applet is stored. Codebase="applets/"
If the applet is in same directory as
your page this can be omitted.
Alt="Text" Text that will be shown in alt="Menu Applet"
browsers where the ability to show
applets has been turned off.
Name=Name Assigning a name to an applet can Name="starter"
be used when applets should
communicate with each other.
Align=Left/ Justifies the applet according to the Align=Right
Right/ Top/ text and images surrounding it.
Texttop/ A full explanation of the individual
Middle/ parameters is given here.
Absmiddle
Baseline
Bottom
Absbottom
Vspace=n Space over and under the applet. Vspace=20
Notes By – Rajan Shukla. 6
Java Programming (22412)
Hspace=n Space to the left and right of Hspace=40
applet.
The <Param> tag is used to enter parameters for the applet.The <Param> tag has the
general syntax:
<Param Name=NameOfParameter Value="ValueOfParameter">
Example -“Create simple applet”
Steps –
1. Create myapp.java File
Import java.awt.*;
Import java.applet.*;
public class myapp extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello",50,50);
}
}
2. Compile myapp.java file using command prompt–
javac myapp.java
3. Create HTML filemyappweb.html -
<HTML>
<HEAD>
<TITLE> Applet Prg</TITLE>
</HEAD>
<BODY>
This is my first applet prg<br>
<APPLET code="myapp.class" width=200 height=200>
</APPLET>
</BODY>
</HTML>
Notes By – Rajan Shukla. 7
Java Programming (22412)
4. Run HTML file with AppletViewer using commandprompt-
Appletviewer myappweb.html
Output –
Example – “Applet program using param tag”
Notes By – Rajan Shukla. 8
Java Programming (22412)
Commonly used methods of Graphics class –
Example –
1. Create GaphicsDemo.java file
Import java.applet.Applet;
Import java.awt.*;
public class GraphicsDemo extends Applet
public void paint(Graphics g)
g.setColor(Color.blue);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
Notes By – Rajan Shukla. 9
Java Programming (22412)
g.fillRect(70,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(70,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(90,150,30,30,30,180);
2. Create GraphicsDemo.HTML file
<html>
<body>
<applet code="GraphicsDemo.class" width="300" height="300">
</applet>
</body>
</html>
Notes By – Rajan Shukla. 10
Java Programming (22412)
Example – Create applet to print Life cycle of Applet
1. Create AppLifeCycle.java file
Import java.awt.*;
Import java.applet.*;
public class AppLifeCycle extends Applet
String msg=" ";
public void init()
msg+="init()--->";
setBackground(Color.blue);
public void start()
msg+="start()--->";
setForeground(Color.white);
public void paint(Graphics g)
msg+="paint()--->";
g.drawString(msg,200,50);
2. Create AppLifeCycle.html file
Notes By – Rajan Shukla. 11
Java Programming (22412)
<html>
hj<body>
<applet code="AppLifeCycle.class" width="500" height="100">
</applet>
</body>
</html>
Example – Create Applet to display parameter value
1. Create AppParam.java file
Import java.awt.*;
Import java.applet.*;
public class AppParam extends Applet
{
String name;
public void init()
{
name=getParameter("n1");
}
public void paint(Graphics g)
{
g.drawString(name,50,50);
}
}
2. Create AppParam.html file
<html>
<body>
<applet code="AppParam.class" width="300" height="300">
Notes By – Rajan Shukla. 12
Java Programming (22412)
<param name="n1" value="svcp">
</applet>
</body>
</html>
Output –
Example –
Import java.awt.*;
Import java.applet.*;
public class AppParam extends Applet
String name;
public void init()
name=getParameter("n1");
public void paint(Graphics g)
g.drawString("Hello " + name,50,50);
Notes By – Rajan Shukla. 13
Java Programming (22412)
Example – Create an applet to accept user name as parameter & display number of characters.
(Summer-10)
1. Create strlen.java file
Import java.awt.*;
Import java.applet.*;
public class strlen extends Applet
{
String s;
int c;
public void init()
{
s=getParameter("name");
int i;
i=s.length();
c=i;
}
public void paint(Graphics g)
{
g.drawString(s,50,50);
g.drawString("Length = " + c,50,70);
}
}
2. Create strlen.html file
<html>
<body>
<applet code="strlen.class" width="300" height="300">
<param name="name" value="shital">
</applet>
</body>
Notes By – Rajan Shukla. 14
Java Programming (22412)
</html>
Output –
Update() & repaint() method
repaint()-
Call repaint( ) when you have changed something and want your changes to show up on
the screen
repaint( ) is a request—it will paint entire window
When you call repaint( ), Java schedules a call to update(Graphics g)
update()-
⚫ This method is called when applet request to redraw a portion of window.
⚫ When you call repaint( ), Java schedules a call to update(Graphics g)
⚫ Here's what update does:
public void update()
{
//redraw a window
}
Public void paint(Graphics g)
{
Update();
}
Program – set background, foreground colour & Font
Applet Code –
Notes By – Rajan Shukla. 15
Java Programming (22412)
import java.awt.*;
import java.applet.*;
public class bgcolor extends Applet
public void paint(Graphics g)
setBackground(Color.pink);
setForeground(Color.blue);
setFont(new Font("Comic Sans MS",Font.BOLD,24));
g.drawString("Hello",50,50);
HTML Code -
<html>
<head>
<title> Concentric Circle </title>
</head>
<body>
<applet code="bgcolor.class" width=200 height=200>
</applet>
</body>
</html>
Notes By – Rajan Shukla. 16
Java Programming (22412)
Program – To draw concentric circle(without loop)
Applet File -
import java.awt.*;
import java.applet.*;
public class concircle extends Applet
public void paint(Graphics g)
g.setColor(Color.red);
g.drawOval(50,50,150,150);
g.drawOval(75,75,100,100);
g.drawOval(100,100,50,50);
HTML File –
<html>
<head>
Notes By – Rajan Shukla. 17
Java Programming (22412)
<title> Concentric Circle </title>
</head>
<body>
<applet code="concircle.class" width=100 height=100>
</applet>
</body>
</html>
Program – To draw concentric circle using Loop
Applet File -
import java.awt.*;
import java.applet.*;
public class concircle extends Applet
public void paint(Graphics g)
int w = 150;
int h = 150;
int r = Math.min(w, h)/8;
int cx = w/2;
int cy = h/2;
g.setColor(Color.red);
for(int i = 0; i < 4; i++)
int d = (i+1)*r;
Notes By – Rajan Shukla. 18
Java Programming (22412)
int x = cx - d/2;
int y = cy - d/2;
g.drawOval(x, y, d, d);
HTML File –
<html>
<head>
<title> Concentric Circle </title>
</head>
<body>
<applet code="concircle.class" width=100 height=100>
</applet>
</body>
</html>
Notes By – Rajan Shukla. 19
Java Programming (22412)
Program – To draw Polygon
Applet Code –
import java.applet.*;
import java.awt.*;
public class poly extends Applet {
public void paint (Graphics g) {
int[] XArray = {10,170,80,10};
int[] YArray = {20,40,140,20};
g.drawPolygon (XArray, YArray, 4);
HTML Code –
<html>
<body>
<applet code="poly.class" width="500" height="500">
</applet>
</body>
</html>
Output –
Notes By – Rajan Shukla. 20
Java Programming (22412)
Program – To draw Polygon & fill it
Applet Code –
import java.applet.*;
import java.awt.*;
public class poly extends Applet {
public void paint (Graphics g) {
int[] XArray = {10,170,80,10};
int[] YArray = {20,40,140,20};
g.fillPolygon (XArray, YArray, 4);
HTML Code –
<html>
<body>
<applet code="poly.class" width="500" height="500">
</applet>
</body>
</html>
Notes By – Rajan Shukla. 21
Java Programming (22412)
Output -
Program – To draw Horizontal Bar Chart for following sales
Name HDD Camera Laptop RAM DVD
Sale 55 80 60 25 70
Applet File -
import java.awt.*;
import java.applet.*;
public class barchart extends Applet
public void paint(Graphics g)
g.setColor(Color.blue);
int h=Integer.parseInt(getParameter("hdd"));
int c=Integer.parseInt(getParameter("camera"));
int l=Integer.parseInt(getParameter("laptop"));
int r=Integer.parseInt(getParameter("ram"));
Notes By – Rajan Shukla. 22
Java Programming (22412)
int d=Integer.parseInt(getParameter("dvd"));
g.drawLine(100,100,100,550);
g.drawLine(100,550,600,550);
g.fillRect(100,450,(h*4),50);
g.fillRect(100,350,(c*4),50);
g.fillRect(100,250,(l*4),50);
g.fillRect(100,150,(r*4),50);
g.fillRect(100,50,(d*4),50);
HTML File –
<html>
<head>
<title> Concentric Circle </title>
</head>
<body>
<applet code="barchart.class" width=800 height=600>
<PARAM NAME=hdd VALUE="55">
<PARAM NAME=camera VALUE="80">
<PARAM NAME=laptop VALUE="60">
<PARAM NAME=ram VALUE="25">
<PARAM NAME=dvd VALUE="70">
</applet>
</body>
</html>
Notes By – Rajan Shukla. 23
Java Programming (22412)
Program – To draw Horizontal Bar Chart for following sales
Year 1991 1992 1993 1994
Turnover 110 150 100 170
Applet Code –
import java.awt.*;
import java.applet.*;
public class year extends Applet
public void paint(Graphics g)
//n=Interger.parseInt(getParameter("columns");
//label = new String[n];
String n1,n2,n3,n4;
Notes By – Rajan Shukla. 24
Java Programming (22412)
n1=getParameter("l1");
n2=getParameter("l2");
n3=getParameter("l3");
n4=getParameter("l4");
int y1=Integer.parseInt(getParameter("y91"));
int y2=Integer.parseInt(getParameter("y92"));
int y3=Integer.parseInt(getParameter("y93"));
int y4=Integer.parseInt(getParameter("y94"));
setBackground(Color.pink);
g.setColor(Color.blue);
g.drawLine(100,100,100,550);
g.drawLine(100,550,600,550);
g.drawString(n1,70,480);
g.fillRect(100,450,(y1*4),50);
g.drawString(n2,70,380);
g.fillRect(100,350,(y2*4),50);
g.drawString(n3,70,280);
Notes By – Rajan Shukla. 25
Java Programming (22412)
g.fillRect(100,250,(y3*4),50);
g.drawString(n4,70,180);
g.fillRect(100,150,(y4*4),50);
HTML Code –
<html>
<head>
<title> Concentric Circle </title>
</head>
<body>
<applet code="year.class" width=800 height=600>
<PARAM NAME=y91 VALUE="110">
<PARAM NAME=y92 VALUE="150">
<PARAM NAME=y93 VALUE="100">
<PARAM NAME=y94 VALUE="170">
<PARAM NAME=l1 VALUE="1991">
<PARAM NAME=l2 VALUE="1992">
<PARAM NAME=l3 VALUE="1993">
<PARAM NAME=l4 VALUE="1994">
<PARAM NAME=columns VALUE="4">
</applet>
Notes By – Rajan Shukla. 26
Java Programming (22412)
</body>
</html>
Font Class in Applet
Syntax for Setting Font
To draw text to the screen, first you need to create an instance of the Font class.
Font objects represent an individual font-that is, its name, style (bold, italic), and point
size.
o Font names are strings representing the family of the font, for
example, "TimesRoman", "Courier", or "Helvetica".
o Font styles are constants defined by the Font class; you can get to them using
class variables-for example, Font.PLAIN, Font.BOLD, or Font.ITALIC.
o Finally, the point size is the size of the font, as defined by the font itself; the point
size may or may not be the height of the characters.
Example - Font f = new Font("TimesRoman", Font.BOLD, 24);
setFont(f);
import java.awt.*;
import java.applet.*;
Notes By – Rajan Shukla. 27
Java Programming (22412)
public class myfont extends Applet
public void paint(Graphics g)
Font myFont = new Font("Courier", Font.PLAIN,20);
g.setFont(myFont);
g.drawString("Create Font Example", 10, 50);
Methods
getFamily()
getFont()
getFontName()
getSize()
getStyle()
getAllFonts()
Getavailablefontfamilyname()
import java.awt.*;
Notes By – Rajan Shukla. 28
Java Programming (22412)
import java.applet.*;
public class myfont extends Applet
public void init()
Font f= new Font("Arial",Font.BOLD,40);
setFont(f);
System.out.println("Family ="+f.getFamily());
System.out.println("Font Name ="+f.getFontName());
System.out.println("Size ="+f.getSize());
System.out.println("Style ="+f.getStyle());
public void paint(Graphics g)
g.drawString("Welcome",50,50);
System.out.println("Font ="+g.getFont());
/*
<applet code="myfont.class" height=200 width=200>
</applet>
*/
Notes By – Rajan Shukla. 29
Java Programming (22412)
getAllFonts() -
import java.awt.*;
import java.applet.*;
public class myallfont extends Applet
public void paint(Graphics g)
Font[] f = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (int i = 0; i < f.length; i++)
Notes By – Rajan Shukla. 30
Java Programming (22412)
System.out.print(f[i].getFamily() + " : ");
System.out.println();
/*
<applet code="myallfont" height=200 width=200>
</applet>
*/
getAvailableFontFamilyNames() -
import java.awt.*;
import java.applet.*;
public class myallavl extends Applet
Notes By – Rajan Shukla. 31
Java Programming (22412)
public void paint(Graphics g)
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] f = e.getAvailableFontFamilyNames();
System.out.println("\nFonts available on this platform: ");
for (int i = 0; i < f.length; i++)
System.out.println(f[i]);
/*
<applet code="myallavl" height=200 width=200>
</applet>
*/
Notes By – Rajan Shukla. 32