W
W
Web__Nikita032019-11-21 19:45:29
Java
Web__Nikita03, 2019-11-21 19:45:29

Why are the squares drawn at the wrong size?

What I think is the problematic code
I have a program where squares move around the screen. But I have a problem, even though side = 30, the squares are very big. Where is my mistake?

public class Ball extends AFigure {

    float x, y, vx, vy;
    Paint p;
    int side;

    Ball(int w ,int h){
        p = new Paint(Color.BLACK);
        p.setStyle(Paint.Style.FILL);

        side = 30;

        x = (float) (Math.random()*w);
        y = (float) (Math.random()*h);

        vx = (float)(Math.random() * 8) + 8;
        vy = (float)(Math.random() * 8) + 8;
    }

    @Override
    public void draw(Canvas s) {
        s.drawRect(x, y, side, side, p);

    }

    @Override
    public void move(int w, int h) {
        if (x > w || x < 0) vx *= -1;
        if (y > h || y < 0) vy *= -1;
        x += vx;
        y += vy;
    }
}

All code - https://pastebin.com/LUtki8c1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Web__Nikita03, 2019-11-21
@Web__Nikita03

I figured out the problem. The answer is here https://medium.com/better-programming/learn-all-an...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question