Created
May 30, 2014 06:33
-
-
Save jeffreyguenther/9d0c37329f9928a2b56e to your computer and use it in GitHub Desktop.
JavaFX and JUNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import edu.uci.ics.jung.algorithms.layout.CircleLayout; | |
import edu.uci.ics.jung.algorithms.layout.Layout; | |
import edu.uci.ics.jung.graph.Graph; | |
import edu.uci.ics.jung.graph.util.TestGraphs; | |
import java.awt.Dimension; | |
import javafx.application.Application; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
/** | |
* | |
* @author jeffreyguenther | |
*/ | |
public class GraphVisTester extends Application{ | |
@Override | |
public void start(Stage stage) throws Exception { | |
Graph<String, Number> demoGraph = TestGraphs.getDemoGraph(); | |
Layout<String, Number> layout = new CircleLayout<>(demoGraph); | |
layout.setSize(new Dimension(800, 800)); | |
GraphViz<String, Number> viewer = new GraphViz<>(layout); | |
stage.setScene(new Scene(new Group(viewer))); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import edu.uci.ics.jung.algorithms.layout.Layout; | |
import edu.uci.ics.jung.algorithms.layout.util.Relaxer; | |
import edu.uci.ics.jung.algorithms.layout.util.VisRunner; | |
import edu.uci.ics.jung.algorithms.util.IterativeContext; | |
import edu.uci.ics.jung.graph.Graph; | |
import java.awt.Dimension; | |
import javafx.scene.layout.Region; | |
import javafx.scene.shape.Circle; | |
import javafx.scene.shape.CircleBuilder; | |
import javafx.scene.shape.Line; | |
import javafx.scene.shape.LineBuilder; | |
public class GraphViz<V, E> extends Region{ | |
private Relaxer relaxer; | |
private Layout<V,E> layout; | |
private double CIRCLE_SIZE = 25; | |
public GraphViz(Layout<V, E> layout) { | |
this.layout = layout; | |
} | |
@Override | |
protected void layoutChildren() { | |
super.layoutChildren(); | |
layout.setSize(new Dimension(widthProperty().intValue(), heightProperty().intValue())); | |
// relax the layout | |
if(relaxer != null) { | |
relaxer.stop(); | |
relaxer = null; | |
} | |
if(layout instanceof IterativeContext) { | |
layout.initialize(); | |
if(relaxer == null) { | |
relaxer = new VisRunner((IterativeContext)this.layout); | |
relaxer.prerelax(); | |
relaxer.relax(); | |
} | |
} | |
Graph<V, E> graph = layout.getGraph(); | |
// draw the vertices in the graph | |
for (V v : graph.getVertices()) { | |
// Get the position of the vertex | |
java.awt.geom.Point2D p = layout.transform(v); | |
// draw the vertex as a circle | |
Circle circle = CircleBuilder.create() | |
.centerX(p.getX()) | |
.centerY(p.getY()) | |
.radius(CIRCLE_SIZE) | |
.build(); | |
// add it to the group, so it is shown on screen | |
this.getChildren().add(circle); | |
} | |
// draw the edges | |
for (E e : graph.getEdges()) { | |
// get the end points of the edge | |
edu.uci.ics.jung.graph.util.Pair<V> endpoints = graph.getEndpoints(e); | |
// Get the end points as Point2D objects so we can use them in the | |
// builder | |
java.awt.geom.Point2D pStart = layout.transform(endpoints.getFirst()); | |
java.awt.geom.Point2D pEnd = layout.transform(endpoints.getSecond()); | |
// Draw the line | |
Line line = LineBuilder.create() | |
.startX(pStart.getX()) | |
.startY(pStart.getY()) | |
.endX(pEnd.getX()) | |
.endY(pEnd.getY()) | |
.build(); | |
// add the edges to the screen | |
this.getChildren().add(line); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment