JavaFx 形状始终相交 returns false/true
JavaFx Shape Intersect always returns false/true
所以,我创建了 2 个矩形并检查它们是否相交但每次都得到不同的结果:
Rectangle a = new Rectangle( 10.00, 10.00 );
Rectangle b = new Rectangle( 30.00, 20.00 );
a.setFill(Color.RED);
_centerPane.getChildren().add(a);
_centerPane.getChildren().add(b);
//both at 0.00
System.out.println( a.intersects( b.getLayoutX(), b.getLayoutY(), b.getWidth(), b.getHeight()) ); //true
System.out.println( b.intersects( a.getLayoutX(), a.getLayoutY(), a.getWidth(), a.getHeight()) ); //true
a.setLayoutX( 100.00);
a.setLayoutY(100.00);
//only a at different position and not visually intersecting
System.out.println( a.intersects( b.getLayoutX(), b.getLayoutY(), b.getWidth(), b.getHeight()) ); //true
System.out.println( b.intersects( a.getLayoutX(), a.getLayoutY(), a.getWidth(), a.getHeight()) ); //false
b.setLayoutX( 73.00);
b.setLayoutY(100.00);
//Now b is set near a and intersects a visually
System.out.println( a.intersects( b.getLayoutX(), b.getLayoutY(), b.getWidth(), b.getHeight()) ); //false
System.out.println( b.intersects( a.getLayoutX(), a.getLayoutY(), a.getWidth(), a.getHeight()) ); //false
这是因为您将 layoutX
和 layoutY
与 x
和 y
混在一起了。如果你 运行 下面的代码(我修改了你的原始代码并添加了一些打印语句)你会看到虽然你在调用 [=17= 时改变了 layoutX
和 layoutY
] 它正在测试位于 (0,0) 且大小为 (10,10) 的 a
是否与位于 (100,100) 的矩形相交,答案当然是否定的。
不要使用 setLayoutX
和 getLayoutX
(或 Y
),只需使用 setX
/getX
和 setY
/getY
.
public static void test(Rectangle a, Rectangle b) {
System.out.println( a.intersects( b.getLayoutX(), b.getLayoutY(), b.getWidth(), b.getHeight()) );
System.out.println( b.intersects( a.getLayoutX(), a.getLayoutY(), a.getWidth(), a.getHeight()) );
}
public static void print(String name, Rectangle r) {
System.out.println(name + " : " + r.toString() + " layoutX: " + r.getLayoutX() + " layoutY: " + r.getLayoutY());
}
public static void main(String[] args) {
Rectangle a = new Rectangle( 10.00, 10.00 );
Rectangle b = new Rectangle( 30.00, 20.00 );
//both at 0.00
print("a", a);
print("b", b);
test(a, b); // true, true
a.setLayoutX(100.00);
a.setLayoutY(100.00);
//only a at different position and not visually intersecting
print("a", a);
print("b", b);
test(a, b); // true, false
b.setLayoutX( 73.00);
b.setLayoutY(100.00);
//Now b is set near a and intersects a visually
print("a", a);
print("b", b);
test(a, b); // false, false
}
所以,我创建了 2 个矩形并检查它们是否相交但每次都得到不同的结果:
Rectangle a = new Rectangle( 10.00, 10.00 );
Rectangle b = new Rectangle( 30.00, 20.00 );
a.setFill(Color.RED);
_centerPane.getChildren().add(a);
_centerPane.getChildren().add(b);
//both at 0.00
System.out.println( a.intersects( b.getLayoutX(), b.getLayoutY(), b.getWidth(), b.getHeight()) ); //true
System.out.println( b.intersects( a.getLayoutX(), a.getLayoutY(), a.getWidth(), a.getHeight()) ); //true
a.setLayoutX( 100.00);
a.setLayoutY(100.00);
//only a at different position and not visually intersecting
System.out.println( a.intersects( b.getLayoutX(), b.getLayoutY(), b.getWidth(), b.getHeight()) ); //true
System.out.println( b.intersects( a.getLayoutX(), a.getLayoutY(), a.getWidth(), a.getHeight()) ); //false
b.setLayoutX( 73.00);
b.setLayoutY(100.00);
//Now b is set near a and intersects a visually
System.out.println( a.intersects( b.getLayoutX(), b.getLayoutY(), b.getWidth(), b.getHeight()) ); //false
System.out.println( b.intersects( a.getLayoutX(), a.getLayoutY(), a.getWidth(), a.getHeight()) ); //false
这是因为您将 layoutX
和 layoutY
与 x
和 y
混在一起了。如果你 运行 下面的代码(我修改了你的原始代码并添加了一些打印语句)你会看到虽然你在调用 [=17= 时改变了 layoutX
和 layoutY
] 它正在测试位于 (0,0) 且大小为 (10,10) 的 a
是否与位于 (100,100) 的矩形相交,答案当然是否定的。
不要使用 setLayoutX
和 getLayoutX
(或 Y
),只需使用 setX
/getX
和 setY
/getY
.
public static void test(Rectangle a, Rectangle b) {
System.out.println( a.intersects( b.getLayoutX(), b.getLayoutY(), b.getWidth(), b.getHeight()) );
System.out.println( b.intersects( a.getLayoutX(), a.getLayoutY(), a.getWidth(), a.getHeight()) );
}
public static void print(String name, Rectangle r) {
System.out.println(name + " : " + r.toString() + " layoutX: " + r.getLayoutX() + " layoutY: " + r.getLayoutY());
}
public static void main(String[] args) {
Rectangle a = new Rectangle( 10.00, 10.00 );
Rectangle b = new Rectangle( 30.00, 20.00 );
//both at 0.00
print("a", a);
print("b", b);
test(a, b); // true, true
a.setLayoutX(100.00);
a.setLayoutY(100.00);
//only a at different position and not visually intersecting
print("a", a);
print("b", b);
test(a, b); // true, false
b.setLayoutX( 73.00);
b.setLayoutY(100.00);
//Now b is set near a and intersects a visually
print("a", a);
print("b", b);
test(a, b); // false, false
}