本演示将通过使用 MouseInfo 获取光标显示坐标来停止颤动。 x 和 y 偏移量是用户最初单击窗口时获得的光标 window 坐标。从光标显示坐标中减去后者可防止窗口移动到单击点的位置。您可以比较两个版本以查看差异。 “println”调用有望让您了解正在发生的事情,以后可能会被删除。
import java.awt.MouseInfo;
int x=100, y=100;
boolean moving=false;
void setup() {
fullScreen();
surface.setSize(300, 300);
surface.setLocation(x, y);
}
void draw() {
background(0);
if (moving) {
x = MouseInfo.getPointerInfo().getLocation().x;
y = MouseInfo.getPointerInfo().getLocation().y;
}
surface.setLocation(x, y);
fill(255);
ellipse(width/2, height/2, 100, 100);
}
void mousePressed() {
moving=true;
}
void mouseReleased() {
moving=false;
}
改进版如下:
import java.awt.MouseInfo;
int x=100, y=100;
boolean dragged = false;
int xOffset = 0;
int yOffset = 0;
void setup() {
fullScreen();
surface.setSize(300, 300);
surface.setLocation(x, y);
}
void draw() {
background(0);
if (dragged) {
x = MouseInfo.getPointerInfo().getLocation().x - xOffset;
y = MouseInfo.getPointerInfo().getLocation().y - yOffset;
}
surface.setLocation(x, y);
fill(255);
ellipse(width/2, height/2, 100, 100);
}
void mouseDragged() {
dragged = true;
println("dragged = ", dragged);
}
void mousePressed() {
println("mouseX " + ":" + "mouseY = ", mouseX, mouseY);
xOffset = mouseX;
yOffset = mouseY;
}
void mouseReleased() {
dragged = false;
println("dragged = ", dragged);
}