java - Paint doesnt seem to be painting things -
i've been trying tp paint image of cheetah mess around paint method , such, doesn't seem working, idea why?
i've tried ton of stuff found on here nothing seems toi work @ all, button, know actual paint method not working because doesn't pain hello string.
here's main class:
package dev.main; import javax.swing.jframe; import javax.swing.jpanel; import dev.angora.gui.gamegui; @suppresswarnings("serial") public class main extends jframe { public static jframe p = new jframe("angora realms"); public static void main (string[] args) { new main(); } public main() { p.setdefaultcloseoperation(jframe.exit_on_close); p.pack(); p.setsize(640, 800); p.setvisible(true); p.setlayout(null); gamegui g = new gamegui(); g.creategui(p); } }
and here class try paint in:
package dev.gui; import java.awt.button; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.net.url; import javax.swing.jframe; import javax.swing.jpanel; public class gamegui extends jframe implements actionlistener { public button drawcard = new button("draw card"); public image cheetah = null; public void creategui(jframe p) { drawcard.addactionlistener(this); drawcard.setbounds(20,30,80,30); p.add(drawcard); } @override public void actionperformed(actionevent event) { object cause = event.getsource(); if (cause == drawcard) { system.out.println("ay"); } } public void paintcomponent(graphics g) { super.paint(g); g.drawstring("hello", 200, 50); if (cheetah == null) { cheetah = getimage("plains/cheetah.png"); graphics2d g2 = (graphics2d)g; g2.drawimage(cheetah, 100, 100, 100, 300, this); } } public image getimage(string path) { image tempimage = null; try { url imageurl = gamegui.class.getresource(path); tempimage = toolkit.getdefaulttoolkit().getimage(imageurl); } catch (exception e) { system.out.println("an error occured -" + e.getmessage()); } return tempimage; } } '
- you're creating 3
jframe
s, it's kind of hard track things going. there no need useextends jframe
either of 2 classes presented, in fact, highlight cause of error... jframe
not havepaintcomponent
method, never called, instead, changegamegui
extendjpanel
, add@override
start ofpaintcomponent
method declaration (before it), changesuper.paint(g);
super.paintcomponent(g);
for example...
public class gamegui extends jpanel implements actionlistener { public button drawcard = new button("draw card"); public image cheetah = null; public void creategui(jframe p) { drawcard.addactionlistener(this); drawcard.setbounds(20,30,80,30); p.add(drawcard); } @override public void actionperformed(actionevent event) { object cause = event.getsource(); if (cause == drawcard) { system.out.println("ay"); } } @override protected void paintcomponent(graphics g) { super.paintcomponent(g);
no offense, i'm not fan of passing reference of "container" child component can add in way. better create instance of gamegui
, add ever container want. gamegui
doesn't need know or care...imho
runnable example
- don't use
pack
,setsize
, competing each other. content of frame should dictate size of frame. done through use of appropriate layout managers , overridinggetpreferredsize
of custom components. - don't call
setvisible
until after you've established base ui - make sure adding components displayable surface if expect them painted
also, you're mixing heavy weight (java.awt.button
) , light weight (swing/javax.swing.jframe
) components together...my recommendation is, don't, cause bunch of other issues don't need deal with.
import java.awt.button; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.net.url; import javax.swing.jframe; import javax.swing.jpanel; public class main extends jframe { public static void main(string[] args) { new main(); } public main() { jframe p = new jframe("angora realms"); p.setdefaultcloseoperation(jframe.exit_on_close); gamegui g = new gamegui(); p.add(g); p.setsize(640, 800); p.setvisible(true); } public class gamegui extends jpanel implements actionlistener { public button drawcard = new button("draw card"); public image cheetah = null; public gamegui() { drawcard.addactionlistener(this); add(drawcard); } @override public void actionperformed(actionevent event) { object cause = event.getsource(); if (cause == drawcard) { system.out.println("ay"); } } @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawstring("hello", 200, 50); if (cheetah == null) { cheetah = getimage("plains/cheetah.png"); graphics2d g2 = (graphics2d) g; g2.drawimage(cheetah, 100, 100, 100, 300, this); } } public image getimage(string path) { image tempimage = null; try { url imageurl = gamegui.class.getresource(path); tempimage = toolkit.getdefaulttoolkit().getimage(imageurl); } catch (exception e) { system.out.println("an error occured -" + e.getmessage()); } return tempimage; } } }
Comments
Post a Comment