1

Context: in my Java Swing app I have a chart (using JFreeChart) and when the user clicks on a datapoint on it, it opens a specific flash animation in a JDialog (flash played using DJNativeSwing).

Problem: when the flash animation starts playing, for some reason the background chart in the main window decides to refresh (calls its paintComponent()) and, as the chart is fairly heavy, this takes ~4 seconds during which the flash animation freezes.

I am thus looking for the most elegant / simplest solution to avoid the flash freeze. So far, I could imagine:

  • Find out why paintComponent() is called and avoid this
  • Open the JDialog (or child window without modal behavior) on a different thread

What would be the best approach and, most importantly, how to do it?

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
Tom
  • 1,375
  • 3
  • 24
  • 45
  • Different threads aren't going to help as swing uses a single thread for updating the EDT, so you'd only end up creating more issues. The paint manager may repaint a component for any number reasons, many of which are out side of your direct control. I'd be more concerned about why the chart is taking so long to repaint as it shouldn't need to do a complete repaint unless the underlying model or component has changed in some way – MadProgrammer May 18 '14 at 20:27
  • @MadProgrammer Sorry I don't have any idea about it. I just think it in the context of multi-threading. – Braj May 18 '14 at 20:29
  • You'll want to [profile](http://stackoverflow.com/q/2064427/230513) to be sure, but you might try setting `useBuffer` to `false` in your `ChartPanel` constructor to see the effect. – trashgod May 18 '14 at 20:33
  • @MadProgrammer: I'd agree that a simple refresh shouldn't take 4 seconds but here I depend on JFreeChart :( – Tom May 18 '14 at 20:33
  • @Trashgod: I'll profile and post results here. I just tested with useBuffer to false: makes the app almost not useable (any mouse move requires a 4sec repaint) and doesn't solve the issue. Thx anyway for suggesting – Tom May 18 '14 at 20:41
  • I only have a passing familiarity with JFreeChart, but as I understand it, it should using a buffer so it doesn't need to completely repaint the graph each time a paint occurs – MadProgrammer May 18 '14 at 20:49
  • 1
    After some profiling it appeared that the 4sec delay were mainly due to JFreeChart calling generateToolTip() for each datapoint on the chart, at each repaint (definitely not optimal: should be called only when tooltip is shown). For now I optimized my tooltip code with a perf gain of 10x which makes the animation delay acceptable. Thx all – Tom May 18 '14 at 22:09
  • @MadProgrammer: Setting `useBuffer` to `true` is the default, but occasionally `false` is faster, e.g. when the graphs simple but large. Tom: You can [answer your own question](http://meta.stackoverflow.com/q/17463/163188). – trashgod May 19 '14 at 01:31

0 Answers0