change the color of the sides of a rectangle with each rotation

Asked 1 years ago, Updated 1 years ago, 302 views

As I mentioned in the title, each time you rotate, the color of the sides of the rectangle is
Red→Orange→Yellow→Green→Blue→Purple
I would like to change it to
In the program below, start at the bottom left corner of the rectangle. Red→Orange→Yellow→Green→Blue→Purple
and the color changes, but at the end of the purple (when the turtle reaches the upper right corner of the rectangle) it returns to the beginning (the lower left corner of the rectangle, i.e. back to the starting point).

What kind of program do you want to do is, even when you finish writing purple, one after another, i.e.,
Red → Orange → Yellow → Green → Blue → Purple → Orange → Yellow → Green → Blue → Purple → Red → Orange → Yellow → Green ...
I'd like to do that.

Current Program

public class RainbowTurtle{
    public void draw() {
    Turtle 1 = new Turtle();
    t1.move(100,200);
    t1.penDown();
        t1.setColor(java.awt.Color.RED);
    t1.go(100);

    Turtle t2 = t1;
    t2.rotate(90);
        t2.setColor(java.awt.Color.ORANGE);
    t2.go(100);

    Turtle t3 = t2;
    t3.rotate(90);
        t3.setColor(java.awt.Color.YELLOW);
    t3.go(100);

    Turtle t4 = t3;
    t4.rotate(90);
        t4.setColor(java.awt.Color.GREEN);
    t4.go(100);

        Turtle t5 = t4;
    t5.rotate(90);
        t5.setColor(java.awt.Color.BLUE);
    t5.go(100);

        Turtle t6 = t5;
    t6.rotate(90);
        t6.setColor(java.awt.Color.MAGENTA);
    t6.go(100);
    }

    public static void main(String[]args) {
    RainbowTurtle rect = new RainbowTurtle();

        while(true){
    rect.draw();
        }
   }
}

How do I rewrite this program to achieve the desired results?
Please point it out.

java

2022-09-30 21:56

1 Answers

Instead of calling draw during an infinite loop of while, you should be able to achieve the desired behavior by making an infinite loop in draw.
Tortoise

  • Change color
  • Go straight
  • Rotate 90 degrees

Let's code consciously so that the behavior is repeated.

public void draw(){
    Turtle 1 = new Turtle();
    t1.move(100,200);
    t1.penDown();
    // Out of loop until initial positioning and penDown
    while(true){
      t1.setColor(java.awt.Color.RED);
      t1.go(100);
      t1.rotate(90);
      (omitted hereinafter)
    }
  }

The main method side while is not required.


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.