1
votes

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.

1
"Worked wonders for performance." Look into premature optimization. This question reeks of it. - Andrew Thompson
BTW - if ./ means the current directory/package and ../ means the parent directory, what does .../ mean? - Andrew Thompson
"then noticed the behavior while messing around in the code." How did you notice it? In the rendering of the GUI or the debugger of the IDE? If the former, make sure it is not being slowed down by the debugger. If the latter, it supports what I am saying - there is no need to optimize the code. - Andrew Thompson
if you are really interested in learning Swing, better stop looking at it with your web design hat on :-) - kleopatra
@kleopatra & AndrewThompson. I really do appreciate the advice and your patience. I understand now why you haven't been particularly impressed by my various questions. I've got a lot of old habits I need to put aside. - Nikki

1 Answers

4
votes

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)