所以我一直在研究Applet并在applet上使用Canvas和BufferStrategy。我已经让它缓冲并正确显示但我认为它以某种方式干扰了MouseListener。当我运行Applet并重新调整屏幕大小时,MouseListener在我指定的画布之外工作。因此,当我将鼠标移动到绘图位置时,鼠标坐标会冻结。然后,当我将鼠标从画布上移开时,它可以工作。因此MouseListener没有任何问题。无论如何我会把它包括在内。
MouseMotion类:
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”继续进行游戏循环增加,所以我知道它仍然在运行。但是,当鼠标退出框时,坐标会在我围绕窗口移动时再次开始变化。
我试着跟随其他人的例子,我相信我有。也许我的方法是错的,我不知道,但任何帮助,答案,评论,提示任何事情都非常感激。