2

Here's what I am trying to replicate:

Currently, my code is as follows:

public void boxes() {
    setLocation(20,20);
    for(int j =0; j < 5; j = j+1) {
    setLocation(20+50*j,20+50*j);
        for (int i= 0; i<4; i= i+1) {
            move(600-(50*j)); 
            turn(90);
    }
  }
}

and the result is:

PLEASE do not write me any code, I'd highly prefer just a general explanation as to how I can make it so that the boxes being drawn do not end at the same point. I've been trying to figure it out for the past two hours with no luck and what I currently have is so far the best I've gotten. Thank you!

This is based on http://www.greenfoot.org/scenarios/3535

Florian Genser
  • 160
  • 2
  • 7

2 Answers2

2

the problem is with the value you pass to the move() function, it should be:

move(600-(50*j*2));

the reason is that the length of each edge of the square should be shorter by twice the offset from the previous square, as it starts offset units deeper and ends offset units sooner (offset=50 in this case).

yurib
  • 8,043
  • 3
  • 30
  • 55
0

The j selects a next square. Ask yourself:

  • nice to know: end point of drawing is identical with begin point
  • you start by (50, 50) more inside. How do you come there from the prior end point
  • the new length to draw is how much smaller
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138