0

Hey everyone, I am learning recursion in Java and I have a lab to do. I have done the first part right, which is to draw those squares. The second part of it is to do the reverse of it, so left starts from the smallest square to biggest. I have to use recursion, and as I have learned, if I do the recursion first then drawRect it should be backwards, but it's not. Can someone help me please?

import java.awt.*;
import java.awt.event.*;

public class Lab19b {
    @SuppressWarnings("deprecation")
    public static void main(String args[]) {
        // Do not change this method
        Windows win = new Windows();
        win.setSize(1000, 750);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        win.show();
    }
}
@SuppressWarnings("serial")
class Windows extends Frame {
    public static void drawSquare(Graphics g,int x,int y,int width,int height) //the first set of squares
    {
        if (width>4 && x<1000) //if the width is more than four and x is not more than monitor...
        {
            g.fillRect(x, y, width, height); //rectangles is filled
            drawSquare(g, x+(width+10),300-(int)(width*.75),(int)(width*.75),(int)(height*.75)); //the recursive call
        }
    }
    public static void drawSquare2(Graphics g,int x,int y,int width,int height) //the second set of squares
    {
        if (width>4 && x<1000) //

        {
            drawSquare2(g,x+(width+10),y,(int)(width*.75),(int)(height*.75)); //the second set of squares
            System.out.println(height);
            g.fillRect(x, y, width, height); //rectangles is filled
        }
    }
    public void paint (Graphics g) //the paint method
        {
        drawSquare(g, 0, 100,200,200); //the first set is drawn
        drawSquare2(g, 0, 500,200,200); //the second set is drawn
        }
    }
2
Contributors
1
Reply
16
Views
3 Years
Discussion Span
Last Post by stultuske
0

first working item, replace your awt code by Swing code. not entirely sure about your drawing logic, neither. seems to me if you increase the sizes in your one loop, you should decrease them in the other.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.