java - Why does drawImage fail when I am using it inside a Jpanel -
i doing initial section of simple platform game in java. have created class called entity
extends jpanel
, added window.
import javax.swing.*; import java.awt.*; /** * created bw12954 on 27/05/16. */ public abstract class entity extends jpanel { private final spritesheet sprites; private point location; private dimension dimensions; public entity(int x, int y, int w, int h, spritesheet sprites) { location = new point(x, y); dimensions = new dimension(w, h); this.sprites = sprites; } public entity(int x, int y, int w, int h) { this(x, y, w, h, null); } @override public dimension getpreferredsize() { return dimensions; } public void setlocation(int x, int y) { location.setlocation(x, y); } /* code removed here brevity */ @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(sprites.get(), (int)location.getx(), (int)location.gety(), null); } }
if add directly jframe below, graphic shows on window expect (note player simple subclass of entity)
public class window { private jframe window; public window() { swingutilities.invokelater(this::run); } private void run() { try { window = new jframe(); window.setdefaultcloseoperation(window.exit_on_close); window.setlocationbyplatform(true); window.setundecorated(true); player p = new player(0,0); window.add(p); window.setextendedstate(jframe.maximized_both); window.setvisible(true); } catch (ioexception e) { // todo handle exception e.printstacktrace(); } } }
however - when create class called world extends jpanel, add that window instead, use add()
method in constructor add new player it, doesn't appear. interestingly, if add setbackground()
constructor player/entity, can see coloured square entity should be. drawimage doesn't appear work.
if else has idea going on here, greatfully appreciated!
if add directly jframe below, graphic shows on window expect to
the default layout manager of content pane of frame borderlayout
. component added frame without constraint added "center" means component automatically resized fill entire space.
however - when create class called world extends jpanel, add window instead, use add() method in constructor add new player it, doesn't appear.
the default layout manager of jpanel
flowlayout
, respects preferred size of component added , reset location of component based on rules of layout manager.
interestingly, if add setbackground() constructor player/entity, can see coloured square entity should be. drawimage doesn't appear work
probably because preferred size calculation incorrect , image truncated.
location = new point(x, y); dimensions = new dimension(w, h);
the above code work if point (0, 0). more general case code should be:
location = new point(x, y); dimensions = new dimension(x + w, y + h);
because need consider paint image realative component.
so when should see image, not see image in proper location because layout manager override location.
so want continue approach of using components need use null layout, means manually need use setsize(...) , setlocation(...) of each component. in case size of component (w, h) , draw image using:
g.drawimage(sprites.get(), 0, 0, this);
note if use component approach there no need create custom component. use jlabel
imageicon
. assign icon when create label.
Comments
Post a Comment