0% found this document useful (0 votes)
19 views

Applet Program Lab Practice

lab practice

Uploaded by

osmytech57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Applet Program Lab Practice

lab practice

Uploaded by

osmytech57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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 java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("welcome to applet",150,150);
} }
2. Applet program to draw the rectangle and line
import java.awt.*;
import java.applet.*;
public class LineRect extends Applet {
public void paint(Graphics g) {
g.drawstring(“Draw line and rectangle”,30,30);
g.drawLine(40,40,60,50);
g.drawRect(40,60,40,50);
} }
OUTPUT:

3. Applet program to drawing fill rectangle and draw Round Rectangle


import java.awt.*;
import java.applet.*;
public class LineRect extends Applet {
public void paint(Graphics g) {
g.fillRect(60,10,30,80);
g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(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 java.awt.*;
import java.applet.*;
public class lab1 extends Applet
{
public void paint(Graphics g){
g.drawOval(30,100,30,30);
g.setColor(Color.red);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}}

OUTPUT:
5. Applet program to draw happy face
import java.awt.*;
import java.applet.*;
public class NewClass extends Applet
{
public void paint(Graphics g)
{
g.drawOval(40,40,120,150); //Head
g.drawOval(57,75,30,20); //Left eye
g.drawOval(110,75,30,20); //Right eye
g.fillOval(68,81,10,10); //Pupil (left)
g.fillOval(121,81,10,10); //Pupil (right)
g.drawOval(85,100,30,30); //Nose
g.fillArc(60,125,80,40,180,180); //Mouth
g.drawOval(25,92,15,30); //Left ear
g.drawOval(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 java.awt.*;
import java.applet.*;
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)
{
g.drawPolygon(x1,y1,n1);
g.fillPolygon(x2,y2,n2);
}}
OUTPUT:

T
import java.awt.*;
import java.applet.*;
public class LineRect extends Applet
{
public void paint(Graphics g)
{
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30);
g.fillRect(60,10,30,80);
g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(20,110,60,30,5,5);
g.drawLine(100,10,230,140);
g.drawLine(100,140,230,10);
}
}

You might also like