1

I'm working on a project where we are supposed to have rectangles of random sizes bounce off a wall and change color each time they bounce.

When you click on them, they are supposed to freeze in place and turn red. I'm just having trouble having them stop and for some reason they slow when one is clicked.

import java.util.Random;
public class Main {

public static void main(String[] args) {
    MovingRectangles[] rectangles = new MovingRectangles[5];

    Random rng = new Random();

    for (int i = 0; i < rectangles.length; i++) {
        rectangles[i] = new MovingRectangles(rng.nextDouble(),      rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble());

    }

    for (int g = 0; g < rectangles.length; g++) {
                rectangles[g] = new MovingRectangles(
                        rng.nextDouble(), 
                        rng.nextDouble(), 
                        rng.nextDouble() / 50 - 0.01, 
                        rng.nextDouble() / 50 - 0.01, 
                        rng.nextDouble() * 0.04 + 0.03,
                        rng.nextDouble() * 0.04 + 0.03
                        );
            }


    while (true) {
                StdDraw.clear();

                for (int h = 0; h < rectangles.length; h++) {
                    rectangles[h].draw();
                    rectangles[h].update();

                } 

                int count = 0;

                    for (int i =0; i < rectangles.length; i++) {
                        rectangles[i].draw();

                        if (StdDraw.mousePressed() &&     rectangles[i].containsPoint(StdDraw.mouseX(), StdDraw.mouseY())) {
                            rectangles[i].freeze();
                        }
                        if (rectangles[i].isFrozen()) {
                        count++;

                StdDraw.show(25);
            }


    }


} 

}}

This is the class for moving rectangles. Stackoverflow says I need to add context to explain what this code is.

import java.util.Random;

public class MovingRectangles {
private double x;
private double y;
private double vx;
private double vy;
private double hx;
private double hy;
private boolean isFrozen;
private int red;
private int green;
private int blue;

public MovingRectangles(double x, double y, double vx, double vy, double hx, double hy) {
    this.x = x;
    this.y = y;
    this.vx = vx;
    this.vy = vy;
    isFrozen = false;
    this.hx = hx;
    this.hy = hy;
    randomColor();

}
public void randomColor() {
    Random rng = new Random();

    red = rng.nextInt(256);
    blue = rng.nextInt(256);
    green = rng.nextInt(256);
}
public void draw() {
    if (isFrozen) {
        StdDraw.setPenColor(StdDraw.RED);
    } else {
    StdDraw.setPenColor(red, green, blue);
    }
    StdDraw.filledRectangle(x, y, hx, hy);
}

public void update() {

    x += vx;
    y += vy;

    if (x - hx < 0) {
        vx *= -1;
        x = 0 + hx;
        randomColor();
    }   
    if (x + hx > 1) {
        vx *= -1;
        x = 1 - hx;
        randomColor();
    }
    if (y - hy < 0) {
        vy *= -1;
        y = 0 + hy;
        randomColor();
    }
    if (y + hy > 1) {
        vy *= -1;
        y = 1 - hy;
        randomColor();
    }
    }
public void freeze() {
    isFrozen = true;
}
public boolean isFrozen() {
    return isFrozen;
}
public boolean containsPoint(double a, double b) {
    return 
    a > x - hx &&
    a < x + hx &&
    b > y - hy &&
    b < y + hy;
}

}

The only other thing I need to add is for it to print "You Win" when all five of the boxes have been clicked. thanks for any help.

Nick Roberts
  • 245
  • 4
  • 12
donkey brian
  • 29
  • 1
  • 4

3 Answers3

0

add at the very beginning of update method line like

if(isFrozen) return;

it should stop your rectangle.

the another way (if you don't want to touch rectangle class). after

            for (int h = 0; h < rectangles.length; h++) {
                rectangles[h].draw();

add if(!rectangles[h].isFrozen()) rectangles[h].update();

vadim
  • 178
  • 1
  • 9
0

My thought is that you're not stopping the actual update of the rectangle.

In your MovingRectangles class...

public void update() {
    if(!this.isFrozen) {
        {...your code...}
    }
}
Community
  • 1
  • 1
Nick Roberts
  • 245
  • 4
  • 12
0

This update to draw fixed the problem I was having.

public void draw() {
    if (isFrozen) {
        StdDraw.setPenColor(StdDraw.RED);
        StdDraw.filledRectangle(x, y, hx, hy);
        vx = 0;
        vy = 0;

    } else {
    StdDraw.setPenColor(red, green, blue);
    }
    StdDraw.filledRectangle(x, y, hx, hy);
}
donkey brian
  • 29
  • 1
  • 4