图像位置不会响应关键事件而改变

时间:2015-08-29 03:21:29

标签: java

package testapplication;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class TestApplication extends JFrame implements Runnable {

    int sizex = 800;
    int sizey = 650;
    int x, y, xDirection, yDirection;
    private Image dbImage;
    private Graphics dbg;
    Image character;

    @Override
    public void run(){
        try{
            while(true){

                move();

                Thread.sleep(5);

            }
        }
        catch(Exception e){
            System.out.println("ERROR!!!");
        }
    }

    public void move(){
        x += xDirection;
        y += yDirection;
        if(x <= 0)
            x = 0;
        if(x >= 778)
            x = 778;
        if(y <= 22)
            y = 22;
        if(y >= 628)
            y = 628;
    }

    public void setXDirection(int xdir){
        xDirection = xdir;

    }
    public void setYDirection(int ydir){
        yDirection = ydir;
    }

    Font font = new Font("Arial", Font.BOLD, 30);

    public class AL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

            //Key press inputs "WASD"
            if(keyCode == KeyEvent.VK_W) {
                setYDirection(-1);
            }
            if(keyCode == KeyEvent.VK_A) {
                setXDirection(-1);
            }
            if(keyCode == KeyEvent.VK_S) {
                setYDirection(+1);
            }
            if(keyCode == KeyEvent.VK_D) {
                setXDirection(+1);
            }
            //end Key press inputs "WASD"

        }
        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            //Key release inputs "WASD"
            if(keyCode == KeyEvent.VK_W) {
                setYDirection(0);
            }
            if(keyCode == KeyEvent.VK_A) {
                setXDirection(0);
            }
            if(keyCode == KeyEvent.VK_S) {
                setYDirection(0);
            }
            if(keyCode == KeyEvent.VK_D) {
                setXDirection(0);
            }
            //end Key release inputs "WASD"

        }
    }


    public TestApplication() {
        //Load images
        ImageIcon i = new ImageIcon("C:/Users/Min/Documents/NetBeansProjects/TestApplication/src/testapplication/Untitled-1.png") {};
        character = i.getImage();
        //Game properties
        addKeyListener(new AL());
        setTitle("TestApplication");
        setSize(sizex, sizey);
        setResizable(false);
        setVisible(true);
        setBackground(Color.LIGHT_GRAY);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        x = 30;
        y = 628;
    }

    @Override
    public void paint(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void paintComponent(Graphics g) {
        g.setFont(font);
        g.setColor(Color.RED);
        g.drawString("Welcome to TESTTEST", 300,125);
        g.setColor(Color.RED);
        g.drawImage(character, x, y, this);


        repaint();
    }

    public static void main(String[] args) {
        TestApplication ta = new TestApplication();
        //Threads
        Thread t1 = new Thread();
        t1.start();
    }

}

在我的Java代码中,应该有一个使用WASD键移动的图像。图像显示,但它不会移动。怎么了? 这是一个简单的Java代码,应该使用WASD键使图像在窗口中漫游。我不确定我在代码中做错了什么,我已经仔细检查过,一切都很好......

2 个答案:

答案 0 :(得分:2)

首先,如果您需要在用户按下其中一个wsda键时更改图像位置,则需要将1和-1添加到x和y的当前值(图像位置)。你只需设置1和-1就可以将图像移动一个像素,即使你例如反复按d按钮多次也是如此。

你需要将方法setXDirection更改为this(我在等号之前添加了一个加号,以便将xDir值添加到xDirection中。)

public void setXDirection(int xDir)
{
     xDirection += xDir
}

使用yDirection进行相同的校正(yDirection + = yDir)

其次,你不要调用你的paint方法。每次用户按下一个键(当然是其中一个键)时都必须调用它,所以在keyReleased方法的最后一行进行调用。

我希望这两个代码能够纠正您的代码,但我认为您需要仔细地重新检查代码。

祝你好运,

伊曼

答案 1 :(得分:1)

您忘了将Runnable实例添加到Thread构造函数。

您的主要方法应该是:

public static void main(String[] args) {
    TestApplication ta = new TestApplication();
    //Threads
    Thread t1 = new Thread(ta);
    t1.start();
}