JAVA QUESTION (recursion) DrawingPanel class: https://docs.google.com/document/d
ID: 3704541 • Letter: J
Question
JAVA QUESTION (recursion)DrawingPanel class: https://docs.google.com/document/d/1wCIMItUt7vTZj3C46juYn_vayZh0Ja8TrhXdkmpMmVQ/edit?usp=sharing Draw circle: import java.awt.*; import java.util.Random; import java.util.Scanner;
public class DrawCircle { public static void main(String [] args){ DrawingPanel panel = new DrawingPanel(500,500); Random rand = new Random(); panel.setBackground(Color.LIGHT_GRAY); Graphics brush = panel.getGraphics(); Scanner keyboard = new Scanner(System.in); System.out.println("Enter number of circles"); int num= keyboard.nextInt(); drawCircle(rand, brush, 1,1,20,20, num); } public static void drawCircle(Random rand, Graphics brush, int x, int y, int width, int height, int num){ if(num==0) return ; else { Color c = new Color (rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); brush.setColor(c); brush.fillOval(x,y,width,height); drawCircle(rand, brush, rand.nextInt(500), rand.nextInt(500), 20,20,num-1); }//end else }//end method }//end class
Part 2. Modify the circle drawing draw a picture like below. This is a recursive program that uses the fWRect method. fuRect has the same parameters as fUOval did Draw circle class import ?AYAsant.. * ; import java.uti1.Random import java.util.Scanner public class Dra Circle public static void main(String args) DrawingPanel panelnew DrawingPaneL(see,5e8): Random rand new Random) panel.setBackground (Color.LIGHT GRAY) Graphics brush = panel.getGraphics(); Scanner keyboardnew Scanner(System.in): System.out printin("Enter number of circles") int num- keyboard.nextInt); drawCircle(rand, brush, 1,1,20,2, num) public static void drawircle(Random rand, Graphics brush, int x, int y, int width, int height, int num)t return else t Color cnew Color (rand.nextInt (256), rand.nextInt (256), rand.nextInt (256)) brush.setcolor(c); brush.flloval(x.y.width height) drawCircle(rand, brush, rand.nextInt (580), rand.nextint(5ee). 28,2e num-): /fend else /end method end class The drawRect recursive method should start the x and y at 0,0. Each recursive call modifies x and y by adding 20. You also need to adjust the size of each rectangle making each successive call make the rectangle height and width reduced by 40 .Instead of asking the user for the number of circles assume that you will stop (the base case) when the width is
Explanation / Answer
import java.awt.*;
import java.util.Random;
import java.util.Scanner;
public class DrawCircle {
public static void main(String [] args){
DrawingPanel panel = new DrawingPanel(500,500);
Random rand = new Random();
panel.setBackground(Color.LIGHT_GRAY);
Graphics brush = panel.getGraphics();
drawRectangle(rand, brush, 0,0,160,160);
}
// Let max height width be 160
public static void drawRectangle(Random rand, Graphics brush, int x, int y, int width, int height){
if(width<20)
return ;
else {
Color c = new Color (rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
brush.setColor(c);
brush.fillRect(x,y,width,height);
drawCircle(rand, brush, x+20,y+20,width-40, height-40);
}//end else
}//end method
}//end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.