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;      } } ' 

  1. you're creating 3 jframes, it's kind of hard track things going. there no need use extends jframe either of 2 classes presented, in fact, highlight cause of error...
  2. jframe not have paintcomponent method, never called, instead, change gamegui extend jpanel , add @override start of paintcomponent method declaration (before it), change super.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 , overriding getpreferredsize of custom components.
  • don't call setvisible until after you've established base ui
  • make sure adding components displayable surface if expect them painted

enter image description here

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

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -