java - Can't get my image to move using KeyListner -


i'm making pokemon game , class supposed paint map screen , move character around according keystrokes detected on arrow keys keypressed method isn't detecting keystrokes , character won't move. can't figure out.

import java.awt.*; import java.applet.*; import java.io.*; import javax.swing.*; import java.awt.event.*; import java.lang.math.*; import java.util.*; import java.util.arraylist; import java.awt.image.*; import java.awt.event.keyevent;  public class map extends japplet implements keylistener { image grass; image sgrass; image sand; image chardown, charup, charleft, charright; image finalboss; image drawn; image[][] gr = new image[13][15]; public int x; public int y; javax.swing.timer t; double ttt = 0; double b = 0; public void init() {     addkeylistener (this);     grass = getimage(getcodebase(),"grass.jpg");     sgrass = getimage(getcodebase(),"non-grass.jpg");     sand = getimage(getcodebase(),"sand.jpg");     finalboss = getimage(getcodebase(), "pleft.jpg");     chardown = getimage(getcodebase(), "chardown.gif");     charup = getimage(getcodebase(), "charup.gif");     charleft = getimage(getcodebase(), "charleft.gif");     charright = getimage(getcodebase(), "charright.gif");     drawn = chardown;     grid(); } public void grid() {     for(int = 0; i<13; i++)     {         for(int j = 0; j<15; j++)         {             double x = math.random();             if(x<=0.70)             {                 gr[i][j] = grass;             }             else if(x>0.70 && x<=0.90)             {                 gr[i][j] = sgrass;             }             else if (x>0.90)             {                 gr[i][j] = sand;             }         }     } }   public void paint(graphics g) {     int = 0;     int b = 0;     for(int z = 0; z<13; z++)     {         for(int w = 0; w<15; w++)         {             g.drawimage(gr[z][w],a,b,50,50,this);             a+=50;                         if (a > 750)             break;         }         b+=50;         = 0;         if (b > 650)         break;     }     g.drawimage(drawn, x, y, 50, 50, this);     g.drawimage(finalboss, 700, 600, 50, 50, this); } public void keypressed(keyevent e) {     joptionpane.showmessagedialog(null, "key pressed");     int key = e.getkeycode();     if (key == 37)     {         //left         x=x-50;         drawn = charleft;     }     else if (key == 39)     {         //right         x=x+50;         drawn = charright;     }     else if (key == 38)     {         //up         y=y-50;         drawn = charup;     }     else if (key == 40)     {         //down         y=y+50;         drawn = chardown;     }     repaint(); } public void keytyped(keyevent e) { } public void keyreleased(keyevent e) { } } 

the problem japplet not have focus events being delivered different component. there few workarounds can try. first, can try adding line setfocusable(true); init() method of japplet. not robust solution, have used in past. alternatively, can use keybindings suggested in this question.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -