How to fix movement with Graphics2D using thread?

public class Billiard extends JPanel
    // Constructor
    public static Ball ball[] = new Ball[16];
    private static int x = 0;
    private static int y = 0;

    public Billiard () {
        super ();

        // White Ball.
        ball[15] = new Ball(x+50,y+165,15);


public void paintComponent (Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint (RenderingHints.KEY_ANTIALIASING, 
        g2d.setRenderingHint (RenderingHints.KEY_RENDERING, 
        g2d.setRenderingHint (RenderingHints.KEY_COLOR_RENDERING, 
        super.paintComponent (g);

        for (int i = 0; i < 16; i++) {
            ball[i].paint (g2d);

        for(int n = 0; n<100; n++){
                catch(Exception e)


When i use .move() on its own it moves the ball, however i cant seem to make the ball move smoothly using threads an repaint, any advice, i have declared the balls using a constructor from a different class, and these balls are being painted to a window in a another class and im adding them to it using setContentPane.

1 answer

  • answered 2018-03-11 12:36 jeff

    You shouldn't need to use multithreading to make this render smoother. You will need to tune your ball movement speed and your frame rate.

    Right now, you are moving the ball in every single frame which is likely what is making it jump so much. Additionally, you are only sleeping for 7 ms between frames which is very rapid.

    My advice: 1) Handle the ball movement outside of the frame rendering loop. I don't think you want to move it on every single frame. 2) Increase the sleep time between frames until the movement looks more natural. Try 50/100 for starters and increase it if your animations are too fast. Decrease it if they are too slow.