java - 鼠标点击坐标始终为0

标签 java mouselistener java-canvas

如您所见,我向游戏中添加了鼠标监听器。

import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class Game implements Runnable
{
private Display display;
public int width, height;
public String title;

private boolean running = false;
private Thread thread;

private BufferStrategy bs;
private Graphics g;

// States
public static State gameState;

// Input
private InputManager inputManager;
private MouseHandler mouseHandler;

public Game(String title, int width, int height)
{
    this.width = width;
    this.height = height;
    this.title = title;
    inputManager = new InputManager();
    mouseHandler = new MouseHandler();
}

/**
 * Initializes all of the graphics
 */
private void initialize()
{
    display = new Display(title, width, height);
    display.getFrame().addKeyListener(inputManager);
    display.getFrame().addMouseListener(mouseHandler);
    Assets.loadAssets();

    gameState = new GameState(this, 1);
    State.setState(gameState);
}

/**
 * Updates everything in the game loop
 */
private void update()
{
    if (State.getState() != null)
        State.getState().update();
}

private void render()
{
    bs = display.getCanvas().getBufferStrategy();

    // If this is the first time running initialize the buffer strategy
    if (bs == null)
    {
        display.getCanvas().createBufferStrategy(3);
        return;
    }

    g = bs.getDrawGraphics();
    // Clear the screen
    g.clearRect(0, 0, width, height);

    // Drawing
    if (State.getState() != null)
        State.getState().render(g);
    // End drawing
    bs.show();
    g.dispose();
}

public void run()
{

    initialize();

    // Updates the game loop 60 times every 1 second = 1,000,000,000
    // nanoseconds
    int fps = 60;
    double timePerUpdate = 1000000000 / fps;
    double timeElapsed = 0;
    long now;
    // Current time of computer in nanoseconds
    long lastTime = System.nanoTime();

    // Game loop
    while (running)
    {
        now = System.nanoTime();
        timeElapsed += (now - lastTime) / timePerUpdate;
        lastTime = now;

        if (timeElapsed >= 1)
        {
            update();
            render();
            timeElapsed--;
        }
    }

    stop();
}

public synchronized void start()
{
    // Do not make a new thread if it is already running
    if (running)
        return;

    // Starts the game
    running = true;
    thread = new Thread(this); // this = Game class
    thread.start(); // Goes to run
}

public synchronized void stop()
{
    // In case stop gets called and it is already not running
    if (!running)
        return;

    // Stops the game
    running = false;
    try
    {
        thread.join();
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
}

public InputManager getInputManager()
{
    return inputManager;
}

public MouseHandler getMouseHandler()
{
    return mouseHandler;
}

public static void main(String[] args)
{
    Game game = new Game("Game", 1024, 768);
    game.start();
}
}

这是我的鼠标适配器类,基本上我想要的只是按下鼠标的 x,y 坐标

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseHandler extends MouseAdapter
{
public int x, y;

public MouseHandler()
{

}

public void MousePressed(MouseEvent e)
{
    x = e.getX();
    y = e.getY();
}
}

当我尝试获取 X 和 Y 坐标时,它们始终为 0,而不是鼠标实际单击的位置。我不知道为什么。

最佳答案

public int x, y;

int 变量始终初始化为 0。

public void MousePressed(MouseEvent e)

方法名称区分大小写。

您的代码永远不会执行,因为要重写的方法应该是mousePressed(...)。修复代码中的拼写错误。

始终使用如下代码:

@Override
public void mousePressed(MouseEvent e)

如果你输入错误,编译器会告诉你。

When I try to get the X, and Y coordinates they are always 0

由于您的代码从未执行,因此返回默认值。

关于java - 鼠标点击坐标始终为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747050/

相关文章:

java - 倒坐标系中直线和矩形的直线裁剪算法?

java - 同一 JVM 中 DataNucleus JDO 的多个实例

java - 正在寻找一个允许动态更改 Java 中的图形/树的开源可视化库?

java - 鼠标滚轮闪烁,更多关于原因

java - MouseListener - 不适用于 JTextArea

java - 如何在 gwt 中创建此类图像?

java.awt.image.DataBufferByte 无法转换为 java.awt.image.DataBufferInt

java - 如何保存过滤器参数。分页

java - 创建 FileReader 哪种方式更适合优化?

Java MouseListener MouseClicked 只能工作一次