Java Program to Create and Fill Shapes using Applet

This is a Java Program to Create and Fill Shapes using Applet

Problem Description

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.

Expected Input and Output

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.
Problem Solution

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.

advertisement
advertisement
Program/Source Code

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.

  1. /*Java Program to Create and Fill Shapes using Applet*/
  2. import java.applet.*;
  3. import java.awt.*;
  4. public class Shapes extends Applet
  5. {
  6.     //Function to initialize the applet
  7.     public void init()
  8.     {
  9. 	setBackground(Color.white);
  10.     }
  11.     //Function to draw and fill shapes
  12.     public void paint(Graphics g)
  13.     {
  14. 	//Draw a square
  15. 	g.setColor(Color.black);
  16. 	g.drawString("Square",75,200);
  17. 	int x[]={50,150,150,50};
  18. 	int y[]={50,50,150,150};
  19. 	g.drawPolygon(x,y,4);
  20. 	g.setColor(Color.yellow);
  21. 	g.fillPolygon(x,y,4);
  22. 	//Draw a pentagon
  23. 	g.setColor(Color.black);
  24. 	g.drawString("Pentagon",225,200);
  25. 	x=new int[]{200,250,300,300,250,200};
  26. 	y=new int[]{100,50,100,150,150,100};
  27. 	g.drawPolygon(x,y,6);
  28. 	g.setColor(Color.yellow);
  29. 	g.fillPolygon(x,y,6);
  30. 	//Draw a circle
  31. 	g.setColor(Color.black);
  32. 	g.drawString("Circle",400,200);
  33. 	g.drawOval(350,50,125,125);
  34. 	g.setColor(Color.yellow);
  35. 	g.fillOval(350,50,125,125);	
  36. 	//Draw an oval
  37. 	g.setColor(Color.black);
  38. 	g.drawString("Oval",100,380);
  39. 	g.drawOval(50,250,150,100);
  40. 	g.setColor(Color.yellow);
  41. 	g.fillOval(50,250,150,100);	
  42. 	//Draw a rectangle
  43. 	g.setColor(Color.black);
  44. 	g.drawString("Rectangle",300,380);
  45. 	x=new int[]{250,450,450,250};
  46. 	y=new int[]{250,250,350,350};
  47. 	g.drawPolygon(x,y,4);
  48. 	g.setColor(Color.yellow);
  49. 	g.fillPolygon(x,y,4);
  50. 	//Draw a triangle
  51. 	g.setColor(Color.black);
  52. 	g.drawString("Traingle",100,525);
  53. 	x=new int[]{50,50,200};
  54. 	y=new int[]{500,400,500};
  55. 	g.drawPolygon(x,y,3);
  56. 	g.setColor(Color.yellow);
  57. 	g.fillPolygon(x,y,3);
  58.     }
  59. }
  60. /*
  61. <applet code = Shapes.class width=600 height=600>
  62. </applet>
  63. */

To compile and run the program use the following commands :

>>>javac Shapes.java
>>>appletviewer Shapes.java
Program Explanation

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.

advertisement

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.

Runtime Test Cases

Here’s the run time test cases for creating and filling shapes for different input cases.

Test case 1 – To View the Shapes.
java-applet-draw-fill-shapes

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.