java - Fading in an image on top of a jPanel -
i want have image fade in onto panel part of card layout. when i'm @ place in program, panel show on top , want image loaded in fade-in effect. big project paste relevant code.
i have gui class contains jframe, jpanels etc. when event triggered, code runs:
cardmain.show(pmain, "cleprechaun"); fadein.run(pleprechaun);
it loads correct panel , runs static method in fadein class, supposed add image onto panel pleprechaun.
here fadein class:
import java.awt.alphacomposite; import java.awt.component; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; public class fadein extends jpanel implements actionlistener { image imagem; timer timer; private float alpha = 0f; public fadein() { imagem = new imageicon("darkforest.jpg").getimage(); timer = new timer(100, this); timer.start(); } // here define alpha 0f 1f public fadein(float alpha) { imagem = new imageicon("darkforest.jpg").getimage(); this.alpha = alpha; } @override public void paintcomponent(graphics g) { system.out.println("paint"); super.paintcomponent(g); graphics2d g2d = (graphics2d) g; g2d.setcomposite(alphacomposite.getinstance(alphacomposite.src_over, alpha)); g2d.drawimage(imagem, 0, 0, null); } public static void run(jpanel jpanel) { jpanel.add(new fadein()); } public void actionperformed(actionevent e) { alpha += 0.05f; if (alpha >1) { alpha = 1; timer.stop(); } repaint(); } }
nothing happens, no image show on panel. try out, instead of jpanel.add(new fadein()); have tried create new jframe , adding new fadein onto that, , works then. of course, instead of image being painted on jpanel, new jframe pops ontop of main one, image nicely fading in. that's not want happening.
is there way solve this?
er go:
assuming have following sequence of calls:
jframe f=new jframe(); f.setsize(500, 500); jpanel j=new jpanel(); f.add(j); f.setvisible(true); fadein.run(j);
you need change size follows:
public fadein(dimension d) { setsize(d); .... // rest of set }
then in run:
run(jpanel jpanel) { jpanel.add(new fadein(jpanel.getsize())); }
Comments
Post a Comment