0

I am writing a program, which is supposed to update and render animation for each frame. I have an AnimationPanel class which extends JPanel and an Animation class, which maintains my animation with an infinity loop. I was invoking paintComponent method by JPanel's paintImmediately method from animation (Animation object) and everything was working fast and well, until I added a new component to the JFrame. Then, components started to overlap one another, because new components were working on the EDT thread, but my animation was working on the main thread and somehow it was calling paintImmediately and then paintComponent also in the main thread, but of course it shouldn't. I tried to put the paintImmediately part of code to EDT thread and it worked, but the performance was really poor (instead of >100fps only ~40fps) and animation started lagging and it was behaving the same way as if it was repaint() method instead. So I guess I did something wrong, but I totally don't know what.
The paintComponent method looks like this:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    animation.render(g2d);
}

and render method look like this:

public void render(Graphics2D g2d) {
    objects.forEach((o) -> o.render(g2d));
}

Each object has its own render method, which usually is g2d.fill(/*object's shape*/)

The render() method called by loop:

private void render() {
    //It calls paintComponent in the main thread
    panel.paintImmediately(0, 0, panel.getWidth(), panel.getHeight());
}

and its modification which was laggy:

private void render() {
    //It calls paintComponent int the EDT thread
    SwingUtilities.invokeAndWait(()->panel.paintImmediately(0, 0, panel.getWidth(), panel.getHeight()));
}

Thank you for your help!

  • Off topic maybe, but who still uses Swing? and why? – Hobbamok Mar 17 '21 at 11:46
  • School project, default java is required :/ Are there any better default java libraries for graphics? – Andrzej Sołtysik Mar 17 '21 at 11:48
  • 1
    Absolutely. But I also had to use Swing in School :/ Generally: JavaFX is just the new Swing. It's the official Oracle GUI/graphics Library and included in the default installation – Hobbamok Mar 17 '21 at 12:13
  • Alright, I'll try it then. Thanks! – Andrzej Sołtysik Mar 17 '21 at 12:23
  • 1
    Btw. JavaFX comes with the FXML System as recommended (but not necessary) entrypoint. Any tutorial that loads a file in the launch (main) method is overcomplicating it for your usecase – Hobbamok Mar 17 '21 at 12:24
  • 1
    *which maintains my animation with an infinity loop.* - you should not be using a loop. Instead you use a Swing Timer. You should not be invoking paintImmediately() or use invokeAndWait(...). Just use repaint() on the panel. I have never seen JavaFX included in the base JDK up to version 11. Maybe that has changed in version 14. – camickr Mar 17 '21 at 14:15
  • But when using repaint() i can see that there are frames which weren't rendered, because repaint() doesn't act at once. That is why I tried using paintimmediately(). However I will try use Timer and repaint(), maybe it will work better, thanks! – Andrzej Sołtysik Mar 17 '21 at 15:54

0 Answers0