我有一個家庭作業任務,我已準備好接受任務,我不得不使用遞歸繪製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
感謝您的信息!都發布瞭解決方案的工作,但我打算將您的標記爲正確的答案,因爲您還深入瞭解了一些內容並給出了一些提示,謝謝:D –