This is a Java Program to Print Concentric Circles
We have to write a program in Java such that it prints five concentric circles in an applet using drawOval function of Graphics class.
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.
1. Consider a radius for the smallest circle.
2. Draw five concentric circles using drawOval function of Graphics class.
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.
/*Java Applet to Print Concentric Circles using Applet*/
import java.applet.*;
import java.awt.*;
public class Concentric extends Applet
{
//Initialize the applet
public void init()
{
setBackground(Color.yellow);
}
//Draw cocentric circle
public void paint(Graphics g)
{
g.setColor(Color.red);
int rad=25;
int dia=50;
for(int i=0;i<5;i++)
{
g.drawOval(250-(i*rad),250-(i*rad),(i+1)*dia,(i+1)*dia);
}
}
}
/*
<applet code = Concentric.class width=500 height=500>
</applet>
*/
To execute the applet use the following commands:
>>>javac Concentric.java >>>appletviewer Concentric.java
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.
Here’s the run time test cases to print concentric circles using Applet.
Test case 1 – To Print Concentric Circles.
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Programming Books
- Check Java Books
- Practice Programming MCQs
- Practice BCA MCQs
- Apply for Java Internship