如何修复分形图形中递归调用的矩形问题?
Looks like you're working on a fractal square assignment, and the recursive part isn't quite there yet. Let's walk through fixing your code step by step to get that fractal working.
First, let's address the issues in your initial code:
- The
drawSquare1method has a truncated line (thefillRectcall cuts off), which causes a syntax error. - The recursive
drawMoreSquaresfunction is commented out and unimplemented.
Here's the fixed, working code, followed by a breakdown of what's happening:
// Lab17bvst.java // Fixed version of the Lab17b assignment. import java.awt.*; import java.applet.*; public class Lab17bvst extends Applet { public void paint (Graphics g) { // Start recursion with depth 10, center at (500,325), initial size 400 drawMoreSquares(10, g, 500, 325, 400); } // Helper to draw a single square centered at (centerX, centerY) with given size private void drawSquare(Graphics g, int centerX, int centerY, int size) { int halfSize = size / 2; int tlX = centerX - halfSize; int tlY = centerY - halfSize; g.fillRect(tlX, tlY, size, size); } // Recursive method to generate the fractal squares private void drawMoreSquares(int depth, Graphics g, int centerX, int centerY, int size) { // Base case: stop when depth is 0 or square is too small to see if (depth <= 0 || size < 5) { return; } // Draw the current square drawSquare(g, centerX, centerY, size); // Size of smaller squares (adjust this ratio for different fractals!) int smallerSize = size / 2; // Recursively draw squares at each corner of the current square drawMoreSquares(depth - 1, g, centerX - smallerSize, centerY - smallerSize, smallerSize); // Top-left drawMoreSquares(depth - 1, g, centerX + smallerSize, centerY - smallerSize, smallerSize); // Top-right drawMoreSquares(depth - 1, g, centerX - smallerSize, centerY + smallerSize, smallerSize); // Bottom-left drawMoreSquares(depth - 1, g, centerX + smallerSize, centerY + smallerSize, smallerSize); // Bottom-right } }
What Changed & Why:
Fixed the Truncated Line: The original
drawSquare1had an incompletefillRectcall. I replaced it with a cleanerdrawSquarehelper that uses center coordinates (easier for recursion than calculating midpoints from maxX/maxY).Implemented Recursive Logic:
- Base Case: Every recursive function needs a stop condition. Here, we stop when the recursion depth hits 0 (we've drawn all levels) or the square size is less than 5 pixels (too small to be visible).
- Recursive Step: For each square, we draw it, then call
drawMoreSquaresfour times—once for each corner of the current square. Each recursive call uses a smaller square size (half the original) and decrements the depth by 1.
Updated Initial Call: Instead of drawing just one square, we start the recursion with an initial depth (10 levels), center coordinates to position the fractal, and a starting size for the largest square.
Customization Tips:
- Change the Fractal Pattern: Instead of dividing the size by 2, try dividing by 3 to get a denser fractal.
- Add Color: Modify the
drawSquaremethod to set different colors based on depth (e.g.,g.setColor(new Color(depth * 20, 0, 0))for red tones that get lighter as depth increases). - Adjust Positions: Instead of drawing squares at the corners, draw them at the midpoints of the current square's sides for a different fractal shape.
内容的提问来源于stack exchange,提问作者David Rusnak

