java - How do I draw an image to a JPanel or JFrame? -
how draw image jpanel or jframe, have read oracle's tutorial on can't seem right. need image "beachroad.png
" displayed on specific set of coordinates. here have far.
public class level1 extends jframe implements actionlistener { static jlayeredpane everythingbutplayer; static level1 l1; public level1() { everythingbutplayer = new jlayeredpane(); bufferedimage img = null; try { img = imageio.read(new file("beachroad.png")); } catch (ioexception e) { } graphics g = img.getgraphics(); g.drawimage(img,0, 0, everythingbutplayer); this.add(everythingbutplayer); }
and in main(),
l1 = new level1(); l1.settitle("poop"); l1.setsize(1920, 1080); l1.setdefaultcloseoperation(jframe.exit_on_close); l1.setvisible(true);
thanks in advance!
try this:
package com.sandbox; import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.windowconstants; import java.awt.graphics; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; public class swingsandbox { public static void main(string[] args) throws ioexception { jframe frame = buildframe(); final bufferedimage image = imageio.read(new file("c:\\projects\\mavensandbox\\src\\main\\resources\\img.jpg")); jpanel pane = new jpanel() { @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(image, 0, 0, null); } }; frame.add(pane); } private static jframe buildframe() { jframe frame = new jframe(); frame.setdefaultcloseoperation(windowconstants.exit_on_close); frame.setsize(200, 200); frame.setvisible(true); return frame; } }
Comments
Post a Comment