From 2d1ee2a6621d9e9cfb7e768b966089d35d3c63e3 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 20 Sep 2017 22:26:39 -0700 Subject: [PATCH] Start implementing the canvas to draw graphs. --- .../nwapw/abacus/fx/graphing/GraphCanvas.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 fx/src/main/kotlin/org/nwapw/abacus/fx/graphing/GraphCanvas.kt diff --git a/fx/src/main/kotlin/org/nwapw/abacus/fx/graphing/GraphCanvas.kt b/fx/src/main/kotlin/org/nwapw/abacus/fx/graphing/GraphCanvas.kt new file mode 100644 index 0000000..12264d8 --- /dev/null +++ b/fx/src/main/kotlin/org/nwapw/abacus/fx/graphing/GraphCanvas.kt @@ -0,0 +1,39 @@ +package org.nwapw.abacus.fx.graphing + +import javafx.beans.value.ChangeListener +import javafx.scene.canvas.Canvas + +/** + * A canvas that renders a graph. + * + * The GraphCanvas uses the provided [Graph] instance in order to draw the outputs on itself. + * @param graph the graph used to render. + */ +class GraphCanvas(graph: Graph): Canvas() { + + /** + * The graph that is currently being used to generate inputs / outputs. + * The redraw is triggered if this graph is reset. + */ + var graph: Graph = graph + set(value) { + field = value + redraw() + } + + init { + val redrawListener = ChangeListener { _, _, _ -> redraw() } + widthProperty().addListener(redrawListener) + heightProperty().addListener(redrawListener) + redraw() + } + + /** + * Redraws the graph onto the canvas. + */ + fun redraw() { + val graphicsContext = graphicsContext2D + val outputs = graph.generateOutputs(graph.generateInputs()) + } + +} \ No newline at end of file