Unit-5 Java Applets & Graphics Programming (20 Marks)
Unit-5 Java Applets & Graphics Programming (20 Marks)
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
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():-
Hello.class.
Step 2:Design a webpage.
Design a webpage using HTML text & incorporate
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
{
/* <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() :-
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 :-
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 :-
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 :-
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 :-
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.
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 :-
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);
}
}
}
/* <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