Search in sources :

Example 1 with IdAlreadyInUseException

use of org.graphstream.graph.IdAlreadyInUseException in project gs-ui-javafx by graphstream.

the class TestStars2 method start.

public void start(Stage primaryStage) throws Exception {
    SingleGraph graph = new SingleGraph("Stars !");
    double x0 = 0.0;
    double x1 = 0.0;
    double width = 100.0;
    double height = 20.0;
    int n = 500;
    Random random = new Random();
    double minDis = 4.0;
    double sizeMx = 10.0;
    graph.setAttribute("ui.stylesheet", styleSheet);
    graph.setAttribute("ui.quality");
    graph.setAttribute("ui.antialias");
    Viewer viewer = new FxViewer(graph, FxViewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
    ViewerPipe pipeIn = viewer.newViewerPipe();
    FxDefaultView view = (FxDefaultView) viewer.addView("view1", new FxGraphRenderer());
    view.resize(1000, (int) (1200 * (height / width)));
    Scene scene = new Scene(view, 1000, 1200 * (height / width), true, SceneAntialiasing.BALANCED);
    primaryStage.setScene(scene);
    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

        @Override
        public void handle(WindowEvent t) {
            Platform.exit();
            System.exit(0);
        }
    });
    primaryStage.setTitle("Stars 2");
    primaryStage.show();
    for (int i = 0; i < n; i++) {
        Node node = graph.addNode(i + "");
        node.setAttribute("xyz", (random.nextDouble() * width), (random.nextDouble() * height), 0);
        node.setAttribute("ui.size", (random.nextDouble() * sizeMx));
    }
    graph.nodes().forEach(node -> {
        Point3 pos = new Point3(GraphPosLengthUtils.nodePosition(node));
        graph.nodes().forEach(otherNode -> {
            if (otherNode != node) {
                Point3 otherPos = new Point3(GraphPosLengthUtils.nodePosition(otherNode));
                double dist = otherPos.distance(pos);
                if (dist < minDis) {
                    if (!node.hasEdgeBetween(otherNode.getId())) {
                        try {
                            graph.addEdge(node.getId() + "--" + otherNode.getId(), node.getId(), otherNode.getId());
                        } catch (IdAlreadyInUseException e) {
                            graph.addEdge(node.getId() + "--" + otherNode.getId() + "-2", node.getId(), otherNode.getId());
                        }
                    }
                }
            }
        });
    });
}
Also used : FxGraphRenderer(org.graphstream.ui.javafx.FxGraphRenderer) Node(org.graphstream.graph.Node) FxViewer(org.graphstream.ui.fx_viewer.FxViewer) Viewer(org.graphstream.ui.view.Viewer) FxDefaultView(org.graphstream.ui.fx_viewer.FxDefaultView) Scene(javafx.scene.Scene) FxViewer(org.graphstream.ui.fx_viewer.FxViewer) Point3(org.graphstream.ui.geom.Point3) IdAlreadyInUseException(org.graphstream.graph.IdAlreadyInUseException) Random(java.util.Random) WindowEvent(javafx.stage.WindowEvent) SingleGraph(org.graphstream.graph.implementations.SingleGraph) ViewerPipe(org.graphstream.ui.view.ViewerPipe)

Aggregations

Random (java.util.Random)1 Scene (javafx.scene.Scene)1 WindowEvent (javafx.stage.WindowEvent)1 IdAlreadyInUseException (org.graphstream.graph.IdAlreadyInUseException)1 Node (org.graphstream.graph.Node)1 SingleGraph (org.graphstream.graph.implementations.SingleGraph)1 FxDefaultView (org.graphstream.ui.fx_viewer.FxDefaultView)1 FxViewer (org.graphstream.ui.fx_viewer.FxViewer)1 Point3 (org.graphstream.ui.geom.Point3)1 FxGraphRenderer (org.graphstream.ui.javafx.FxGraphRenderer)1 Viewer (org.graphstream.ui.view.Viewer)1 ViewerPipe (org.graphstream.ui.view.ViewerPipe)1