java - Image as background and paintComponent -


i trying make game college ball player navigates keyboard through maze. thing stuck on background image of maze, when add "ball" repaints background (defualt color, no image) when comment out paint , paint component, maze background back, no ball of course.

i new java , have searched , can't see soulition code have. setopaque? isopaque?.... opaque right direction?

once this... totally understand using collision detection detect collisions maze "wall"

trying figure out, being new java, can make grown man cry.

main.java

public class main{  public static void main (string[] args){      //system.out.println("start of game");      gameframe game1 = new gameframe(); } 

javagame.java

import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.imageio.imageio; import javax.swing.imageicon; import java.io.file; import java.io.ioexception;   public class gameframe extends jframe{          private int framewidth = 240, frameheight =315;     int x, y;     private image dbimage;     private graphics dbg;  //below constructor          public gameframe(){                    super("operationmaze!");         addkeylistener(new al());         setresizable(false);         //settitle("operationmaze!2");         //setsize(250, 302);         setsize(framewidth, frameheight); //set height , width         setdefaultcloseoperation(jframe.exit_on_close);                           //because of opening file, there needs try catch incase there error opening.             //plus java won't let unless try catch.     try{                  setcontentpane(new jlabel(new imageicon(imageio.read(new file("c:/documents , settings/brando/my documents/netbeansprojects/gameframe/src/maze.jpg")))));      }   catch (ioexception e) {                 e.printstacktrace();                                     }                  //show frame           setvisible(true);         //setopaque(false);             y = 35;         x = 15; }      public void paint(graphics g) {         dbimage = createimage(getwidth(), getheight());        dbg = dbimage.getgraphics();        paintcomponent(dbg);        g.drawimage(dbimage, 0, 0, this);                }      public void paintcomponent(graphics g) {                          g.filloval(x, y, 15, 15);         //g.setopaque(false);          repaint();             }        // private void setopaque(boolean b) {    //     throw new unsupportedoperationexception("not supported yet."); //to change body of generated methods, choose tools | templates.     // }      public class al extends keyadapter {           public void keypressed(keyevent e) {             int keycode = e.getkeycode();             if(keycode == e.vk_left) {                 if(x <= 5)                     x = 5;                 else                      x += -5;             }             if(keycode == e.vk_right) {                 if(x >= 230)                     x = 230;                 else                     x += +5;             }              if(keycode == e.vk_up) {                 if(y <= 23)                     y=23;                 else                     y += -5;             }              if(keycode == e.vk_down) {                 if(y >= 277)                     y = 277;                 else                     y += +5;             }      }     } } 

ok, lets's see what's going wrong here:

when add "ball" repaints background (default color, no image)

in scenario, when using custom paint method, don't call super.paint(g), jlabel image doesnt drawn.

the image do create blank:

dbimage = createimage(getwidth(), getheight()); 

(graphics offers convenient overloaded method of drawimage allows specify width & height, allowing avoid doing this.)

the result: no background image.

when comment out paint , paint component, maze background back, no ball of course.

now swing calling default paint implementation ensures child components drawn you're not calling filloval method, net result: background image, no ball.

where next:

to allow swing work of custom painting need override paintcomponent rather calling directly. cannot done in top level container jframe. sub-classing jcomponent , overriding paintcomponent in new component allow swing call method without having directly. adding @override allow compiler check are indeed overriding method. you're intending paint image, load image , save reference use when need paint it:

dbimage = imageio.read(new file("myimage.jpg")); 

then in new jcomponent-based class:

@override protected void paintcomponent(graphics g) {    super.paintcomponent(g);    g.drawimage(dbimage, 0, 0, getwidth(), getheight(), this);    g.filloval(x, y, 15, 15); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -