Skip to content

Commit

Permalink
Add ability to set color of edges and nodes using GraphCanvas
Browse files Browse the repository at this point in the history
  • Loading branch information
FeldrinH committed Oct 23, 2023
1 parent 90abe70 commit e68922d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/main/java/ee/ut/dendroloj/GenericGraphLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
47 changes: 38 additions & 9 deletions src/main/java/ee/ut/dendroloj/GraphCanvas.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <b>EXPERIMENTAL API</b>
Expand All @@ -15,30 +22,52 @@
*/
public final class GraphCanvas<T> {

final Map<T, String> vertices = new HashMap<>();
final List<Vertex<T>> vertices = new ArrayList<>();
final List<Edge<T>> 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<T> {
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<T> {
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;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ee/ut/dendroloj/GraphGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;" +
Expand All @@ -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;" +
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/GraafKatsed.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -39,9 +40,9 @@ public static void main(String[] args) {

GraphCanvas<Tipp> 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);
Expand Down

0 comments on commit e68922d

Please sign in to comment.