From e68922d03d7c7e6cab5601b3457dde2066afd3ef Mon Sep 17 00:00:00 2001 From: Juhan Oskar Hennoste Date: Mon, 23 Oct 2023 03:55:25 +0300 Subject: [PATCH] Add ability to set color of edges and nodes using GraphCanvas --- .../ee/ut/dendroloj/GenericGraphLayout.java | 8 ++-- .../java/ee/ut/dendroloj/GraphCanvas.java | 47 +++++++++++++++---- src/main/java/ee/ut/dendroloj/GraphGUI.java | 3 +- src/test/java/GraafKatsed.java | 5 +- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/main/java/ee/ut/dendroloj/GenericGraphLayout.java b/src/main/java/ee/ut/dendroloj/GenericGraphLayout.java index 85b763b..460aa5b 100644 --- a/src/main/java/ee/ut/dendroloj/GenericGraphLayout.java +++ b/src/main/java/ee/ut/dendroloj/GenericGraphLayout.java @@ -19,13 +19,15 @@ public static Layout autoLayout() { public static Graph assembleGraph(GraphCanvas graphCanvas) { Graph graph = new MultiGraph("dendroloj"); - for (var vertex : graphCanvas.vertices.entrySet()) { - Node node = graph.addNode(getNodeId(vertex.getKey())); - node.setAttribute("label", vertex.getValue()); + for (var vertex : graphCanvas.vertices) { + Node node = graph.addNode(getNodeId(vertex.vertex)); + node.setAttribute("label", vertex.label); + if (vertex.color != null) node.setAttribute("ui.color", vertex.color); } for (var edge : graphCanvas.edges) { Edge graphEdge = graph.addEdge(getNewEdgeId(), getNodeId(edge.from), getNodeId(edge.to), true); if (edge.label != null) graphEdge.setAttribute("label", edge.label); + if (edge.color != null) graphEdge.setAttribute("ui.color", edge.color); } return graph; } diff --git a/src/main/java/ee/ut/dendroloj/GraphCanvas.java b/src/main/java/ee/ut/dendroloj/GraphCanvas.java index 591c75c..cad029e 100644 --- a/src/main/java/ee/ut/dendroloj/GraphCanvas.java +++ b/src/main/java/ee/ut/dendroloj/GraphCanvas.java @@ -1,9 +1,16 @@ package ee.ut.dendroloj; -import java.util.ArrayList; -import java.util.HashMap; +import org.graphstream.graph.Edge; +import org.graphstream.graph.Graph; +import org.graphstream.graph.Node; +import org.graphstream.graph.implementations.MultiGraph; +import org.graphstream.ui.layout.Layout; +import org.graphstream.ui.layout.springbox.implementations.SpringBox; + +import java.awt.*; +import java.util.*; import java.util.List; -import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; /** * EXPERIMENTAL API @@ -15,30 +22,52 @@ */ public final class GraphCanvas { - final Map vertices = new HashMap<>(); + final List> vertices = new ArrayList<>(); final List> edges = new ArrayList<>(); public void drawVertex(T vertex, String label) { - vertices.put(vertex, label); + drawVertex(vertex, label, null); } - public void drawEdge(T from, T to, String label) { - edges.add(new Edge<>(from, to, label)); + public void drawVertex(T vertex, String label, Color color) { + vertices.add(new Vertex<>(vertex, label, color)); } public void drawEdge(T from, T to) { - edges.add(new Edge<>(from, to, null)); + drawEdge(from, to, null, null); + } + + public void drawEdge(T from, T to, String label) { + drawEdge(from, to, label, null); + } + + public void drawEdge(T from, T to, String label, Color color) { + edges.add(new Edge<>(from, to, label, color)); + } + + static final class Vertex { + public final T vertex; + public final String label; + public final Color color; + + private Vertex(T vertex, String label, Color color) { + this.vertex = vertex; + this.label = label; + this.color = color; + } } static final class Edge { public final T from; public final T to; public final String label; + public final Color color; - Edge(T from, T to, String label) { + private Edge(T from, T to, String label, Color color) { this.from = from; this.to = to; this.label = label; + this.color = color; } } diff --git a/src/main/java/ee/ut/dendroloj/GraphGUI.java b/src/main/java/ee/ut/dendroloj/GraphGUI.java index 902e40e..695fb61 100644 --- a/src/main/java/ee/ut/dendroloj/GraphGUI.java +++ b/src/main/java/ee/ut/dendroloj/GraphGUI.java @@ -29,6 +29,7 @@ public static void initGenericGUI(double uiScale, Graph graph, Layout layout) { graph.setAttribute("ui.stylesheet", String.format(Locale.ROOT, "edge {" + " size: %fpx;" + + " fill-mode: dyn-plain;" + " text-size: %f; text-alignment: center;" + " text-background-mode: plain; text-background-color: rgba(255, 255, 255, 180);" + " text-padding: %f;" + @@ -38,7 +39,7 @@ public static void initGenericGUI(double uiScale, Graph graph, Layout layout) { "}" + "node {" + " size: %fpx;" + - " fill-mode: plain;" + + " fill-mode: dyn-plain;" + " fill-color: rgb(210, 210, 210);" + " text-size: %f; text-alignment: center;" + " text-offset: 0px, -%fpx;" + diff --git a/src/test/java/GraafKatsed.java b/src/test/java/GraafKatsed.java index 3297c9f..64197f8 100644 --- a/src/test/java/GraafKatsed.java +++ b/src/test/java/GraafKatsed.java @@ -1,6 +1,7 @@ import ee.ut.dendroloj.Dendrologist; import ee.ut.dendroloj.GraphCanvas; +import java.awt.*; import java.util.ArrayList; import java.util.List; @@ -39,9 +40,9 @@ public static void main(String[] args) { GraphCanvas lõuend = new GraphCanvas<>(); for (Tipp tipp : tipud) { - lõuend.drawVertex(tipp, tipp.tähis); + lõuend.drawVertex(tipp, tipp.tähis, Color.GREEN); for (Kaar kaar : tipp.kaared) { - lõuend.drawEdge(tipp, kaar.lõppTipp, String.valueOf(kaar.kaal)); + lõuend.drawEdge(tipp, kaar.lõppTipp, String.valueOf(kaar.kaal), Color.MAGENTA); } } Dendrologist.drawGraph(lõuend);