Introduction To Applet with
Graphics Programming
Java Applet
• 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.
6
We can download applet into web pages in 2 ways
[Link] our own applet and embed the into web pages .
[Link] an applet from a remote computer system and then
embed it into web page
• Local applet-
• An applet developed locally and stored in local system is known as local
applet.
• [Link] a web page trying to find a local applet ,it doesn't need to use
internet and therefore the local system doesn’t requires the internet
connection .It simply searches the directories in local system and locates and
load the specified applet it into a web page.
• Remote applet-
• An applet is that developed by someone else and stored on remote
computer connected to internet .
• IF your system connected to internet we can download the remote applet
onto our system via at the internet and run it.
Lifecycle of Java Apple
12
Applet Initialization and Termination
• It is important to understand the order in which the various
methods shown in the skeleton are called.
• When an applet begins, the AWT calls the following methods,
in this sequence:
1. init( )-Executed only once
2. start( )-Executed when browser maximized
3. paint( )-Each time
• When an applet is terminated, the following sequence of
method calls takes place:
1. stop( ) -Executed when browser minimized
2. destroy( )-Executed when browser closed
13
• init( )
• The init( ) method is the first method to be called. This is where you
should initialize variables.
• This method is called only once during the run time of your applet.
• start( )
• The start( ) method is called after init( ). It is also called to restart an
applet after it has been stopped.
• Whereas init( ) is called once—the first time an applet is loaded—
• start( ) is called each time an applet’s HTML document is displayed
onscreen.
• So, if a user leaves a web page and comes back, the applet resumes
execution at start( ).
14
An example abstract class
• paint( )
• The paint( ) method is called each time your applet’s
output must be redrawn.
• This situation can occur for several reasons. For example,
the window in which the applet is running may be
overwritten by another window and then uncovered
.window may be minimized and then restored. paint( ) is
also called when the appletbegins execution.
• Whatever the cause, whenever the applet must redraw
its output,paint( ) is called. The paint( ) method has one
parameter of type Graphics. This parameter will contain
the graphics context, which describes the graphics
environment in which the applet is running.
15
• stop( )
• The stop( ) method is called when a web browser leaves the
HTML document containing the applet—when it goes to
another page,
• When stop( ) is called, the applet is probably running. You
should use stop( ) to suspend threads that don’t need to run
when the applet is not visible.
• You can restart them when start( ) is called if the user returns to
the page.
• destroy( )
• The destroy( ) method is called when the environment
determines that your applet needs to be removed completely
from memory.
• At this point, you should free up any resources the applet may
be using. The stop( ) method is always called before destroy( ).
16
How to run an Applet?
• There are two ways to run an applet
• By html file.
• By appletViewer tool (for testing purpose).
C:> javac [Link]
C:>appletviewer [Link]
Example of using parameter in Applet:
import [Link];
import [Link];
public class UseParam extends Applet{
public void paint(Graphics g){
String str=getParameter("msg");
[Link](str,50, 50);
} }
/*
<applet code="[Link]" width="300" height="300
">
<param name="msg" value="Welcome to applet">
</applet>
*/
/*<applet code="[Link]" width=400 try
height=300> {
</applet>*/ str1=[Link]();
import [Link].*;
a=[Link](str1);
import [Link].*;
public class GettingInputfromtheUser extends Applet str2=[Link]();
{ b=[Link](str2);
TextField t1, t2; }
public void init() catch(Exception e)
{ {}
t1 = new TextField(10);
t2 = new TextField(10);
c=a+b;
str=[Link](c);
add(t1); [Link]("Sum is",10,15);
add(t2); [Link](str,100,75);
}
[Link]("0"); public boolean action(Event event, Object object)
[Link]("0"); {
}
public void paint(Graphics g)
repaint();
{ return true;
int a=0,b=0,c=0; }
String str1,str2,str; }
[Link]("Enter the number in each box",10,50);
Graphics Programming
Drawing Lines and Rectangle
import [Link].*;
import [Link].*;
public class LineRect extends Applet F:\Graphics>javac [Link]
{ F:\Graphics>appletviewer [Link]
public void paint(Graphics g)
{
[Link](10,10,50,50);
[Link](10,60,40,30);
[Link](60,10,30,80);
[Link](10,100,80,50,10,10);
[Link](20,110,60,30,5,5);
[Link](100,10,230,140);
[Link](100,140,230,10);
}
}
/*<applet code="[Link]" width="300" heig
ht="300">
</applet> */
import [Link];
import [Link].*;
/*<applet code="[Link]" width="300
" height="300">
</applet> */
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
[Link]([Link]);
[Link]("Welcome",50, 50);
[Link](20,30,20,300);
[Link](70,100,30,30);
[Link](170,100,30,30);
[Link](70,200,30,30);
[Link]([Link]);
[Link](170,200,30,30);
[Link](90,150,30,30,30,270);
[Link](270,150,30,30,0,180);
}
}
Drawing Polygon
import [Link].*;
import [Link].*;
public class Poly extends Applet
{
int x1[]={20,120,220,20};
int y1[]={20,120,20,20};
int n1=4;
int x2[]={120,220,220,120};
int y2[]={120,20,220,120};
int n2=4;
public void paint(Graphics g)
{
[Link](x1,y1,n1);
[Link](x2,y2,n2);
}
}
/*<applet code="[Link]" width="300" height="300">
</applet> */
import [Link].*;
public void paint(Graphics g)
import [Link].*;
{
public class BarChart extends Applet
for(int i=0;i<n;i++)
{
{
int n=0;
[Link]([Link]);
String label[];
[Link](label[i],20,i*50+30);
int value [];
[Link](50, i*50+10, value[i],40);
public void init()
}
{
}
try{
}
n=[Link](getParameter("columns"));
/*<applet code="[Link]" width="300“
label=new String[n];
height="250">
value=new int[n];
<param name="columns" value="4">
label[0]=getParameter("Label1");
<param name="c1" value="110">
label[1]=getParameter("Label2");
<param name="c2" value="150">
label[2]=getParameter("Label3");
<param name="c3" value="100">
label[3]=getParameter("Label4");
<param name="c4" value="170">
value[0]=[Link](getParameter("c1"));
value[1]=[Link](getParameter("c2"));
<param name="Lable1" value="91">
value[2]=[Link](getParameter("c3"));
<param name="Lable2" value="92">
value[3]=[Link](getParameter("c4"));
<param name="Lable3" value="93">
}
<param name="Lable4" value="94">
catch(NumberFormatException e){}
</applet> */
}
Color ------ Red Green Blue
0-255 0-255 0-255
setColor (Object /Static Member)
• By Object
Color c=new Color (0,0,0) Black
(255, 255, 255) White
(255,0,0)Red
(0, 255,0)Green
(0,0, 255)Blue
• By Static Member Constant
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
Example : [Link]([Link])
/*<applet code="[Link]" width=400
height=300>
</applet>*/
import [Link].*;
import [Link].*;
public class Applet1 extends Applet
{
public void init(){
setBackground([Link]);
setForeground([Link]);
}
public void paint(Graphics g)
{
[Link]("Welcome",10,50);
}
}
Font- awt
g. setFont (Object)
Font f= new Font (String str, int, int )
Font face Font style Font size
Time New Roman 0- Plain
Arial 1- Bold
Calibri 2 - Italic
Bookman Old Style 3 -Bold+Itallic