Skip to content

Commit 6ffdc03

Browse files
committed
Refactor examples.
1 parent cd10f17 commit 6ffdc03

10 files changed

Lines changed: 347 additions & 153 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656

5757
- (1.0.0) Package now available through [Maven Central](https://central.sonatype.com/namespace/com.brunomnsilva). The library seems stable, after dozens of college projects of my students have used it. Hence, the version was bumped to 1.0.0.
5858

59-
- (0.9.4) You can now annotate a method with `@SmartLabelSource` within a model class to provide the displayed label for a vertex/edge; see the example at `com.brunomnsilva.smartgraph.example`. If no annotation is present, then the `toString()` method is used to obtain the label's text.
59+
- (0.9.4) You can now annotate a method with `@SmartLabelSource` within a model class to provide the displayed label for a vertex/edge; see the example at `com.brunomnsilva.smartgraph.examples.cities`. If no annotation is present, then the `toString()` method is used to obtain the label's text.
6060

61-
- (0.9.4) You can manually alter a vertex position on the panel at anytime, through `SmartGraphPanel.setVertexPosition(Vertex<V> v)`; see the example at `com.brunomnsilva.smartgraph.example`.
61+
- (0.9.4) You can manually alter a vertex position on the panel at anytime, through `SmartGraphPanel.setVertexPosition(Vertex<V> v)`; see the example at `com.brunomnsilva.smartgraph.examples.cities`.
6262

63-
- (0.9.4) You can override specific default properties by using a *String* parameter to the `SmartGraphProperties` constructor; see the example at `com.brunomnsilva.smartgraph.example`. This is useful if you want to display visually different graphs within the same application.
63+
- (0.9.4) You can override specific default properties by using a *String* parameter to the `SmartGraphProperties` constructor; see the example at `com.brunomnsilva.smartgraph.examples.cities`. This is useful if you want to display visually different graphs within the same application.
6464

6565
- (0.9.4) You can now style labels and arrows individually.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ graphView.setAutomaticLayout(true);
103103

104104
Below are provided some graphs and the corresponding visualization, either using a static placement strategy or by the automatic force-directed layout algorithm.
105105

106+
:bulb: You can check the `examples` package and the [wiki](https://github.com/brunomnsilva/JavaFXSmartGraph/wiki) for these and other advanced usages.
107+
106108
### Sample Graph
107109

108110
The following code creates a sample graph:

pom.xml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
<!-- Default configuration for running with: mvn clean javafx:run -->
115115
<id>default-cli</id>
116116
<configuration>
117-
<mainClass>com.brunomnsilva.smartgraph.Main</mainClass>
117+
<mainClass>com.brunomnsilva.smartgraph.examples.digraph.Main</mainClass>
118118
<launcher>app</launcher>
119119
<jlinkZipName>app</jlinkZipName>
120120
<jlinkImageName>app</jlinkImageName>
@@ -124,10 +124,36 @@
124124
</configuration>
125125
</execution>
126126
<execution>
127-
<!-- Configuration for running with: mvn clean javafx:run@example -->
128-
<id>example</id>
127+
<!-- Configuration for running with: mvn clean javafx:run@cities -->
128+
<id>cities</id>
129129
<configuration>
130-
<mainClass>com.brunomnsilva.smartgraph.example.ExampleMain</mainClass>
130+
<mainClass>com.brunomnsilva.smartgraph.examples.cities.Main</mainClass>
131+
<launcher>app</launcher>
132+
<jlinkZipName>app</jlinkZipName>
133+
<jlinkImageName>app</jlinkImageName>
134+
<noManPages>true</noManPages>
135+
<stripDebug>true</stripDebug>
136+
<noHeaderFiles>true</noHeaderFiles>
137+
</configuration>
138+
</execution>
139+
<execution>
140+
<!-- Configuration for running with: mvn clean javafx:run@dynamic -->
141+
<id>dynamic</id>
142+
<configuration>
143+
<mainClass>com.brunomnsilva.smartgraph.examples.dynamic.Main</mainClass>
144+
<launcher>app</launcher>
145+
<jlinkZipName>app</jlinkZipName>
146+
<jlinkImageName>app</jlinkImageName>
147+
<noManPages>true</noManPages>
148+
<stripDebug>true</stripDebug>
149+
<noHeaderFiles>true</noHeaderFiles>
150+
</configuration>
151+
</execution>
152+
<execution>
153+
<!-- Configuration for running with: mvn clean javafx:run@flowers -->
154+
<id>flowers</id>
155+
<configuration>
156+
<mainClass>com.brunomnsilva.smartgraph.examples.flowers.Main</mainClass>
131157
<launcher>app</launcher>
132158
<jlinkZipName>app</jlinkZipName>
133159
<jlinkImageName>app</jlinkImageName>

src/main/java/com/brunomnsilva/smartgraph/example/City.java renamed to src/main/java/com/brunomnsilva/smartgraph/examples/cities/City.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License
33
*
4-
* JavaFXSmartGraph | Copyright 2023-2024 brunomnsilva@gmail.com
4+
* JavaFXSmartGraph | Copyright 2023-2025 brunomnsilva@gmail.com
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
package com.brunomnsilva.smartgraph.example;
25+
package com.brunomnsilva.smartgraph.examples.cities;
2626

2727
import com.brunomnsilva.smartgraph.graphview.SmartLabelSource;
2828
import com.brunomnsilva.smartgraph.graphview.SmartRadiusSource;

src/main/java/com/brunomnsilva/smartgraph/example/Distance.java renamed to src/main/java/com/brunomnsilva/smartgraph/examples/cities/Distance.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License
33
*
4-
* JavaFXSmartGraph | Copyright 2023-2024 brunomnsilva@gmail.com
4+
* JavaFXSmartGraph | Copyright 2023-2025 brunomnsilva@gmail.com
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
package com.brunomnsilva.smartgraph.example;
25+
package com.brunomnsilva.smartgraph.examples.cities;
2626

2727
import com.brunomnsilva.smartgraph.graphview.SmartLabelSource;
2828

src/main/java/com/brunomnsilva/smartgraph/example/ExampleMain.java renamed to src/main/java/com/brunomnsilva/smartgraph/examples/cities/Main.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License
33
*
4-
* JavaFXSmartGraph | Copyright 2023-2024 brunomnsilva@gmail.com
4+
* JavaFXSmartGraph | Copyright 2023-2025 brunomnsilva@gmail.com
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
package com.brunomnsilva.smartgraph.example;
25+
package com.brunomnsilva.smartgraph.examples.cities;
2626

2727
import com.brunomnsilva.smartgraph.containers.SmartGraphDemoContainer;
2828
import com.brunomnsilva.smartgraph.graph.Graph;
@@ -40,7 +40,7 @@
4040
* Example program that depicts some world cities and their distances.
4141
* @author brunomnsilva
4242
*/
43-
public class ExampleMain extends Application {
43+
public class Main extends Application {
4444

4545
@Override
4646
public void start(Stage ignored) {
@@ -107,7 +107,11 @@ public void start(Stage ignored) {
107107
//graphVertex.setStyleInline("-fx-fill: red;"); //this will overwrite the property later on
108108

109109
graphView.setVertexDoubleClickAction(graphVertex -> {
110-
graphVertex.setStyleClass("myVertex");
110+
//toggle different styling
111+
if( !graphVertex.removeStyleClass("myVertex") ) {
112+
graphVertex.addStyleClass("myVertex");
113+
}
114+
// Use instead (to set it permanently): graphVertex.setStyleClass("myVertex");
111115
});
112116

113117
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)