下面的 (Scala) 代码产生错误的结果(即 [x = 0.0, y = 0.0]
)。
为什么?
如何解决?
根据JavaDoc此代码应打印 Circle
的 x
和 y
坐标 50, 80
。
object CircleTestLauncher extends App{
Application.launch(classOf[CircleTest])
}
class CircleTest extends Application with App
{
override def start(p1: Stage): Unit = {
val c1= new Circle(50,80,10)
val sp=new Group
sp.getChildren.add(c1)
p1.setScene(new Scene(sp,300,300))
p1.show()
println("in start method, scene coord. of circle ="+c1.localToScene(Point2D.ZERO))
}
}
打印:
in start method, scene coord. of circle =Point2D [x = 0.0, y = 0.0]
编辑:
然而,根据this,接受的答案解决了问题博客文章我的解决方案也应该有效,问题仍然存在:为什么上面的代码不起作用?
两个坐标(getCenter 与 localToScene)之间有什么区别?
localToScene 到底有什么用?
我用谷歌搜索了一下,发现这方面的信息很少。
JavaFX 书籍也没有解释这一点。
最佳答案
我不确定它在 Scala 中是如何完成的,但在 java 中,以下代码工作正常
System.out.println("X :" +c1.getCenterX()+ " Y: "+c1.getCenterY());
输出为
X :50.0 Y: 80.0
关于java - 如何获取 JavaFX 8 中节点的场景坐标? localToScene 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22835299/