java - Add moving objects to JFrame -


i'm creating java game. in game there hero , bubble. hero supposed move when press arrow keys , bubble supposed have continuous diagonal movement. when add hero or bubble directly jframe separately desired behavior, when add them both small square! tried add them same jpanel , after add jpanel jframe not working. have define type of layout jpanels.

what doing wrong?

code:

public class pang { public static void main(string[] args) {      jframe f=new jframe();     jpanel gamepanel=new jpanel();     gamepanel.setpreferredsize(new dimension(800, 600));       drawhero d=new drawhero();     drawbubble bubble=new drawbubble();      gamepanel.add(d);     gamepanel.add(bubble);      f.add(gamepanel);      f.setvisible(true);     f.setdefaultcloseoperation(jframe.exit_on_close);     f.setsize(800, 600); } }   public class drawhero extends jpanel implements actionlistener, keylistener {     timer mytimer = new timer(5, this);     int x = 0, y = 0, dx = 0, dy = 0, step = 10;     private transient image imagehero = null;      public drawhero() {         mytimer.start();         addkeylistener(this);         setfocusable(true);         setfocustraversalkeysenabled(false);      }      public void paintcomponent(graphics g) {         super.paintcomponent(g);         graphics2d g2 = (graphics2d) g;          imagehero = getheroimage();          g2.drawimage(imagehero, x, y, 40, 40, null);      }      public image getheroimage() {         image image = null;          image = getimage("hero.png");         return image;     }      public image getimage(string path) {         image tempimage = null;         try {             url heroiurl = drawhero.class.getresource(path);             tempimage = toolkit.getdefaulttoolkit().getimage(heroiurl);         } catch (exception e) {             system.out.println("error loading hero image! - "                     + e.getmessage());         }         return tempimage;     }      public void actionperformed(actionevent e) {          repaint();     }      public void moveup() {          y = y - step;      }      public void movedown() {         y = y + step;      }      public void moveleft() {          x = x - step;      }      public void moveright() {         x = x + step;      }      public void keypressed(keyevent e) {         int keycode = e.getkeycode();         if (keycode == keyevent.vk_up) {             moveup();         }         if (keycode == keyevent.vk_down) {             movedown();         }         if (keycode == keyevent.vk_left) {             moveleft();         }         if (keycode == keyevent.vk_right) {             moveright();         }     }      public void keytyped(keyevent e) {     }      public void keyreleased(keyevent e) {     } }   public class drawbubble extends jpanel implements actionlistener, keylistener {     timer mytimer = new timer(5, this);     int x = 100, y = 200, dx = 0, dy = 0, step = 10;     private transient image imagehero = null;      public drawbubble() {         mytimer.start();         addkeylistener(this);         setfocusable(true);         setfocustraversalkeysenabled(false);      }      public void paintcomponent(graphics g) {         super.paintcomponent(g);         graphics2d g2 = (graphics2d) g;         g2.fill(new ellipse2d.double(x, y, 40, 40));       }       public void actionperformed(actionevent e) {           x=x+dx;          y=y+dy;         repaint();     }      public void movebubble() {          dy=2;          dx=2;     }        public void keypressed(keyevent e) {         movebubble();      }      public void keytyped(keyevent e) {     }      public void keyreleased(keyevent e) {     } } 

i recommend neither drawhero nor drawbubble (which should called hero nor bubble respectively) should extend jcomponent. instead each should know how draw graphics object passed it, when requested so.

then single gamefield or playingarea class should keep references bubble objects , hero , draw call draw(graphics) method of objects.

using approach not necessary worry layouts within gamefield component (they become irrelevant).

that basic strategy pursue rendering stationary objects in this answer [collision detection complex shapes.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -