java - Drawing a background image -
i draw image background image, fine when i'm trying add jbuttons frame whole business goes wrong picture drew disappear , see original background.
the picture drawing looks that:
public void paint(graphics g){ g.drawimage(bg, 0, 0, null); }
i guess problem paint method, need mentioned i'm extending jframe class.
edit: here pictures demonstrate mean.
it first draw image:
and when move mouse place buttons should have been drew, that's get:
import java.awt.*; import java.awt.image.bufferedimage; import javax.swing.*; import javax.swing.border.emptyborder; import java.net.url; import javax.imageio.imageio; public class framewithbg { public static void main(string[] args) throws exception { url url = new url("http://i.stack.imgur.com/ovog3.jpg"); final bufferedimage bg = imageio.read(url); runnable r = new runnable() { @override public void run() { jpanel c = new panelwithbackgroundimage(bg); c.setlayout(new gridlayout(0,5,16,16)); c.setborder(new emptyborder(10, 10, 10, 10)); (int ii = 1; ii < 26; ii++) { c.add(new jbutton("button " + ii)); } jframe f = new jframe(c.getclass().getsimplename()); f.add(c); f.pack(); f.setvisible(true); } }; // swing guis should created , updated on edt // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html swingutilities.invokelater(r); } } class panelwithbackgroundimage extends jpanel { image bg; panelwithbackgroundimage(image bg) { this.bg = bg; } @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(bg, 0, 0, getwidth(), getheight(), this); } }
Comments
Post a Comment