Java 和 Processing 3.0 “frame” class 已弃用,是否有替代方案?
Java and Processing 3.0 “frame” class deprecated, is there an alternative?
所以 Processing 曾经有一个 "frame" class 允许一些通用的功能。我试图在屏幕上获取 PApplet window 的位置,但没有 "frame" class,我似乎无法做到。
旧方法:
int fX = frame.getX();
or
int fX = frame.getLocationOnScreen().x;
整个事情应该是动态的,所以在 Processings setup() 中设置 window 位置不是一个选项。
希望有人能帮忙,
您好!
B
您最好只使用旧的 frame
变量。
对于像这样的大多数事情,您应该使用类型为 PSurface
的新 surface
变量。您可以查看 PSurface
here 的来源。读到这里,我们看到不幸的是,surface
变量不允许我们访问框架的位置。
由于无法从surface
变量中获取位置,所以必须更深入地获取native组件(默认情况下,一个 SmoothCanvas
扩展了一个 awt Canvas
)。我通过查看 PSurfaceAWT
available here.
的来源来解决这个问题
该代码看起来有点粗糙,因为您必须进行一些转换:
import processing.awt.PSurfaceAWT;
import processing.awt.PSurfaceAWT.SmoothCanvas;
void setup(){
size(200, 200);
}
void draw(){
background(0);
int x = ( (SmoothCanvas) ((PSurfaceAWT)surface).getNative()).getFrame().getX();
text("x: " + x , 20, 20);
}
除了 Kevin 的出色回答之外,还有一个函数将其与 P2D
、P3D
和 FX2D
渲染器的等效函数一起使用:
void setup(){
size(300,300,FX2D);
}
void draw(){
background(0);
text(getLocationOnScreen().toString(),10,15);
}
PVector getLocationOnScreen(){
PVector location = new PVector();
// JAVA2D
if(surface instanceof processing.awt.PSurfaceAWT){
java.awt.Frame frame = ( (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative()).getFrame();
java.awt.Point point = frame.getLocationOnScreen();
location.set(point.x,point.y);
}
// P2D, P3D
else if(surface instanceof processing.opengl.PSurfaceJOGL){
com.jogamp.newt.opengl.GLWindow window = (com.jogamp.newt.opengl.GLWindow)(((PSurfaceJOGL)surface).getNative());
com.jogamp.nativewindow.util.Point point = window.getLocationOnScreen(new com.jogamp.nativewindow.util.Point());
location.set(point.getX(),point.getY());
}
// FX2D
else if(surface instanceof processing.javafx.PSurfaceFX){
javafx.scene.canvas.Canvas canvas = (javafx.scene.canvas.Canvas)((processing.javafx.PSurfaceFX)surface).getNative();
javafx.geometry.Point2D point = canvas.localToScreen(0,0);
location.set((float)point.getX(),(float)point.getY());
}
return location;
}
(代码代码使用导入更优雅,但因为我保留了完全限定的 class 名称,所以在新草图中更容易 copy/paste/test 函数)
所以 Processing 曾经有一个 "frame" class 允许一些通用的功能。我试图在屏幕上获取 PApplet window 的位置,但没有 "frame" class,我似乎无法做到。
旧方法:
int fX = frame.getX();
or
int fX = frame.getLocationOnScreen().x;
整个事情应该是动态的,所以在 Processings setup() 中设置 window 位置不是一个选项。
希望有人能帮忙,
您好! B
您最好只使用旧的 frame
变量。
对于像这样的大多数事情,您应该使用类型为 PSurface
的新 surface
变量。您可以查看 PSurface
here 的来源。读到这里,我们看到不幸的是,surface
变量不允许我们访问框架的位置。
由于无法从surface
变量中获取位置,所以必须更深入地获取native组件(默认情况下,一个 SmoothCanvas
扩展了一个 awt Canvas
)。我通过查看 PSurfaceAWT
available here.
该代码看起来有点粗糙,因为您必须进行一些转换:
import processing.awt.PSurfaceAWT;
import processing.awt.PSurfaceAWT.SmoothCanvas;
void setup(){
size(200, 200);
}
void draw(){
background(0);
int x = ( (SmoothCanvas) ((PSurfaceAWT)surface).getNative()).getFrame().getX();
text("x: " + x , 20, 20);
}
除了 Kevin 的出色回答之外,还有一个函数将其与 P2D
、P3D
和 FX2D
渲染器的等效函数一起使用:
void setup(){
size(300,300,FX2D);
}
void draw(){
background(0);
text(getLocationOnScreen().toString(),10,15);
}
PVector getLocationOnScreen(){
PVector location = new PVector();
// JAVA2D
if(surface instanceof processing.awt.PSurfaceAWT){
java.awt.Frame frame = ( (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative()).getFrame();
java.awt.Point point = frame.getLocationOnScreen();
location.set(point.x,point.y);
}
// P2D, P3D
else if(surface instanceof processing.opengl.PSurfaceJOGL){
com.jogamp.newt.opengl.GLWindow window = (com.jogamp.newt.opengl.GLWindow)(((PSurfaceJOGL)surface).getNative());
com.jogamp.nativewindow.util.Point point = window.getLocationOnScreen(new com.jogamp.nativewindow.util.Point());
location.set(point.getX(),point.getY());
}
// FX2D
else if(surface instanceof processing.javafx.PSurfaceFX){
javafx.scene.canvas.Canvas canvas = (javafx.scene.canvas.Canvas)((processing.javafx.PSurfaceFX)surface).getNative();
javafx.geometry.Point2D point = canvas.localToScreen(0,0);
location.set((float)point.getX(),(float)point.getY());
}
return location;
}
(代码代码使用导入更优雅,但因为我保留了完全限定的 class 名称,所以在新草图中更容易 copy/paste/test 函数)