Java Canvas - Rectangle2D Scales When Moving

I am drawing a series of rectangles on canvas. The rectangles should move at an angle. For some reason, when they move, they expand:

xPos += xSpeed;
yPos += ySpeed;
updateBounds(xPos, yPos, width, height);

      

My UpdateBounds method:

public void updateBounds(double x, double y, double w, double h) {
    bounds.setRect(x, y, w, h);
}

      

Bounds is a Rectangle2D object. And my drawing method:

g.fillRect((int) bounds.getX(), (int) bounds.getY(),
                (int) bounds.getMaxX(), (int) bounds.getMaxY());

      

Why am I getting this behavior?

+3


source to share


1 answer


Graphics.fillRect () takes a width and height parameter, not the largest x and y position to draw the rectangle.

The third and fourth parameters fillRect

must be Rectangle2D getWidth () and GetHeight () .



As a reference, a link to what getMaxX () will receive .

+3


source







All Articles