Question

(In Java) Create an applet which draws two sets of concentric circles. Each set will use recursio...

(In Java) Create an applet which draws two sets of concentric circles. Each set will use recursion:

  • Start with a diameter of 255 for the outermost circle
  • As each circle is drawn, use a color created via a constructor in the Color class:
  Color circleColor = new Color(diameter, diameter, diameter) ;  
  • Use recursion to draw the next inner circle:
    • for one set of circles, decrease the diameter of each circle by 20 pixels
    • for the other set of circles, decrease the diameter of each circle by 10%
  • The base case: stop when the diameter of the circle is less than to 20 pixels.

Include a program file of your code and a PrintScreen of the output of your program.

Below is a sample output. Notice how the circles on the left form a figure that appears flat, while the circles on the right appear to have some depth.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class Circle extends Applet
{

public void drawCircle(Graphics g,int diameter)
{
//condition to check if diameter is less than 20 pixels
if(diameter<20)
return;
//constructor for Color class
Color circleColor= new Color(diameter,diameter,diameter);
g.setColor(circleColor);
//decrease the diameter of each circle by 20
int i=20+(255-diameter);
//Method to create a circle
g.drawOval(i,i,255-(255-diameter-diameter),255-(255-diameter-diameter));
drawCircle(g,diameter-20);

}

//Method for drawing concentric circle diameter decreses by 10%
public void drawCircle2(Graphics g,int diameter)
{
if(diameter<20)
return;
Color circleColor= new Color(diameter,diameter,diameter);
g.setColor(circleColor);
//x- coordinate
int x=540+(255-diameter);
//y-Coordinate
int y=20+(255-diameter);
g.drawOval(x,y,255-(255-diameter-diameter),255-(255-diameter-diameter));
//reduce diameter by 10%
drawCircle2(g,diameter-(int)(0.1*diameter));

}
public void paint(Graphics g)
{
  
//Method to decrease the diameter of each circle by 20 pixels
drawCircle(g,255);
//Method to decrease the diameter of each circle by 10%
drawCircle2(g,255);
}

}

let Viewer: Circle.class Applet

Add a comment
Know the answer?
Add Answer to:
(In Java) Create an applet which draws two sets of concentric circles. Each set will use recursio...
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
  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow...

    import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...

  • import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow...

    import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...

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
ADVERTISEMENT