MouseListener没有动静

时间:2014-01-14 18:34:40

标签: java swing mouseevent

我一直在学习如何使用java中的RPG风格游戏,并找到了一个很棒的教程。但是,本教程展示了如何使用键侦听器移动精灵,我想要做的是通过在屏幕上获取鼠标位置来移动精灵,并在没有任何键侦听器的情况下自行移动精灵。继承了我到目前为止的代码,感谢您的帮助! (哦,问题是精灵没有移动,忘了说......是因为移动方法中的while循环?)

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

import javax.swing.ImageIcon;

public class SpaceCraft {

    private String craft = "craft.png";

    private int dx;
    private int dy;
    private int x;
    private int y;
    private Image image;

    public SpaceCraft() {
        ImageIcon spaceCraft = new ImageIcon(this.getClass().getResource(craft));
        image = spaceCraft.getImage();
        x = 40;
        y = 60;
    }

    public void move() {
        while ((x==Board.mouseX)==false) {
            if (x < Board.mouseX){
                x += 1;
            } else if (x > Board.mouseX){
                x -= 1;
            }
        }
        while ((y==Board.mouseY)==false) {
            if (y < Board.mouseY){
                y += 1;
            } else if (y > Board.mouseY){
                y -= 1;
            }
        }

    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Image getImage() {
        return image;
    }



   /* public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT){
            dx = -1;
        }
        if (key == KeyEvent.VK_RIGHT) {
            dx = 1;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -1;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 1;
        }

    }*/

   /* public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }*/


}


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.*;

import javax.swing.*;

import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {

    public static int mouseX;
    public static int mouseY;
    private Timer timer;
    private SpaceCraft craft;

    public Board() {
        //addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.GREEN);
        setDoubleBuffered(true);

        craft = new SpaceCraft();

        timer = new Timer(5, this);
        timer.start();
    }

    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    public void actionPerformed(ActionEvent e) {
        craft.move();
        repaint();
    }
    public void mouseReleased(MouseEvent e) {
        mouseX = e.getXOnScreen();
        mouseY = e.getYOnScreen();
        craft.move();
        repaint();
    }

    /*private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            craft.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            craft.keyPressed(e);
        }
    }*/



}

import javax.swing.*;
import java.applet.*;
import java.awt.Dimension;
import java.net.*;

public class RType extends Applet {
    Board b = new Board();

    public void init() {
        b.setPreferredSize(new Dimension(400, 400));
        add(b);

    }

}

3 个答案:

答案 0 :(得分:2)

您必须将MouseListener添加到面板Board

更改

public class Board extends JPanel implements ActionListener {

public class Board extends JPanel implements ActionListener,MouseListener  {

在构造函数中添加

addMouseListener(this);

答案 1 :(得分:1)

你需要一次移动你的精灵,画上每一步。你现在这样做的方式,你的精灵移动时没有任何绘画,所以它看起来就像是传送到鼠标位置。

您还必须实际添加MouseListener,而不仅仅是编写MouseListener方法。

答案 2 :(得分:0)

你可以采取几种方法。

首先,您可以在鼠标事件上调用actionPerformed(ActionEvent e)时记录当前位置,并跟踪当前的X和Y.

然后你只需要做到这一点,以便在游戏循环中,现在必须在paint()中,你将X和Y应用到工艺中。

所以添加一个对craft.move()的调用;在paint()

然后,如果你想让它变得更好,而不是记录actionPerformed事件,你可以在调用craft.move()时直接检查鼠标的位置。

PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();

是一种方法,虽然我认为这会给你绝对的坐标,而你想要它们相对于你的窗口,对吗?

Display.getCurrent().getCursorLocation(); 

MouseInfo.getPointerInfo();

虽然这只是将飞行器精确地移动到鼠标位置。如果您希望它具有最大速度并转向鼠标位置,则需要使您的craft.move()方法更加智能化。