Java 更改动画方向的关键点输入?

Java 更改动画方向的关键点输入?,java,applet,key,awt,keylistener,Java,Applet,Key,Awt,Keylistener,我正试图得到一个工作的电子游戏。我的问题是,我不知道如何使用键盘和WASD更改动画的方向 import java.applet.Applet; import java.awt.Color; import java.awt.Event; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; publ

我正试图得到一个工作的电子游戏。我的问题是,我不知道如何使用键盘和WASD更改动画的方向

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;

public class Grid extends Applet implements KeyListener
{
    int xCoord;   int yCoord;
    boolean firstPaint;
    int currentDirection1 = 1;
    int currentDirection2 = 3;
    int t=1;    int ponex = 400; int poney= 201;
    int ptwox= 400; int ptwoy = 401;

    public void init()  {  firstPaint = true;   this.addKeyListener(this); }

    public void paint(Graphics g)
    {
        if (firstPaint)
            firstPaint = false; 
        else
        {
            g.setColor(Color.cyan);
            g.fillRect(xCoord,yCoord,01,10);
        }

        g.setColor(Color.black);
        g.fillRect(0, 0, 801, 601);
        g.setColor(Color.red);

        for(int x = 0; x < 800; x += 20)
            g.drawLine(x, 600, x, 0);
        for(int y=0;y<600;y+=20)
            g.drawLine(800,y,0,y);
        try {
            Thread.sleep(500);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        draw(g);

    }
    public void draw(Graphics g)
    {
        g.setColor(Color.cyan);
        g.fillRect(ponex, poney, 20, 20);
        if(currentDirection1 == 1)
        {
            g.fillRect(ponex+=t, poney, 20, 20);
        }
        else if(currentDirection1 == 2)
        {
            g.fillRect(ponex, poney+=t, 20, 20);
        }
        else if(currentDirection1 == 3)
        {
            g.fillRect(ponex-=t, poney, 20, 20);
        }
        else if(currentDirection1 == 0)
        {
            g.fillRect(ponex, poney-=t, 20, 20);
        }
        g.setColor(Color.green);
        g.fillRect(ptwox, ptwoy, 20, 20);
        if(currentDirection2 == 1)
        {
            g.fillRect(ptwox+=t, ptwoy, 20, 20);
        }
        else if(currentDirection2 == 2)
        {
            g.fillRect(ptwox, ptwoy+=t, 20, 20);
        }
        else if(currentDirection2 == 3)
        {
            g.fillRect(ptwox-=t, ptwoy, 20, 20);
        }
        else if(currentDirection2 == 0)
        {
            g.fillRect(ptwox, ptwoy-=t, 20, 20);
        }
        try {
            Thread.sleep(30);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        draw(g);
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        xCoord = x;
        yCoord = y;
        repaint();
        return true;
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 38) {
            if (currentDirection1 != 2){//up
                currentDirection1 = 0;
            }
        } else if (e.getKeyCode() == 40) {//down
            if (currentDirection1 != 0){
                currentDirection1 = 2;
            }
        } else if (e.getKeyCode() == 39) {//right
            System.out.print("right");
            if (currentDirection1 != 3){
                currentDirection1 = 1;
            }
        } else if (e.getKeyCode() == 37) {//left
            if (currentDirection1 != 1){
                currentDirection1 = 3;
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_W){
            System.out.println("W");
            if (currentDirection2 != 2){
                currentDirection2 = 0;
            }
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            if (currentDirection2 != 0){
                currentDirection2 = 2;
            }
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            if (currentDirection2 != 3){
                currentDirection2 = 1;
            }
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            if (currentDirection2 != 1){
                currentDirection2 = 3;
            }
        }
    }
}
import java.applet.applet;
导入java.awt.Color;
导入java.awt.Event;
导入java.awt.Graphics;
导入java.awt.event.KeyEvent;
导入java.awt.event.KeyListener;
导入java.awt.event.MouseEvent;
公共类网格扩展小程序实现KeyListener
{
int xCoord;int yCoord;
布尔第一油漆;
int currentDirection1=1;
int currentDirection2=3;
int t=1;int-ponex=400;int-poney=201;
int-ptwox=400;int-ptwoy=401;
public void init(){firstPaint=true;this.addKeyListener(this);}
公共空间涂料(图g)
{
如果(第一道漆)
firstPaint=假;
其他的
{
g、 setColor(Color.cyan);
g、 fillRect(xCoord,yCoord,01,10);
}
g、 设置颜色(颜色为黑色);
g、 fillRect(0,0,801601);
g、 setColor(Color.red);
对于(int x=0;x<800;x+=20)
g、 抽绳(x,600,x,0);

对于(int y=0;y在EDT上睡觉从来都不好。除此之外,问题是什么?1)为什么要编写小程序?如果是因为老师的规范,请参考他们。2)为什么要使用AWT而不是Swing?请参阅上的此答案,了解放弃使用AWT组件的许多好理由。如果需要支持旧的基于AWT的API,请参阅。3)对于Swing,通常在基于AWT的较低级别上使用键绑定,
KeyListener
。有关详细信息,请参阅有关如何使用它们的详细信息。4)不要阻止EDT(事件调度线程)-发生这种情况时,GUI将“冻结”。而不是调用
Thread.sleep(n)
为重复任务实施Swing
计时器,或为长时间运行的任务实施
SwingWorker
。有关详细信息,请参阅。