Applet program lab practice
Commonly used methods of Graphics class for applet program:
❖ public void drawString(String str, int x, int y): is used to draw the specified string.
❖ public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width
and height.
❖ public void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default
color and specified width and height.
❖ public void drawOval(int x, int y, int width, int height): is used to draw oval with the specified
width and height.
❖ public void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and
specified width and height.
❖ public void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1)
and (x2, y2).
❖ public boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the
specified image.
❖ public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is
used draw a circular or elliptical arc.
❖ public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is
used to fill a circular or elliptical arc.
❖ public void setColor(Color c): is used to set the graphics current color to the specified color.
❖ public void setFont(Font font): is used to set the graphics current font to the specified font.
1. Simple applet program to display text on screen
import [Link];
import [Link];
public class First extends Applet{
public void paint(Graphics g){
[Link]([Link]);
[Link]("welcome to applet",150,150);
} }
2. Applet program to draw the rectangle and line
import [Link].*;
import [Link].*;
public class LineRect extends Applet {
public void paint(Graphics g) {
[Link](“Draw line and rectangle”,30,30);
[Link](40,40,60,50);
[Link](40,60,40,50);
} }
OUTPUT:
3. Applet program to drawing fill rectangle and draw Round Rectangle
import [Link].*;
import [Link].*;
public class LineRect extends Applet {
public void paint(Graphics g) {
[Link](60,10,30,80);
[Link](10,100,80,50,10,10);
[Link](20,110,60,30,5,5);
}
}
OUTPUT:
4. Applet program to draw oval and fill oval by default or set color and also to draw arc and fill arc
by default or set color.
import [Link].*;
import [Link].*;
public class lab1 extends Applet
{
public void paint(Graphics g){
[Link](30,100,30,30);
[Link]([Link]);
[Link](170,200,30,30);
[Link](90,150,30,30,30,270);
[Link](270,150,30,30,0,180);
}
}}
OUTPUT:
5. Applet program to draw happy face
import [Link].*;
import [Link].*;
public class NewClass extends Applet
{
public void paint(Graphics g)
{
[Link](40,40,120,150); //Head
[Link](57,75,30,20); //Left eye
[Link](110,75,30,20); //Right eye
[Link](68,81,10,10); //Pupil (left)
[Link](121,81,10,10); //Pupil (right)
[Link](85,100,30,30); //Nose
[Link](60,125,80,40,180,180); //Mouth
[Link](25,92,15,30); //Left ear
[Link](160,92,15,30); //Right ear
}}
Output of question number 4.
6. Applet program to draw poygon
Polygons are shapes with many sides. A polygons may be defined as a set of connected lines. The end of
first line is the beginning of second line, and so on,
Syntax:
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
Draws a closed polygon defined by arrays of x and y coordinates.
import [Link].*;
import [Link].*;
public class NewClass 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);
}}
OUTPUT:
T
import [Link].*;
import [Link].*;
public class LineRect extends Applet
{
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);
}
}