0

I'm making a pokemon game and this class is just supposed to paint the map to the screen and move the character around according to keystrokes detected on the arrow keys but the keyPressed method isn't detecting keystrokes and my character won't move. I can't figure it 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 i = 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 a = 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;
        a = 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 was 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) { }
}
Ilya
  • 21,871
  • 8
  • 73
  • 92
esnitz
  • 3
  • 2

1 Answers1

0

The most likely problem is that your JApplet does not have the focus so the events are being delivered to a different component. There are a few workarounds you can try. First, you can try adding the line setFocusable(true); to the init() method of your JApplet. This is not the most robust solution, but I have used it successfully in the past. Alternatively, you can use KeyBindings as suggested in this question.

Community
  • 1
  • 1
Paul
  • 134
  • 4