Java Program to Print Concentric Circles

This is a Java Program to Print Concentric Circles

Problem Description

We have to write a program in Java such that it prints five concentric circles in an applet using drawOval function of Graphics class.

Expected Input and Output

For print concentric circles using applet, we can have the following set of input and output.

To view the concentric circles :

On the execution of the program,
it is expected that five concentric circles are displayed in the applet.
Problem Solution

1. Consider a radius for the smallest circle.
2. Draw five concentric circles using drawOval function of Graphics class.

advertisement
advertisement
Program/Source Code

Here is source code of the Java Program to print concentric circles using applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Applet to Print Concentric Circles using Applet*/
  2. import java.applet.*;
  3. import java.awt.*;
  4. public class Concentric extends Applet
  5. {
  6.     //Initialize the applet
  7.     public void init()
  8.     {
  9. 	setBackground(Color.yellow);
  10.     }
  11.     //Draw cocentric circle
  12.     public void paint(Graphics g)
  13.     {
  14. 	g.setColor(Color.red);
  15. 	int rad=25;
  16. 	int dia=50;
  17. 	for(int i=0;i<5;i++)
  18. 	{
  19. 	    g.drawOval(250-(i*rad),250-(i*rad),(i+1)*dia,(i+1)*dia);
  20. 	}
  21.     }
  22. }
  23. /*
  24. <applet code = Concentric.class width=500 height=500>
  25. </applet>
  26. */

To execute the applet use the following commands:

Note: Join free Sanfoundry classes at Telegram or Youtube
>>>javac Concentric.java
>>>appletviewer Concentric.java
Program Explanation

1. To draw concentric circles use the drawOval(x,y,width,height) to draw an cirlce starting from the (x,y) co-ordinate of equal height and width.
2. Increment/decrement the parameters of drawOval for each circle.

Runtime Test Cases

Here’s the run time test cases to print concentric circles using Applet.

advertisement

Test case 1 – To Print Concentric Circles.
java-applet-concentric-circles

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.