0% found this document useful (0 votes)
20 views96 pages

Unit-5 Java Applets & Graphics Programming (20 Marks)

Uploaded by

harshgbirje
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
20 views96 pages

Unit-5 Java Applets & Graphics Programming (20 Marks)

Uploaded by

harshgbirje
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 96

JAVA APPLETS & GRAPHICS

PROGRAMMING(10 MARKS)
JAVA APPLETS
WHAT IS AN APPLET?
 An applet is small programs that are primarily used in
Internet Computing.
 An applet is small application that is access on an
internet server, transported over the internet,
automatically installed and run as part of a web
document.
 Applets can run on any web browser that supports java
(like Microsoft Internet Explorer, Netscape Navigator
and Sun’s HotJava) or by using appletviewer tool which
is included with jdk.
 Applet can be used to perform arithmetic operations,
display graphics, play sound, create animation and play
interactive games.
 To create an applet, we need to import the package such
as java.applet and java.awt & text
Click to add extend the user class with
Applet class.
 java.applet package provides the Applet class that is
used to create Applet.
 java.awt provides component class which develops
GUI components such as button, check box. It also
has paint () method that allows to print string in an
applet.
TYPES OF APPLET.
 Following are the types of applets:-
 Local Applets
 Remote Applets
 Local Applets:-
 An applet developed locally & stored in a local

system is known as local Applet.


 When web page is trying to find Local Applet, it does
not need internet connection. It simply searches
directories in local system & loads it.
 Remote Applets:-
 An applet which is developed by someone else &

stored on a remote computer connected to the


Internet.
 But in order to load, we must know the applet address
on the web known as URL & must be specified in the
applet HTML document as the value of codebase
attribute.
DIFFERENTIATE BETWEEN JAVA APPLET & JAVA
APPLICATION.
APPLET LIFE CYCLE
 When Applet is loaded, it undergoes a series of changes
in its state. The states are as follows:-
 Born State or Initialization state
 Running State
 Idle State or Stopped State
 Dead State
 Born State or Initialization State
 When applet is firstly loaded, it enters into born stage.
 This initialization stage occurs only once in a life cycle
by calling init () method of applet class.
 At this stage, we may create object needed by applet,
setup variable and its initial value, set up font & color,
etc.
 Syntax of init():-

public void init ()


{
//……action;
}
 Running Stage:-
 Applets enter the running state when the system calls

start () method of Applet class.


 This occurs automatically after the applet is
initialized.
 Starting can also occurs if the applet is in idle state.
This method may be called more than once.
 Syntax of start():-

public void start ()


{
//body
}
 Idle State or Stop State:-
 An applet becomes idle when it is stopped from

running. Stopping occurs automatically when we


leave the page containing currently running applet as
by calling stop () method explicitly.
 Syntax of stop():-

public void stop ()


{
//body
}
 Dead State
 This counterpart to the initialization stage & occurs when
the system is about to remove the applet from memory.
 If the applet has resources that need to be cleaned up
before the applet exits, then it is done in by overriding the
Applet class destroy() method.
 This method occurs only once.

 Syntax of destroy():-
public void destroy ()
{
//body
}
 Display State:-
 An applet moves to display state whenever it has to
perform some output operation on the screen. This
task is achieve by using paint () method.
 Syntax of paint():-

public void paint (Graphics g)


{
//statements;
}
SKELETON OF APPLET
 When applet starts running, it overrides life cycle
method such as init (), start (), stop () & destroy ().
 This methods provides the mechanism by which the
browser or appletviewer interfaces to the applet &
controls its execution.
 The methods such as init (), start (), stop () & destroy ()
are define by applet class and paint () method is defined
by awt class components.
import java.applet.*;
import java.awt.*;
public class AppletSkeleton extends Applet
{
public void init () // Called First.
{
// Initialization
}
public void start ()
{
// Called Second after init () method. Also
// Start or Resume Execution called whenever applet is
restart.
}
public void stop ()
{
// Called when applet is stop from running.
// Suspend Execution
}
public void destroy ()
{
// Called when applet is terminated.
// Shutdown Applet Activities
}
public void paint (Graphics g)
{
// Called when applet window must be
// Display contain of Window restored.
}
}
<APPLET> TAG
<APPLET
CODE=applet_file.class
[CODEBASE=URL of applet code]
WIDTH = pixel
HEIGHT=pixel
[ALIGN=Alignment]
[HSPACE=pixel]
[VSPACE=pixel]
[ALT=alternet text]
>
[<PARAM NAME=”name1” VALUE=”value1”>]
[<PARAM NAME=”name2” VALUE=”value2”>]
……………
……………
</APPLET>
STEPS TO CREATE & EXECUTE AN
APPLET
Step 1: Building an Applet Code.
 Open the notepad & type the applet code & save this
file with java extension.
Ex: Hello.java
import java.applet.*;
import java.awt.*;
public class Hello extends Applet
{
public void paint (Graphics g)
{
g.drawString (“Welcome to
Java”, 100,200);
}
}
 Compile the applet code by using java compiler.
Ex: - javac Hello.java
 After compilation, it create an executable applet i.e.

Hello.class.
Step 2:Design a webpage.
 Design a webpage using HTML text & incorporate

<APPLET> tag into the webpage.


<HTML>
<HEAD>
<TITLE>Java Program</TITLE>
</HEAD>
<BODY>
<APPLET CODE=”Hello.class”
WIDTH=300 HEIGHT=300>
</APPLET>
</BODY>
</HTML>
 Save this file with HTML extension i.e. Hello.html in the
same directory.
Step 3:Run an applet.
 Through Web Browser :-

To run an applet, open Hello.html through any java


enabled web browser.
 Through AppletViewer:-

Applet can run by using appletviewer i.e. part of jdk.


Appletviewer is a window application run from command
prompt. When appletviewer appears the applet appears
viewer’s name window.
Ex: - C:\>appletviewer Hello.java
import java.applet.*;
import java.awt.*;
public class Hello extends
Applet
{
public void paint(Graphics g) to add text
Click
{
g.drawString("java",10,100);
}
}
/* <applet code="Hello.class"
width=500 height=500>
</applet> */
import java.applet.*; <html>
import java.awt.*; <body>
public class Hello5 extends
Applet
<applet
{ code="Hello5.class"
public void paint(Graphics g) width=500 height=500>
{ </applet>
g.drawString("java",10,100); </body>
} </html>
}
import java.awt.*; public void stop()
import java.applet.*;
{
/* System.out.println("Stopping an
<applet code="Applet1.class" width=400 applet");
height=200> }
</applet>
*/
public void destroy()
public class Applet1 extends Applet {
{ System.out.println("Destroying an
applet");
public void init()
{
}
System.out.println("Initializing an applet"); public void paint(Graphics g)
} {
g.drawString("Welcome",50,30);
public void start()
{
}
System.out.println("Starting an applet");
} }
<PARAM> TAG
 An <APPLET> tag in a HTML document allow to pass
parameter to the applet using <PARAM> tag.
 Each <PARAM> tag has attribute NAME & VALUE.

 Syntax:-

<APPLET CODE=”Hello.class”
WIDTH=300 HEIGHT=300>
<PARAM NAME=”name”
VALUE=”value”>
</APPLET>
import java.applet.*;
import java.awt.*;
public class paramtag extends Applet
{
String str;
public void init ()
{
str=getParameter (“name”);
}
public void paint (Graphics g)
{
g.drawString (str, 100,100);
}
}
 Passing parameter to an applet code using <PARAM>
tag is similar to passing parameters to the main ()
method using command line arguments.
 When an applet is loaded, parameters are pass through
an applet. So define init () method in the applet to get
hold of parameters define in the <PARAM> tag which is
done by using getParameter() method.
 getPararmeter() method takes one string argument
representing name of the parameter & return string
containing value of that parameter.
 Example:-
String s=getParameter (“name”);
/*
<APPLET CODE=”paramtag.class” WIDTH=300
HEIGHT=300>
<PARAM NAME=”name” VALUE=”Welcome
to Java”>
</APPLET>
*/
import java.applet.*;
import java.awt.*;
public class FirstAppletPara extends Applet
{
String str;
public void init()
{
str=getParameter("Name");
}
public void paint(Graphics g)
{
g.drawString(str,50,30);
}
}
/* <applet code="FirstAppletPara.class"
width=500 height=500>
<param name="Name" value="Suwarna">
</applet> */
import java.applet.*;
/* <applet
import java.awt.*;
public class FirstAppletParaAdd extends code="FirstAppletParaAdd.
Applet class" width=500
{
String str1,str2;
height=500>
public void init() <param name="a"
{
str1=getParameter("a");
value="5">
str2=getParameter("b"); <param name="b"
}
public void paint(Graphics g)
value="4">
{ </applet> */
int
c=Integer.parseInt(str1)+Integer.parseInt(str2)
;
String s="sum="+String.valueOf(c);
g.drawString(s,50,30);
}
}
import java.applet.*;
import java.awt.*;
public class AppletBal extends Applet
{
String str1;
public void init()
{
str1=getParameter("bal");
}
public void paint(Graphics g)
{
int bal=Integer.parseInt(str1);
if(bal<500)
g.drawString("low balance",50,30);
else
g.drawString("sufficient balance",50,30);
}
}
/* <applet code="AppletBal.class" width=500
height=500>
<param name="bal" value="600">
</applet> */
import java.applet.*;
import java.awt.*;
public class AppletLen extends Applet
{

public void paint(Graphics g)


{
String str1=this.getParameter("Uname");
int len;
len=str1.length();
String msg="Length Suwarna="+len;
g.drawString(msg,50,30);
}
}
/* <applet code="AppletLen.class" width=500
height=500>
<param name="Uname" value="Suwarna">
</applet> */
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class AppletButton extends Applet
{

Button b1=new Button("submit");


public void init()
{
add(b1);
}
}

/* <applet code="AppletButton.class"
width=500 height=500>
</applet> */
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class AppletCheckBox extends Applet
{
Checkbox cb1=new Checkbox("adhar",true);
Checkbox cb2=new Checkbox("pan",false);
public void init()
{
add(cb1);
add(cb2);
}
}

/* <applet code="AppletCheckBox.class"
width=500 height=500>
</applet> */
GRAPHICS
PROGRAMMING
METHODS OF GRAPHIC CLASS
I] drawLine() :-

 To draw a Line, drawLine() method is used.


 The drawLine() takes two pairs of coordinates
(x1,y1) & (x2,y2) & draws the line between them.
 Syntax:-

drawLine(int x1,int y1,int x2,int y2);


or
drawLine(x1,y1,x2,y2);
 Ex:- g.drawLine(10,10,50,50);
import java.applet.*;
import java.awt.*;
public class AppletLine extends
Applet
{
public void paint(Graphics g)
{
g.drawLine(0,0,200,100);
g.drawString("Line Demo",50,80);
}
}
/* <applet code="AppletLine.class"
width=500 height=500>
</applet> */
II] drawRect() :-
 drawRect() method is used to draw rectangle & square.
 It takes four arguments: first two represents top left corner of
rectangle & remaining two represents the width & height of
rectangle in pixels.
 To draw square. We have to put the same value for width &
height.

 Syntax :-
drawRect(int top,int left,int width,int height);
or
drawRect(x,y,w,h);
 Ex :- g.drawRect(10,60,40,30);
import java.applet.*;
import java.awt.*;
public class AppletRect extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10,10,50,50);
g.drawRoundRect(70,30,50,30,10,10);
g.fillRect(40,100,150,100);
g.fillRoundRect(200,10,70,100,10,10);
g.drawString("Rect Demo",30,90);
}
}
/* <applet code="AppletRect.class"
width=500 height=500>
</applet> */
III] fillRect() :-
 This method is used to draw solid rectangle & square.
 It takes four arguments: first two represents top left corner
of rectangle & remaining two represents the width &
height of rectangle in pixels.
 To draw a solid (filled with color) square. We have to put
the same value for width & height.
 Syntax :-

fillRect(int top,int left,int width,int height);


or
fillRect(x,y,w,h);

 Ex :- g.fillRect(10,60,40,30);
IV] drawRoundRect() :-
 This method is used to draw rounded corner rectangle & square.
 It takes six arguments : first two represents the top left corner of
the rectangle, after that two argument represents the width &
height of rectangle in pixels, remaining two represents the
diameter of rounding corner along x-axis & y-axis.
 To draw rounded corner square. We have to put the same value
for width & height.
 Syntax :-

drawRoundRect(int top,int left,int width,int


height,int x_diameter,int y_diameter);
or
drawRoundRect(x,y,w,h,x_diameter,y_diameter);
 Ex :- g.drawRoundRect(10,60,40,30,5,5);
V] fillRoundRect() :-
 This method is used to draw solid rounded corner rectangle &
square.
 It takes six arguments: first two represents the top left corner of
the rectangle, after that two argument represents the width &
height of rectangle in pixels, remaining two represents the
diameter of rounding corner along x-axis & y-axis.
 To draw a solid (filled with color) rounded corner square. We
have to put the same value for width & height.
 Syntax :-

fillRoundRect(int top,int left,int width,int


height,int x_diameter,int y_diameter);
or
fillRoundRect(x,y,w,h,x_diameter,y_diameter);
 Ex :- g.fillRoundRect(10,60,40,30,5,5);
VI] drawOval() :-
 This method is used to draw circle & ellipse or oval.
 It takes the four argument: first two represents the top
left corner of the imaginary rectangle and the other
two represents the width & height of the oval itself.
 If the width and height are the same, then the oval
becomes a circle.
 Syntax :-

drawOval(int top,int left,int width,int height);


or
drawOval(x,y,w,h);
 Ex :- g.drawOval(10,60,40,30);
import java.applet.*;
import java.awt.*;
public class AppletOval extends Applet
{
public void paint(Graphics g)
{
g.drawOval(10,10,50,50);
g.fillOval(200,10,70,100);

g.drawString("Oval Demo",30,90);
}
}
/* <applet code="AppletOval.class"
width=500 height=500>
</applet> */
import java.applet.*;
import java.awt.*;
public class AppletCircle extends Applet
{
public void paint(Graphics g)
{
g.drawOval(50,50,100,100);
g.drawOval(40,40,120,120);
g.drawOval(30,30,140,140);

g.drawString("Circle Demo",30,90);
}
}
/* <applet code="AppletCircle.class"
width=500 height=500>
</applet> */
VII] fillOval() :-
 This method is used to draw solid (filled with color) circle
& ellipse or oval.
 It takes the four argument: first two represents the top left
corner of the imaginary rectangle and the other two
represents the width & height of the oval itself.
 If the width and height are the same, then the oval
becomes a solid circle.
 Syntax :-

fillOval(int top,int left,int width,int height);


or
fillOval(x,y,w,h);
 Ex :- g.fillOval(10,60,40,30);
import java.applet.*;
import java.awt.*;
public class AppletColor extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.ORANGE);
g.fillRect(10,10,50,50);
g.setColor(Color.RED);
g.fillRect(10,50,50,50);
g.setColor(Color.GREEN);
g.fillRect(10,90,50,50);

g.drawString("Color Demo",30,90);
}
}
/* <applet code="AppletColor.class"
width=500 height=500>
</applet> */
VIII] drawArc() :-
 This method is used to draw arc.
 It takes the six argument: first two represents the top left corner of
the imaginary rectangle and the third & fourth represents the width
& height of the oval itself & last two represents the starting angle of
the arc and the number of degrees (sweep angle) around arc.
 Syntax :-

drawArc(int top,int left,int width,int height,int


startingangle,int sweepingangle);
or
drawArc(x,y,w,h);
When sweeping angle is negative then it draws arc in a clockwise
direction. And when sweeping angle is positive then it draws arc
in anticlockwise direction.
 Ex :- g.drawArc(10,60,40,30,0,180);
IX] fillArc() :-
 This method is used to draw solid (filled with color) arc.
 It takes the six argument: first two represents the top left
corner of the imaginary rectangle and the third & fourth
represents the width & height of the oval itself & last two
represents the starting angle of the arc and the number of
degrees (sweep angle) around arc.
 Syntax :-

fillArc(int top,int left,int width,int height,int


startingangle,int sweepingangle);
or
fillArc(x,y,w,h);
 Ex :- g.fillArc(10,60,40,30,0,180);
X] drawPolygon() :-
 To draw polygon, the drawPolygon() method is used,
which takes three parameter as follows:
 An array of integers containing x coordinates.
 An array of integers containing y coordinates.

 An integer of total no of points.

 Syntax :-
drawPolygon(int x[ ],int y[ ],int
no_of_points);
 Ex :- drawPolygon(xp,yp,np);
import java.applet.*;
import java.awt.*;
public class AppletArc extends Applet
{
public void paint(Graphics g)
{
g.drawArc(100,60,100,100,0,90);
g.setColor(Color.green);
g.fillArc(100,60,55,70,0,90);

g.drawString("Arc Demo",30,90);
}
}
/* <applet code="AppletArc.class"
width=500 height=500>
</applet> */
XI] fillPolygon() :-
 To draw solid (filled with color) polygon, the
fillPolygon() method is used, which takes three
parameter as follows:
 An array of integers containing x coordinates.
 An array of integers containing y coordinates.

 An integer of total no of points.

 Syntax :-
fillPolygon(int x[ ],int y[ ],int no_of_points);
 Ex :- fillPolygon(xp,yp,np);
EXPLAIN DRAWING POLYGON WITH
SUITABLE EXAMPLE
 A polygon is a closed path or circuit which is made by
joining line segments.
 In polygon, the end of the first line is connected with the
beginning of second line, the end of the second is the
beginning of the third and so on.
 It may be consider as set of lines connected together.
 We can draw polygon of n sides using drawLine()
method as follows:
public void paint(Graphics g)
{
g.drawLine(10,20,100,200);
g.drawLine(100,200,50,60);
g.drawLine(50,60,10,20);
}
 We can also use addPoint() method to draw polygon using polygon
objects.
 Syntax :-

 Polyobj.addPoint(x,y);

 Ex :-

public void paint(Graphics g)


{
Polygon p=new polygon();
p.addPoint(20,20);
p.addPoint(120,120);
p.addPoint(220,220);
g.drawPolygon(p);
}
 We can also draw polygon by using drawPolygon() or
fillPolygon() method, which takes three parameter as follows:
 An array of integers containing x coordinates.
 An array of integers containing y coordinates.
 An integer of total no of points.
 Ex :-

public void paint(Graphics g)


{
int x[ ]={10,170,80,10};
int x[ ]={10,170,80,10};
int n= x.length;
g.drawPolygon(x,y,n);
}
import java.applet.*; g.drawString("Polygon
import java.awt.*; Demo",300,300);
public class AppletPoly extends
Applet }
{ }
public void paint(Graphics g)
/* <applet
{
code="AppletPoly.class"
int xpt[]={50,20,20,20,130};
int ypt[]={80,30,200,200,30};
width=500 height=500>
int num=5; </applet> */
g.drawPolygon(xpt,ypt,num);
g.setColor(Color.magenta);
g.fillPolygon(xpt,ypt,num);
g.setColor(Color.black);
import java.applet.*;
import java.awt.*;
public class AppletTri extends Applet
{
public void paint(Graphics g)
{
int xpt[]={20,100,60};
int ypt[]={60,60,10};
int num=3;
g.drawPolygon(xpt,ypt,num);
g.setColor(Color.red);
g.fillPolygon(xpt,ypt,num);
g.drawString("Triangle Demo",300,300);
}
}
/* <applet code="AppletTri.class"
width=500 height=500>
</applet> */
import java.applet.*;
import java.awt.*;
public class AppletSmiley extends Applet
{
public void paint(Graphics g)
{
g.drawOval(20,40,250,250);
g.drawOval(70,100,50,50);
g.drawOval(180,100,50,50);
g.drawArc(100,150,100,100,180,180);

g.drawString("Smiley Demo",300,300);
}
}
/* <applet code="AppletSmiley.class"
width=500 height=500>
</applet> */
import java.applet.*;
import java.awt.*;
public class AppletGround extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.cyan);
setForeground(Color.red);
g.drawString("Applet",50,30);
Color newC=new Color(255,255,7);
g.setColor(newC);
g.drawString("Applet",50,70);

}
}
/* <applet code="AppletGround.class"
width=500 height=500>
</applet> */
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class AppleTextField extends
Applet
{
TextField tf=new TextField("suwarna");
public void init()
{
add(tf);
}
}

/* <applet code="AppleTextField.class"
width=500 height=500>
</applet> */
import java.applet.*;
import java.awt.*;
public class AppletFont extends Applet
{
public void paint(Graphics g)
{
Font f=new Font("Monotype
Corsiva",Font.BOLD,40);
g.setFont(f);

g.drawString("Applet",50,70);

}
}
/* <applet code="AppletFont.class"
width=500 height=500>
</applet> */
import java.applet.*; g.drawString(msg,30,70);
import java.awt.*; int size=f.getSize();
public class AppletFont1 extends Applet
msg="Size:"+size;
{
g.drawString(msg,30,90);
String msg="";
Font f;
int style=f.getStyle();
public void init() if((style & Font.PLAIN)==Font.PLAIN)
{ msg="Font Style:PLAIN";
f=new Font("ARIAL",Font.BOLD,12); if((style & Font.BOLD)==Font.BOLD)
setFont(f); msg="Font Style:BOLD";
} if((style & Font.ITALIC)==Font.ITALIC)
msg="Font Style:ITALIC";
public void paint(Graphics g)
g.drawString(msg,30,110);
{
f=g.getFont();
String name=f.getName(); }
msg="Font Name:"+name; }
g.drawString(msg,30,50); /* <applet code="AppletFont1.class"
String family=f.getFamily(); width=500 height=500>
msg="Font Family:"+family; </applet> */
import java.applet.*;
import java.awt.*;
public class AppletGround1 extends
Applet
{
public void paint(Graphics g)
{
setBackground(Color.red);
setForeground(Color.blue);
g.drawString("Applet",50,30);

}
}
/* <applet code="AppletGround1.class"
width=500 height=500>
</applet> */
/*WAP TO DISPLAY A STRING "CONCENTRIC CIRCLES" USING FONT
'ARIAL' SIZE AS 12 AND STYLE AS BOLD + ITALIC AND DISPLAY
THREE CONCENTRIC CIRCLES WITH DIFFERENT COLORS ON THE
APPLET.*/
import java.applet.*;
import java.awt.*;
public class AppletFontCon extends Applet
{
Font f;
public void init()
{
f=new Font("ARIAL",Font.BOLD+Font.ITALIC,12);
setFont(f);
}

public void paint(Graphics g)


{
f=g.getFont();
g.drawString("Concentric Circle",50,20);
g.setColor(Color.RED);
g.drawOval(50,50,100,100);
g.setColor(Color.BLUE);
g.drawOval(40,40,120,120);
g.setColor(Color.GREEN);
g.drawOval(30,30,140,140);

}
}
/* <applet code="AppletFontCon.class" width=500 height=500>
</applet> */
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class AppletRadioButton extends Applet
{
CheckboxGroup cbg=new CheckboxGroup();
Checkbox cb1=new Checkbox("Male",cbg,false);
Checkbox cb2=new
Checkbox("Female",cbg,true);
public void init()
{
add(cb1);
add(cb2);
}
}

/* <applet code="AppletRadioButton.class"
width=500 height=500>
</applet> */
END

You might also like