0

I'm working on some animation where I have a certain number of dots wandering around my JFrame and based on their distance they should be connected by lines of different strengths.

The base code for moving the dots works and actually I also had them displayed correctly in the beggining but I had some issues where the movement was stuttering (probably due to the repaint process). At that point the Window class handled the entire repaint procedure.

After reading some posts around here I adapted my code according to the github page linked in this post to use the individual Dots as JComponents and have them being repainted individually. However, now the problem is that although I still have 100 Dots as components on my JPanel, only one of them is being painted (however, the stuttering is gone at least). I also see that all components are being iterated and their repaint method is being called but they just don't display.

This is my paintComponent method in the Dot class:

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(new Color(0, 0, 0));
    Ellipse2D.Double circle = new Ellipse2D.Double(x - 10 / 2, y - 10 / 2, 10, 10);
    g2d.fill(circle);
}

And this is what my repaintTimer looks like:

final Timer repaintTimer = new Timer(20, new ActionListener() {
    
    @Override
    public void actionPerformed(ActionEvent e) {
        for(Component component : window.getContentPane().getComponents()) {
            component.repaint();
        }
        
        recalculateDots();
    }
});
repaintTimer.start();

The result I get is something like this: enter image description here

I tried some things of which I thought that it could solve the problem but nothing really helped and I'm really confused as to why this is happening. I'd appreciate any help very much because this issue doesn't make any sense for me.

Samaranth
  • 385
  • 3
  • 16
  • I suggest creating an application model consisting of one or more plain Java getter/setter classes to hold the dots and lines and one and only one drawing `JPanel` which is repainted completely for every frame of your animation. The Oracle tutorial [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) will show you how.' – Gilbert Le Blanc Mar 22 '22 at 15:46
  • 1
    Agree with above suggestion to NOT use individual components and instead do the custom painting yourself. Check out: https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681 for an example to get you started. – camickr Mar 22 '22 at 16:06
  • Thanks for these suggestions. The problem is that - if my assumption from the first glance of this code is correct - this was what I did in my initial attempt, but it had this stuttering animation to it. But I think I'll have a better look at the code sometime later when I have more time and provide an update if it worked. – Samaranth Mar 22 '22 at 16:38
  • 1
    @camickr I just had the time to take a closer look at the code in the article you provided and it finally worked. Thanks for sharing this, I think I wouldn't have solved this otherwise. If you want to make it an official answer, I can mark it as accepted. – Samaranth Mar 24 '22 at 23:57
  • If the linked answer helped you can just upvote the answer. There is no need to repeat the answer here. – camickr Mar 25 '22 at 02:19

0 Answers0