java - paint() can't draw gif but png works fine? -


i'm using jlabel in attempt draw animated gif image onto it. can use constructor new jlabel(new imageicon(fml.class.getresource("giphy.gif"))); , work fine when go override paint method doesn't seem want draw it, @ all. image isn't still, it's not there! should mention both methods shown below work fine png not gif. missing or java bug? (using java 1.8)

edit: i've been looking around , seems i'm not off point on need doing i'm missing something. i've seen many posts like one doesn't seem working in case.

i should note there's literally nothing else going on in class, it's simple jpanel.

    import java.awt.eventqueue; import java.awt.flowlayout; import java.awt.graphics; import java.awt.gridbaglayout; import java.awt.image; import java.awt.event.mouseadapter; import java.awt.event.mouseevent;  import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class fml {      public static void main(string[] args) {         new fml();     }      public fml() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception ex) {                 } catch (instantiationexception ex) {                 } catch (illegalaccessexception ex) {                 } catch (unsupportedlookandfeelexception ex) {                 }                  jframe frame = new jframe("test");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.getcontentpane().setlayout(new flowlayout());                 frame.getcontentpane().add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          private imageicon animatedgif;          public testpane() {             setlayout(new gridbaglayout());             jbutton btn = new jbutton(new imageicon(fml.class.getresource("fmdxw.png")));             btn.setsize(50, 50);             btn.setrolloverenabled(true);             animatedgif = new imageicon(fml.class.getresource("giphy.gif"));             btn.setrollovericon(animatedgif);             add(btn);              btn.addmouselistener(new mouseadapter() {                  @override                 public void mouseentered(mouseevent e) {                     animatedgif.getimage().flush();                 }              });              //i need             final imageicon image = new imageicon(fml.class.getresource("giphy.gif"));              //to render on             final imageicon image2 = new imageicon(fml.class.getresource("fmdxw.png"));               //i'm not understanding why can add gif constructor paint method fails.             jlabel label = new jlabel(image) {                 @override                 public void paint(graphics g) {                     super.paint(g);                     g.drawimage(image2.getimage(), 64, 64, this);                 }             };             //setsize because want 100% sure it's loading correct size.             //removing doesn't affect problem @ hand.             label.setsize(64, 64);             add(label);         }     } } 

g.drawimage(image2.getimage(), 64, 64, this); 

assuming size of image 64, why trying draw second image outside bounds of label? when change location (0, 0) gif paints fine me (but i'm not using animated gif).

both methods shown below work fine png not gif.

i don't see how reason gave above. in case, did try normal gif?

this reason why should not trying manage painting since need worry location , hardcoding location not practice. let layout manager you.

again, why doing this. why trying override paint method draw second image?

instead should doing like:

  1. drawing both images in paintcomponent() method
  2. use panel overlaylayout, can stack 2 labels on top of 1 another.
  3. you can set layout manager of layout gridbaglayout , when add jlabel label automatically centered.

//setsize because want 100% sure it's loading correct size.

you said know how layout managers work. if know statement nothing since layout manager override value set, using code wrong , shows basic lack of understanding , should not included in mcve since point of mcve` simplify problem as possible.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -