|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * JavaFXSmartGraph | Copyright 2019-2024 brunomnsilva@gmail.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.brunomnsilva.smartgraph.examples.digraph; |
| 25 | + |
| 26 | +import com.brunomnsilva.smartgraph.containers.SmartGraphDemoContainer; |
| 27 | +import com.brunomnsilva.smartgraph.graph.Digraph; |
| 28 | +import com.brunomnsilva.smartgraph.graph.DigraphEdgeList; |
| 29 | +import com.brunomnsilva.smartgraph.graph.Graph; |
| 30 | +import com.brunomnsilva.smartgraph.graphview.*; |
| 31 | +import javafx.application.Application; |
| 32 | +import javafx.scene.Scene; |
| 33 | +import javafx.stage.Stage; |
| 34 | +import javafx.stage.StageStyle; |
| 35 | + |
| 36 | +/** |
| 37 | + * Class that provides an example of using the library. |
| 38 | + * |
| 39 | + * @author brunomnsilva |
| 40 | + */ |
| 41 | +public class Main extends Application { |
| 42 | + |
| 43 | + private volatile boolean running; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void start(Stage ignored) { |
| 47 | + |
| 48 | + Graph<String, String> g = build_sample_digraph(); |
| 49 | + |
| 50 | + SmartPlacementStrategy initialPlacement = new SmartCircularSortedPlacementStrategy(); |
| 51 | + ForceDirectedLayoutStrategy<String> automaticPlacementStrategy = new ForceDirectedSpringGravityLayoutStrategy<>(); |
| 52 | + |
| 53 | + SmartGraphPanel<String, String> graphView = new SmartGraphPanel<>(g, initialPlacement, automaticPlacementStrategy); |
| 54 | + |
| 55 | + /* |
| 56 | + After creating, you can change the styling of some element. |
| 57 | + This can be done at any time afterwards. |
| 58 | + */ |
| 59 | + if (g.numVertices() > 0) { |
| 60 | + graphView.getStylableVertex("A").setStyleInline("-fx-fill: gold; -fx-stroke: brown;"); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + Scene scene = new Scene(new SmartGraphDemoContainer(graphView), 1024, 768); |
| 65 | + |
| 66 | + Stage stage = new Stage(StageStyle.DECORATED); |
| 67 | + stage.setTitle("JavaFX SmartGraph Visualization"); |
| 68 | + stage.setMinHeight(500); |
| 69 | + stage.setMinWidth(800); |
| 70 | + stage.setScene(scene); |
| 71 | + stage.show(); |
| 72 | + |
| 73 | + /* |
| 74 | + IMPORTANT: Must call init() after scene is displayed, so we can have width and height values |
| 75 | + to initially place the vertices according to the placement strategy. |
| 76 | + */ |
| 77 | + graphView.init(); |
| 78 | + |
| 79 | + /* |
| 80 | + Bellow you can see how to attach actions for when vertices and edges are double-clicked |
| 81 | + */ |
| 82 | + graphView.setVertexDoubleClickAction((SmartGraphVertex<String> graphVertex) -> { |
| 83 | + System.out.println("Vertex contains element: " + graphVertex.getUnderlyingVertex().element()); |
| 84 | + |
| 85 | + //toggle different styling |
| 86 | + if( !graphVertex.removeStyleClass("myVertex") ) { |
| 87 | + /* for the golden vertex, this is necessary to clear the inline |
| 88 | + css class. Otherwise, it has priority for included styles. Test and uncomment. */ |
| 89 | + //graphVertex.setStyleInline(null); |
| 90 | + |
| 91 | + graphVertex.addStyleClass("myVertex"); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + graphView.setEdgeDoubleClickAction(graphEdge -> { |
| 96 | + System.out.println("Edge contains element: " + graphEdge.getUnderlyingEdge().element()); |
| 97 | + //dynamically change the style when clicked; style is propagated to the arrows |
| 98 | + graphEdge.setStyleClass("myEdge"); |
| 99 | + }); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param args the command line arguments |
| 105 | + */ |
| 106 | + public static void main(String[] args) { |
| 107 | + launch(args); |
| 108 | + } |
| 109 | + |
| 110 | + private Graph<String, String> build_sample_digraph() { |
| 111 | + |
| 112 | + Digraph<String, String> g = new DigraphEdgeList<>(); |
| 113 | + |
| 114 | + g.insertVertex("A"); |
| 115 | + g.insertVertex("B"); |
| 116 | + g.insertVertex("C"); |
| 117 | + g.insertVertex("D"); |
| 118 | + g.insertVertex("E"); |
| 119 | + g.insertVertex("F"); |
| 120 | + |
| 121 | + g.insertEdge("A", "B", "AB"); |
| 122 | + g.insertEdge("B", "A", "AB2"); |
| 123 | + g.insertEdge("A", "C", "AC"); |
| 124 | + g.insertEdge("A", "D", "AD"); |
| 125 | + g.insertEdge("B", "C", "BC"); |
| 126 | + g.insertEdge("C", "D", "CD"); |
| 127 | + g.insertEdge("B", "E", "BE"); |
| 128 | + g.insertEdge("F", "D", "DF"); |
| 129 | + g.insertEdge("F", "D", "DF2"); |
| 130 | + |
| 131 | + //yep, its a loop! |
| 132 | + g.insertEdge("A", "A", "Loop"); |
| 133 | + |
| 134 | + return g; |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments