java - Adding a timer to KeyListener -


i extended jframe class , have own model , jpanel extended classes instance variables. implemented keylistener jframe , works arrow keys model moves extremely slow around frame when hold keys down. question how attach keylistener methods timer or make model move faster when hold keys. if possible, how can model move 2 directions @ once, left , up?

public class gamecontroller extends jframe implements keylistener,actionlistener {     private gamepieces p;     private gamepanel panel;     private timer timer;      public gamecontroller()     {         super("balls");         setsize(800, 600);         timer = new timer(10, this);         setdefaultcloseoperation(jframe.exit_on_close);          container c = getcontentpane();         c.setlayout(new borderlayout());          p = new gamepieces();         panel = new gamepanel();          p.addobserver(panel);          c.add(panel);          addkeylistener(this);          panel.update(p, null);          setresizable(false);         timer.start();     }      public void actionperformed(actionevent e)      {         string actioncommand = e.getactioncommand();         if (e.getsource() == timer)         {             p.checkeat();             p.moveothers();             panel.update(p, null);         }     }      public void keytyped(keyevent e)     {         int s = 0;     }       public void keypressed(keyevent e)     {         int key = e.getkeycode();         if (key==38)         {             p.up();             panel.update(p, null);         }         else if (key==40)         {             p.down();             panel.update(p, null);         }         else if (key==39)         {             p.right();             panel.update(p, null);         }         else if (key==37)         {             p.left();             panel.update(p, null);         }     }      public void keyreleased(keyevent e)     {         int o = 0;     }      public static void main(string[]args)     {         gamecontroller = new gamecontroller();         a.setvisible(true);     } } 

you on right way should little more sophisticated. follow mvc pattern. should have model remembers panel should - up(), down() methods update model. should have viewer shows panel @ current position. panel.update() belongs viewer. , should have controller changes position (invoke these , down, left , right) , invokes viewer when necessary - show movement or next frame.

your keylistener tells controller move position in intervals when key pressed. , tells controller stop doing when key released. see, parts move smoothly , independently. keylistener tells controller do, controller - changes model , invokes viewer. model nothing - holds data. viewer nothing shows current data.

p.s. don't forget thread safety since press multiple keys , invoke controller multiple times in same thread.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -