3

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:

ball with blue trail

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.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Tuấn Phạm
  • 688
  • 6
  • 20
  • I don't see any code here to clear the GraphicsContext on each tick of the animation or to fill it with black or something. So, It is not especially surprising to find that it leaves a trail behind it. – ManoDestra Apr 13 '16 at 21:12
  • 1
    well if i understand you right, i did paint the ball black before the move happens (coordinates of the ball change). you could see it all the first lines of the method. I'm learning so please enlighten me if theres something i dont know. – Tuấn Phạm Apr 13 '16 at 21:15
  • 1
    Well, if you're painting the ball, then moving it during an update, then repainting, it will leave a trace from the previous ball. Normally, you would have to clear the entire canvas (or GraphicsContext) prior to any draw operations, unless you can do it in such a way that the draw operation of everything is reversed. It's the nature of [immediate mode](https://en.wikipedia.org/wiki/Immediate_mode_%28computer_graphics%29) drawing. – ManoDestra Apr 13 '16 at 21:38
  • 1
    Maybe i understand what you are saying. I will have a look at the matter. What would you suggest to fix this ? Should i create a separate a method to paint the ball back to black BEFORE updating it ? Or there is something i should add into ? Many thanks – Tuấn Phạm Apr 13 '16 at 21:44
  • Just paint a black rectangle or something before any of your draw operations on each animation frame tick. Update your model, clear the drawing area, do your draw operations, repeat. The same applies to any animation cycle in immediate mode. – ManoDestra Apr 13 '16 at 22:00

1 Answers1

1

I don't know that much about JavaFX, I use a different system for graphics, but I think if you add this at the beginning, it may help a little:

gc.clearRect(0D, 0D, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);