Nikki
Nikki

Reputation: 3674

Animated Gifs triggering paintComponent (Java)?

While reading religiously through SO, I've seen many examples of using animated gifs in java via the classic:

  JLabel mylabel=new JLabel(new ImageIcon("animated.gif"));

Which works fine but when I add mylabel to a JPanel, I've noticed the JPanel's paintComponent method gets called every time the gif changes frames.

Is that really necessary?
Isn't the gif isolated to the JLabel?

It seems like an expensive waste of time to repaint the whole panel (and whatever other components are added to it) every time a gif cycles frames.

In case you're wondering why I care: From doing web design (js/css), I learnt how to avoid reflows almost entirely and isolate repaints to only the element being changed. Worked wonders for performance.

Upvotes: 1

Views: 562

Answers (1)

kleopatra
kleopatra

Reputation: 51525

Is that really necessary?

Yes: the JLabel is not-opaque by default, that's why its parent must repaint itself behind on changes of the label .. And if you had explored deeper: the parent only repaints the area behind the label (see the clip)

Upvotes: 4

Related Questions