Question
Asap
Please write a Java program with comments
Asap: Please write a java program with comments Write a program that uses recursion to display concentric circles, as shown i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

concentricCircles.java:

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class concentricCircles {
   public static void main(String[] args) {
       Ovals ovalsPanel = new Ovals(); //call class to draw a circle
       JFrame newFrame = new JFrame(); //create a frame
       newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       newFrame.add(ovalsPanel);
       newFrame.setSize(311, 333); //set the size of the frame
       newFrame.setVisible(true);
   }
}

class Ovals extends JPanel {
   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       int width = getWidth(); //width
       int height = getHeight(); //height
       System.out.printf("Width is %d \t and height is %d\n", width, height); //print the height and width
       // calculates the center of the panel
       int xPoint = width / 2;
       int yPoint = height / 2;
       drawCircles(g, 15, xPoint, yPoint); //Recursive call
   }

   private void drawCircles(Graphics g, int n, int X, int Y) {
       if (n > 0) {
           g.drawOval(X - (n * 10), Y - (n * 10), (n * 20), (n * 20)); //draw a circle
           drawCircles(g, n - 1, X,Y); //call recurrsive function
       }
   }
}

output:

Add a comment
Know the answer?
Add Answer to:
Asap Please write a Java program with comments Asap: Please write a java program with comments...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT