0

I've been coding a project of balls bouncing of the wall and everything works fine besides when I move the mouse over the drawing panel fast enough, the repaint is laggy. So I made an example in Edit 2 and the problem exists there too. Why? How can I fix it or work around it?

Edit 1: This is how my code looks like: here
Edit 2: I created this example to make it more clear.

import javax.swing.*;
import java.awt.*;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class DrawingPanel extends JPanel
{
    public Ball ball;
    public DrawingPanel()
    {
        super();
        setPreferredSize(new Dimension(300, 100));
        setBackground(Color.black);
        this.ball = new Ball(10);
        this.ball.start();
    }
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Laggy ball");
        DrawingPanel panel = new DrawingPanel();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
        while(true)
        {
            try
            {
                Thread.sleep(17);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            panel.repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        ball.draw(g);
    }

    public class Ball extends Thread
    {
        public int position;
        public int velocity;
        public Ball(int velocity)
        {
            super();
            this.position = 0;
            this.velocity = velocity;
        }
        void draw(Graphics g)
        {
            g.setColor(Color.blue);
            g.fillOval(position, 50, 20, 20);
        }
        void update()
        {
            position += velocity;
            if(position > getWidth() || position < 0)
                velocity *= -1;
        }
        @Override
        public void run()
        {
            while(true)
            {
                try
                {
                    Thread.sleep(17);
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                update();
            }
        }
    }
}

Edit 3: One problem solved. At least I know the problem is not the code but my laptop.

Edit 4: I noticed the problem only exists while using external mouse. Otherwise it's all fine. It was a local problem not with the code. Thanks for help, though!

reconn
  • 371
  • 3
  • 9
  • 1
    This should be ask on **codereview** .. How did you repaing your balls ? From scratch or just the changes ? note : Since I can't open a Pastebin link from the office, I can't see your code. You should add this to your question (the interesing part only) – AxelH Nov 21 '16 at 10:54
  • 1
    Did you implement a mousemotionlistener (according to your tag) ? – Gildraths Nov 21 '16 at 10:56
  • This might help you : http://stackoverflow.com/questions/19781603/cause-of-lag-in-painting-a-line-set-by-dragging-the-mouse – theDarkerHorse Nov 21 '16 at 11:03
  • @Gildraths I did not implement any mouse (motion) listener – reconn Nov 21 '16 at 11:11
  • Don't call `dispose()`, this should do it (if my `Graphic` are not to rusty) – AxelH Nov 21 '16 at 11:17
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 21 '16 at 11:17
  • @AxelH I posted a clear example without using dispose() in Edit 2, same effect. But as you can see I dispose g2d which is rebuilt every repaint() so that's fine – reconn Nov 21 '16 at 11:54
  • 1
    Now that you gave this example, I can't reproduce your lag. This is smooth on a light VM (single core, 2Gb of RAM). – AxelH Nov 21 '16 at 12:00
  • @AxelH apparently you are right :( On my friend's macbook it works fine while on my macbook (same model) it lags while mouse is in motion over the panel. – reconn Nov 21 '16 at 12:10
  • Well, I would point that this is a OSX problem maybe, I did work some time to made a app compatible to OSX because Java was not fully compatible. – AxelH Nov 21 '16 at 12:14
  • @AxelH You may be right.. anyways thanks for all the help! – reconn Nov 21 '16 at 12:21

0 Answers0