在处理 Java 中移动无边界 window

move borderless window in Processing Java

我希望此草图 运行 没有上 window 边框,并且我希望能够移动 window 而不会出现我现在遇到的奇怪晃动,并且仅此而已按下鼠标时。这是我的代码:

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+=mouseX-pmouseX;
  y+=mouseY-pmouseY;
 }
 surface.setLocation(x, y);

 fill(255);
 ellipse(width/2, height/2, 100, 100);
}

void mousePressed() {
 moving=true;
}

void mouseReleased() {
 moving=false;
}

问题是,我感到奇怪的颤抖,我不知道 fullScreen() 技术是否是最好的方法...

尽管我是 window 管理的新手,请随意建议对代码进行重大更改。

此演示将通过使用 MouseInfo 获取光标 显示 坐标来停止抖动。 x 和 y 偏移量是用户最初单击 window 时获得的光标 window 坐标。从光标显示坐标中减去后者可防止 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);
}