You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复分形图形中递归调用的矩形问题?

Fixing the Recursive Rectangle Fractal in Your Lab17bvst.java Code

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:

  1. The drawSquare1 method has a truncated line (the fillRect call cuts off), which causes a syntax error.
  2. The recursive drawMoreSquares function 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:

  1. Fixed the Truncated Line: The original drawSquare1 had an incomplete fillRect call. I replaced it with a cleaner drawSquare helper that uses center coordinates (easier for recursion than calculating midpoints from maxX/maxY).

  2. 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 drawMoreSquares four 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.
  3. 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 drawSquare method 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:36:08