Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 理解基本的KeyListener代码_Java_Input_Key_Keylistener_Pressed - Fatal编程技术网

Java 理解基本的KeyListener代码

Java 理解基本的KeyListener代码,java,input,key,keylistener,pressed,Java,Input,Key,Keylistener,Pressed,嘿,伙计们,我是java游戏编程新手。我正在学习David Brackeen的《用java开发游戏》一书中的教程。它很古老,在第三章中有一个使用KeyListener的示例。它似乎不起作用,尽管它甚至没有注册一个密钥事件。这里发生的是代码 import java.awt.*; import java.awt.event.*; import javax.swing.SwingUtilities; import com.brackeen.javagamebook.graphics.*; impor

嘿,伙计们,我是java游戏编程新手。我正在学习David Brackeen的《用java开发游戏》一书中的教程。它很古老,在第三章中有一个使用KeyListener的示例。它似乎不起作用,尽管它甚至没有注册一个密钥事件。这里发生的是代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;

import com.brackeen.javagamebook.graphics.*;
import com.brackeen.javagamebook.test.GameCore;

/**
    A simple mouselook test. Using mouselook, the user can
    virtually move the mouse in any direction indefinitly.
    Without mouselook, the mouse stops when it hits the edge of
    the screen.
    <p>Mouselook works by recentering the mouse whenever it is
    moved, so it can always measure the relative mouse movement,
    and the mouse never hits the edge of the screen.
*/
public class MouselookTest extends GameCore
    implements MouseMotionListener, KeyListener
{

    public static void main(String[] args) {
        new MouselookTest().run();
    }

    private Image bgImage;
    private Robot robot;
    private Point mouseLocation;
    private Point centerLocation;
    private Point imageLocation;
    private boolean relativeMouseMode;
    private boolean isRecentering;

    public void init() {
        super.init();
        mouseLocation = new Point();
        centerLocation = new Point();
        imageLocation = new Point();

        relativeMouseMode = true;
        isRecentering = false;

        try {
            robot = new Robot();
            recenterMouse();
            mouseLocation.x = centerLocation.x;
            mouseLocation.y = centerLocation.y;
        }
        catch (AWTException ex) {
            System.out.println("Couldn't create Robot!");
        }
        Window window = screen.getFullScreenWindow();
        window.addMouseMotionListener(this);
        window.addKeyListener(this);
        bgImage = loadImage("images/background.jpg");
    }


    public synchronized void draw(Graphics2D g) {

        int w = screen.getWidth();
        int h = screen.getHeight();

        // make sure position is correct
        imageLocation.x %= w;
        imageLocation.y %= screen.getHeight();
        if (imageLocation.x < 0) {
            imageLocation.x += w;
        }
        if (imageLocation.y < 0) {
            imageLocation.y += screen.getHeight();
        }

        // draw the image in four places to cover the screen
        int x = imageLocation.x;
        int y = imageLocation.y;
        g.drawImage(bgImage, x, y, null);
        g.drawImage(bgImage, x-w, y, null);
        g.drawImage(bgImage, x, y-h, null);
        g.drawImage(bgImage, x-w, y-h, null);

        // draw instructions
        g.setRenderingHint(
                RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.drawString("Press Space to change mouse modes.", 5,
            FONT_SIZE);
        g.drawString("Press Escape to exit.", 5, FONT_SIZE*2);
    }


    /**
        Uses the Robot class to try to postion the mouse in the
        center of the screen.
        <p>Note that use of the Robot class may not be available
        on all platforms.
    */
    private synchronized void recenterMouse() {
        Window window = screen.getFullScreenWindow();
        if (robot != null && window.isShowing()) {
            centerLocation.x = window.getWidth() / 2;
            centerLocation.y = window.getHeight() / 2;
            SwingUtilities.convertPointToScreen(centerLocation,
                window);
            isRecentering = true;
            robot.mouseMove(centerLocation.x, centerLocation.y);
        }
    }


    // from the MouseMotionListener interface
    public void mouseDragged(MouseEvent e) {
        mouseMoved(e);
    }


    // from the MouseMotionListener interface
    public synchronized void mouseMoved(MouseEvent e) {
        // this event is from re-centering the mouse - ignore it
        if (isRecentering &&
            centerLocation.x == e.getX() &&
            centerLocation.y == e.getY())
        {
            isRecentering = false;
        }
        else {
            int dx = e.getX() - mouseLocation.x;
            int dy = e.getY() - mouseLocation.y;
            imageLocation.x += dx;
            imageLocation.y += dy;
            // recenter the mouse
            if (relativeMouseMode) {
                recenterMouse();
            }

        }

        mouseLocation.x = e.getX();
        mouseLocation.y = e.getY();

    }


    // from the KeyListener interface
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            // exit the program
            stop();
        }
        else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            // change relative mouse mode
            relativeMouseMode = !relativeMouseMode;
        }
    }


    // from the KeyListener interface
    public void keyReleased(KeyEvent e) {
        // do nothing
    }


    // from the KeyListener interface
    public void keyTyped(KeyEvent e) {
        // do nothing
    }

}

您可以尝试将代码示例分解为几行?这不仅能改善你的问题,而且能增加你很快得到好答案的机会?我真的需要一些帮助,我不知道你是否意识到这一点,但也不是一个论坛,在那里,一个肿块把你带到一个线程列表;它主要是通知那些之前评论说是我的人。你没有得到答案是因为我在第一次评论中试图解释的问题,不是因为没有人看到你的问题。如果您在理解该评论时遇到问题,请随时要求澄清?我的错,我是新来的,我将编辑代码!感谢您提供的信息: