【问题标题】:move borderless window in Processing Java在处理 Java 中移动无边框窗口
【发布时间】:2022-01-19 19:35:21
【问题描述】:

我希望这个草图在没有上窗口边框的情况下运行,并且我希望能够移动窗口而不会出现我现在得到的奇怪震动,并且只有在按下鼠标时。这是我的代码:

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() 技术是否是最好的方法......

请随意建议对代码进行重大更改,因为我是窗口管理的新手。

【问题讨论】:

    标签: java processing


    【解决方案1】:

    本演示将通过使用 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);
    }
    
    

    【讨论】:

    • 编辑添加 x,y 偏移量,以防止最初点击时窗口移动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2018-04-18
    相关资源
    最近更新 更多