我想编写一个在scalafx图表的2个数据节点之间画一条线的函数。 我有一个散点图,其中使用自定义scalafx区域设置数据的节点(因为我还希望数据的样式和可悬停的)。 该图表采用简单的窗格布局。
我的函数addLine将2个节点作为参数,获取两个节点在窗格透视图中的位置,创建一条线,然后将其添加到窗格中。
我的问题是,线没有连接2个节点,节点与线的起点和终点之间存在间隙。我不明白我在哪里做错了...
我的代码:
package oscar.visualfx.routing
import java.util.concurrent.{LinkedBlockingQueue, ThreadPoolExecutor,TimeUnit}
import org.apache.commons.lang.time.StopWatch
import oscar.visualfx.VisualFrameScalaFX
import oscar.visualfx.wlp.DeliveryStore
import scalafx.application.{JFXApp, Platform}
import scalafx.geometry.Point2D
import scalafx.scene.Node
import scalafx.scene.chart.XYChart.{Data, Series}
import scalafx.scene.chart.{NumberAxis, ScatterChart}
import scalafx.scene.layout.Pane
import scalafx.scene.paint.Color
import scalafx.scene.shape.Line
class RoutingWindow(nodesPosition: Array[(Int,Int)], vehicles: Range) extends VisualFrameScalaFX("routing") {
var vehiclesColor: Seq[Color] = Seq()
val watch = new StopWatch
val TPE = new ThreadPoolExecutor(1, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue[Runnable])
val offset = 50
val Xmax: Int = nodesPosition.map(_._1).max + offset
val Xmin: Int = nodesPosition.map(_._1).min - offset
val Ymax: Int = nodesPosition.map(_._2).max + offset
val Ymin: Int = nodesPosition.map(_._2).min - offset
val scatterChart = new ScatterChart(new NumberAxis(Xmin, Xmax, 1), new NumberAxis(Ymin, Ymax, 1))
val nodes: Array[DeliveryStore] = Array.tabulate(nodesPosition.length)(i => new DeliveryStore(nodesPosition(i),i,10))
val nodesSeries: Series[Number, Number] = new Series[Number, Number] {name = "Nodes"}
val pane = new Pane()
Color.getClass.getDeclaredFields.foreach(f => {
f.setAccessible(true)
if (f.getName.startsWith("Dark")) {vehiclesColor = vehiclesColor :+ f.get(Color).asInstanceOf[Color]}
})
scatterChart.getData.add(nodesSeries)
nodes.foreach(d => {
val data = Data[Number, Number](d.pos._1, d.pos._2)
data.setNode(d)
nodesSeries.getData.add(data)
})
scatterChart.legendVisible = false
scatterChart.animated = false
scatterChart.setMinSize(900, 800)
pane.children.addAll(scatterChart)
this.setFrameNodes(node = pane)
this.stage.getScene.getStylesheets.add(getClass.getResource("../css/RoutingWindow.css").toExternalForm)
this.stage.sizeToScene()
this.showStage()
def addLine(startNode: Node, endNode: Node, vehicle: Int): Unit = {
val startPoint = scatterChart.localToParent(startNode.localToParent(new Point2D(startNode.getBoundsInLocal.getWidth/2, startNode.getBoundsInLocal.getHeight/2)))
val endPoint = scatterChart.localToParent(endNode.localToParent(new Point2D(endNode.getBoundsInLocal.getWidth/2, endNode.getBoundsInLocal.getHeight/2)))
val line = new Line {
fill = Color.White
stroke = Color.White
startX = startPoint.getX
startY = startPoint.getY
endX = endPoint.getX
endY = endPoint.getY
smooth = true
}
Platform.runLater({
this.pane.children.add(line)
line.toFront()
})
}
预先感谢