I am programming a simple game in which the ball should bounce when it hits the wall. I used JavaFX for the GUI, Canvas and GraphicsContext for drawing.
My code for drawing the Ball:
public void moving(){
gc.setFill(Color.BLACK);
gc.fillOval(ball.getCenterX()-15, ball.getCenterY()-15, 30,30);
if (ball.getCenterX()<285 && ball.getCenterX()>15) {
double xtemp = ball.getCenterX()+vektorx;
ball.setCenterX(xtemp);
} else {
vektorx = -vektorx;
double xtemp = ball.getCenterX()+vektorx;
ball.setCenterX(xtemp);
}
if (ball.getCenterY()<485 && ball.getCenterY()>15) {
double ytemp = ball.getCenterY()+vektory;
ball.setCenterY(ytemp);
} else {
vektory = -vektory;
double ytemp = ball.getCenterY()+vektory;
ball.setCenterY(ytemp);
}
gc.setFill(Color.AQUA);
gc.fillOval(ball.getCenterX()-15, ball.getCenterY()-15, 30,30);
}
Note that gc
is the GraphicsContext I used. Everything works the way it's supposed to, but only one thing bothers me:
The ball leaves a trace of road its been taking and it bothers me very much. I tried the code for one move (draw it in blue and black again), it leaves no trace, but when I put it on a thread and let it run, this thing happens.