我有一份我准备上交的作业,在作业中我不得不使用递归来绘制10层深的嵌套圆圈,在敲了几下我的脑袋后我终于完成了它。我唯一的问题是,我正在绘制10级或实际11级的图像吗?
这个问题来自这样一个事实:我已经明确说明递归到了10级时结束,但我确实告诉方法绘制原始圆然后它自己调用。这让我认为它绘制了第一级然后降低了10级,总共达到11级。它创建的图像到目前为止我无法计算圆圈:/
任何澄清将不胜感激,谢谢!
// import statements
import java.awt.*;
import javax.swing.*;
public class RecursiveCircles
{
public static void main(String[] args)
{
// create a new canvas
RCanvas myCanvas = new RCanvas();
// create JFrame
JFrame myJframe = new JFrame();
myJframe.setTitle("Recursive Circles");
// set JFrame size, location and close operation
myJframe.setSize(1500, 500);
myJframe.setLocation(100, 100);
myJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// and canvas to JFrame and make it visble
myJframe.getContentPane().add(myCanvas);
myJframe.setVisible(true);
} // end main
} // end class RecursiveCircles
/*
* this class will draw the main circle of the image and will have the recursive
* method that draws the two outer circles down 10 levels
*/
class RCanvas extends Canvas
{
// defualt constructor
public RCanvas ()
{}
public void paint (Graphics graphics)
{
// declare variables
String title = "Recursive Circles";
int n = 300; // diamerter of the circle
int xOrigin = 600; // x location of start of circle (makes circle around the origin I wanted
int yOrigin = 100; // y location of start of circle (makes circle around the origin i wanted
int radius = n/2; // radius of circle (half the diamerter
int level = 10;
// make canvas background color black
graphics.setColor(Color.black); // make the background color black
graphics.fillRect(0, 0, 1500, 500); // rectangle that fills the JFrame
// put title on canvas
graphics.setColor(Color.BLUE);
graphics.drawString(title, 725, 50);
// draw main circle
graphics.setColor(Color.WHITE);
graphics.drawOval(xOrigin, yOrigin, n, n);
drawRCircles(graphics,xOrigin,yOrigin,radius,level); // call recrusrive method
System.out.println(level);
} // end paint
/*
* This is the recursive method that will draw the two circles on the outer sides of the
* main circle. it will then call itself and repate the process till it is 10 levels deep
*/
public void drawRCircles(Graphics graphics,int xOrigin,int yOrigin, int radius, int level)
{
int newRadius = (radius/2); // radius of smaller circle
int newXOrigin = xOrigin - (newRadius); // xOrigin of circle on left of the main circle
int newYOrigin = yOrigin + (newRadius); // yOrigin of circle on the right of the main circle
int newXOrigin2 = xOrigin + (newRadius*3); // xOrigin of circle on the right of the main circle
int newYOrigin2 = yOrigin + (newRadius); // yOrigin of circle on the right of the main circle
if (level > 0) // counts down from 10 to make the recursive image 10 levels deep
{
graphics.drawOval(newXOrigin, newYOrigin, newRadius*2, newRadius*2); // draw recursive circle on the left of main circle
graphics.drawOval(newXOrigin2, newYOrigin2, newRadius*2, newRadius*2); // draw recursive circle on the right of main circle
drawRCircles(graphics, newXOrigin, newYOrigin , newRadius, (level-1)); // make recursion of left circle
drawRCircles(graphics, newXOrigin2, newYOrigin2,newRadius,(level-1)); // make recursion of right circle
}// end if
} // end drawRCircles
}// end class
答案 0 :(得分:0)
您的代码正在绘制11个圈子,但您对drawRCircles
本身的呼叫仅对其中的10个圈子负责。
更具体地说,这些线条绘制了1个圆圈。
// draw main circle
graphics.setColor(Color.WHITE);
graphics.drawOval(xOrigin, yOrigin, n, n);
无论您是否具有drawRCircles
功能,都会绘制此圆圈。 (尝试删除它并运行代码以查看会发生什么。)
然后,你的代码调用 drawRCircles
函数11次,但是自上次调用它以来只有绘制 10个圆圈,其中level = 0失败了测试if(level > 0)
。
它创建的图像到目前为止我无法计算圆圈:/
快速提示:由于您想知道,如果给定最高级别N
,是否会绘制N
或N+1
级别的圈子,您也可以尝试更改{ {1}}变量更易于管理(如2)并可视化检查。
回到上面的代码,忽略level
部分(因为它与你的递归圆绘图无关),你有
draw main circle
并在 drawRCircles(graphics,xOrigin,yOrigin,radius,level); // call recursive method
System.out.println(level);
函数中
public void drawRCircles(…)
让我们用level = 2而不是10来检查它:
drawRCircles(…,level-1);
级别数= 2 =绘制的递归圆的数量。