MouseEvent取消

时间:2016-09-06 14:55:14

标签: java mouseevent chess

我正在创建一个MouseEvent,它似乎在启动后取消了几个滴答,因为我有时会在它停止响应之前获得一个小的响应。有谁看到原因?
这是我在init()

中初始化的地方
public class Game implements Runnable {

private Display display;
private int width, height;
public String title;

private boolean running = false;
private Thread thread;

private BufferStrategy bs;
private Graphics g;

//Input
Mouse mouse;

//Handler
private Handler handler;

public Game(String title, int width, int height){
    this.title = title;
    this.width = width;
    this.height = height;

    mouse = new Mouse();
}

public synchronized void start(){
    if(running)
        return;
    running = true;
    thread = new Thread(this);
    thread.start();
}
public synchronized void stop(){
    if(!running)
        return;
    running = false;
    try {
        thread.join();
    } 
    catch (InterruptedException e){ e.printStackTrace(); }
}

public void run() {
    init();

    int fps = 60;
    double timePerTick = 1000000000 / fps;
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();
    long timer = 0;
    int ticks = 0;

    while(running){
        now = System.nanoTime();
        delta += (now - lastTime) / timePerTick;
        timer += now - lastTime;
        lastTime = now;

        if(delta >= 1){

            tick();
            render();
            ticks++;
            delta--;
        }

        if(timer >= 1000000000){
            ticks = 0;
            timer = 0;
        }
    }

    stop();
}

private void init() {
        display = new Display(title, width, height);

        display.getFrame().addMouseListener(mouse);

        Assets.init();

        handler = new Handler(this);
}

private void tick() {
    Tile tile = new Tile();
    tile.tick();
}

private void render() {
    bs = display.getCanvas().getBufferStrategy();
    if(bs == null){
        display.getCanvas().createBufferStrategy(3);
        return;
    }
    g = bs.getDrawGraphics();
    g.clearRect(0, 0, width, height); //Clears the Screen
    //Start of Drawing

    ChessBoard cb = new ChessBoard(64);
    cb.render(g);
    cb.tick();

    //End of Drawing
    bs.show();
    g.dispose();
}

}

这是MouseEvent

public class Mouse implements MouseListener {

    public void mousePressed(MouseEvent e) {
       System.out.println("1");
    }

    public void mouseReleased(MouseEvent e) {
           System.out.println("2");
    }

    public void mouseEntered(MouseEvent e) {
           System.out.println("3");
    }

    public void mouseExited(MouseEvent e) {
           System.out.println("4");
    }

    public void mouseClicked(MouseEvent e) {
           System.out.println("5");
    }
}

0 个答案:

没有答案