0

所以一直在研究小程序,在小程序上使用Canvas和BufferStrategy。我已经让它缓冲并正确显示,但我认为它以某种方式干扰了 MouseListener。当我运行 Applet 并重新调整屏幕大小时,MouseListener 在我指定的画布之外工作。因此,当我将鼠标移到它正在绘制的位置时,鼠标坐标会冻结。然后,当我将鼠标移出画布时,它就可以工作了。所以MouseListener没有错。无论如何我都会包括它。

鼠标运动类:

public class MouseMotion implements MouseMotionListener{
    private int x;
    private int y;
    public MouseMotion()
    {
        x = 0;
        y = 0;
        System.out.println("MouseMotion initialized");
    }

    public void mouseDragged(MouseEvent e) {}

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

    //Return methods...
}

鼠标类:

public class Mouse implements MouseListener{
    private int clickedX;
    private int clickedY;
    private int clickedButton;

    private boolean pressed;
    private int pressedX;
    private int pressedY;
    private int pressedButton;

    public Mouse()
    {
        //Instantiates all the variables
        System.out.println("Mouse initialized");
    }

    public void mouseClicked(MouseEvent e) {
        clickedX = e.getX();
        clickedY = e.getY();
        clickedButton = e.getButton();
    }

    public void mousePressed(MouseEvent e) {
        pressed = true;
        pressedX = e.getX();
        pressedY = e.getY();
        pressedButton = e.getButton();
    }

    public void mouseReleased(MouseEvent e) {
        pressed = false;
        pressedX = 0;
        pressedY = 0;
        pressedButton = 0;
    }

    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}

    //Methods that return each variable...
}

AppletMain 类:

    public class AppletMain extends Applet implements Runnable{

    //Constants
    private final int WIDTH = 720;
    private final int HEIGHT = WIDTH / 16 * 9;

    //Applet objects
    private BufferStrategy bs;
    private Canvas canvas;

    //Game loop variables
    ...

    //Engine objects
    private Mouse mouse;
    private MouseMotion mouseMotion;

    public void init()
    {
        canvas = new Canvas();

        mouse = new Mouse();
        mouseMotion = new MouseMotion();
        addMouseListener(mouse);
        addMouseMotionListener(mouseMotion);
    }

    public void render()
    {
        //Setup graphics
        Graphics2D g = (Graphics2D)bs.getDrawGraphics();

        g.clearRect(0,0,WIDTH,HEIGHT);

        //Just prints out the debugging stuff
        g.setColor(Color.BLACK);
        g.drawRect(0,0,WIDTH-1,HEIGHT-1);
        g.drawString("TPS: " + Integer.toString(tps) + " FPS: " + Integer.toString(fps), 2,20);
        g.drawString("MouseMotion: X - " + Integer.toString(mouseMotion.getX()) + " Y - " + Integer.toString(mouseMotion.getY()), 2,40);
        g.drawString("Mouse(PRESSED): X - " + Integer.toString(mouse.getPressedX()) + " Y - " + Integer.toString(mouse.getPressedY()) + " button - " + Integer.toString(mouse.getPressedButton()), 2,60);
        g.drawString("Mouse(CLICKED): X - " + Integer.toString(mouse.getClickedX()) + " Y - " + Integer.toString(mouse.getClickedY()) + " button - " + Integer.toString(mouse.getClickedButton()), 2,80);

        //Cleanup graphics
        g.dispose();
        bs.show();
    }

    public void tick()
    {
        //Ticks the game
    }

    public void start()
    {
        if(bs == null)
        {
            setIgnoreRepaint(true);
            canvas.setIgnoreRepaint(true);
            canvas.setSize(WIDTH,HEIGHT);
            setLayout(null);
            add(canvas);
            canvas.setLocation(0,0);

            canvas.createBufferStrategy(3);
            bs = canvas.getBufferStrategy();
        }

        new Thread(this).start();

        canvas.requestFocus();
    }

    public void stop()
    {
        running = false;
    }

    public void destroy()
    {
        if(running)
            running = false;
    }

    public void run()
    {
        init();
        //Game loop that calls tick() and render() 60 times per second
    }
}

在我的代码中,我在画布周围绘制了黑框,正如我之前所说,当我的鼠标进入黑框时,坐标停止变化。“fps”和“tps”继续运行,游戏循环增加,所以我知道它仍在运行。但是,当鼠标退出框时,当我在窗口周围移动时,坐标再次开始变化。

我试图效仿其他人的榜样,我相信我做到了。也许我对此的方法都是错误的,我不知道,但非常感谢任何帮助、答案、评论、提示。

4

0 回答 0