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.

advertisement

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.

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.

advertisement
  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:

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

advertisement
Runtime Test Cases

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

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

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn