This is a Java Program to Create and Fill Shapes using Applet
We have to write a program in Java such that it creates the shapes – Square, Pentagon, Circle, Oval, Rectangle and Triangle and fills color inside the shapes.
For creating and filling shapes, we can have the following set of input and output.
To View the Shapes :
When the program is executed, it is expected that the applet consists of the following shapes Square, Pentagon, Circle, Oval, Rectangle and Triangle And also fills Yellow color inside the shapes.
1. Use method drawPolygon and fillPolygon of the Graphics class to draw and fill the shapes – Square, Pentagon, Rectangle and Triangle.
2. Use method drawOval and fillOval of the Graphics class to draw and fill the shapes – Oval and Circle.
Here is source code of the Java program to create and fill shapes. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/*Java Program to Create and Fill Shapes using Applet*/
import java.applet.*;
import java.awt.*;
public class Shapes extends Applet
{
//Function to initialize the applet
public void init()
{
setBackground(Color.white);
}
//Function to draw and fill shapes
public void paint(Graphics g)
{
//Draw a square
g.setColor(Color.black);
g.drawString("Square",75,200);
int x[]={50,150,150,50};
int y[]={50,50,150,150};
g.drawPolygon(x,y,4);
g.setColor(Color.yellow);
g.fillPolygon(x,y,4);
//Draw a pentagon
g.setColor(Color.black);
g.drawString("Pentagon",225,200);
x=new int[]{200,250,300,300,250,200};
y=new int[]{100,50,100,150,150,100};
g.drawPolygon(x,y,6);
g.setColor(Color.yellow);
g.fillPolygon(x,y,6);
//Draw a circle
g.setColor(Color.black);
g.drawString("Circle",400,200);
g.drawOval(350,50,125,125);
g.setColor(Color.yellow);
g.fillOval(350,50,125,125);
//Draw an oval
g.setColor(Color.black);
g.drawString("Oval",100,380);
g.drawOval(50,250,150,100);
g.setColor(Color.yellow);
g.fillOval(50,250,150,100);
//Draw a rectangle
g.setColor(Color.black);
g.drawString("Rectangle",300,380);
x=new int[]{250,450,450,250};
y=new int[]{250,250,350,350};
g.drawPolygon(x,y,4);
g.setColor(Color.yellow);
g.fillPolygon(x,y,4);
//Draw a triangle
g.setColor(Color.black);
g.drawString("Traingle",100,525);
x=new int[]{50,50,200};
y=new int[]{500,400,500};
g.drawPolygon(x,y,3);
g.setColor(Color.yellow);
g.fillPolygon(x,y,3);
}
}
/*
<applet code = Shapes.class width=600 height=600>
</applet>
*/
To compile and run the program use the following commands :
>>>javac Shapes.java >>>appletviewer Shapes.java
1. The method drawPolygon(int x[],int y[],int n) is used to draw a polygon of n vertices, where each vertex has a co-ordinate (x,y) from the array of co-ordinates x[] and y[].
2. The method fillPolygon(int x[],int y[],int n) is used to fill a polygon of n vertices, where each vertex has a co-ordinate (x,y) from the array of co-ordinates x[] and y[].
3. The method drawOval(int x,int y,int width,int height) is used to draw an oval starting from the co-ordinate (x,y) of given width and height.
4. The method fillOval(int x,int y,int width,int height) is used to fill an oval starting from the co-ordinate (x,y) of given width and height.
Here’s the run time test cases for creating and filling shapes for different input cases.
Test case 1 – To View the Shapes.
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Java Books
- Practice Information Technology MCQs
- Practice BCA MCQs
- Practice Programming MCQs
- Apply for Computer Science Internship