George Violettas
George Violettas

Reputation: 381

render a graphstream graph into javafx

The idea is to show a graphstream graph embedded in a Javafx simple GUI. There is not much info on the net about this. graphstream has a github (which has a lot of errors like not finding the gs-algo in maven) and the code produces errors. I created a minimal JavaFX project with empty Controller and fxml file. The Application file is below. The first group of lines is a normal graphstream graph which shows ok on a SEPARATE window. The next two groups are trying to show it inside the javafx window.

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.swingViewer.GraphRenderer;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();

        Graph graph = new SingleGraph("Graph");
        graph.addNode("1"); graph.addNode("2"); graph.addNode("3");
        graph.addEdge("1-->2","1","2", true);
        graph.addEdge("1-->3","1","3", true);

        /* These 3 lines show correctly a graph, ON A SEPARATE WINDOW */
        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        viewer = graph.display(true);
        viewer.enableAutoLayout();

        // Those below both do not work. I tried many vestions of gs-ui-javafx but ...
        //https://github.com/graphstream/gs-ui-javafx/blob/master/src-test/org/graphstream/ui/viewer_fx/test/AllFxTest.java
        FxViewer fxviewer = new FxViewer(graph, FxViewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        FxGraphRenderer renderer = new FxGraphRenderer();
        FxDefaultView view = (FxDefaultView) fxviewer.addView(FxViewer.DEFAULT_VIEW_ID, renderer);

        /* https://github.com/graphstream/gs-ui-javafx/blob/master/src-test/org/graphstream/ui/viewer_fx/test/AllFxTest.java */
        FxViewer v = new FxViewer(graph, FxViewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        v.enableAutoLayout();
        FxViewPanel panel = (FxViewPanel)v.addDefaultView(false, new GraphRenderer());
        Scene scene2 = new Scene(panel, 800, 600);
        primaryStage.setScene(scene2);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 0

Views: 1586

Answers (2)

George Violettas
George Violettas

Reputation: 381

Important Note: If you want to use Maven to import the needed libraries (highly recommended) you need to first import the jitpack.io repository to maven.

Upvotes: 0

H.Brahimi
H.Brahimi

Reputation: 263

There's some confusion in your code. You can properly use Javafx in the version 2 of Graphstream, but in this version, you can't do

Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);

because Viewer became an abstract class as you can see here :

This means that you doesn't use the good version of graphstream. If you want to use javafx, then you should use gs-core, gs-algo and gs-ui-javafx at the 2.0-alpha version as explained in the README here.

Then you can find some example here to help you.

Upvotes: 2

Related Questions