Skip to content

Commit c56e112

Browse files
author
brunomnsilva
committed
Enhancement for #11. Added "edge.arrowsize" property and SmartGraphEdge.getStylableArrow();
1 parent bee2aef commit c56e112

8 files changed

Lines changed: 50 additions & 7 deletions

File tree

smartgraph.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ vertex.label = true
1616
#
1717
edge.tooltip = true
1818
edge.label = false
19-
# only makes sense if displaying an oriented graph
19+
# only makes sense if displaying a directed graph
2020
edge.arrow = true
2121

22+
# size in pixels (side of a triangle); only for directed graphs
23+
edge.arrowsize = 5
24+
2225
# (automatic) Force-directed layout related configurations
2326
# -- You should experiment with different values for your
2427
# -- particular problem, knowing that not all will achieve

src/com/brunomnsilva/smartgraph/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ public void start(Stage ignored) {
112112
graphView.setEdgeDoubleClickAction(graphEdge -> {
113113
System.out.println("Edge contains element: " + graphEdge.getUnderlyingEdge().element());
114114
//dynamically change the style when clicked
115-
graphEdge.setStyle("-fx-stroke: black; -fx-stroke-width: 2;");
115+
graphEdge.setStyle("-fx-stroke: black; -fx-stroke-width: 3;");
116116

117+
graphEdge.getStylableArrow().setStyle("-fx-stroke: black; -fx-stroke-width: 3;");
117118

118119
//uncomment to see edges being removed after click
119120
//Edge<String, String> underlyingEdge = graphEdge.getUnderlyingEdge();

src/com/brunomnsilva/smartgraph/graphview/SmartArrow.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ public class SmartArrow extends Path implements SmartStylableNode {
3737
/* Styling proxy */
3838
private final SmartStyleProxy styleProxy;
3939

40-
public SmartArrow() {
40+
/**
41+
* Constructor
42+
*
43+
* @param size determines the size of the arrow (side of the triangle in pixels)
44+
*/
45+
public SmartArrow(double size) {
4146

4247
/* Create this arrow shape */
4348
getElements().add(new MoveTo(0, 0));
44-
getElements().add(new LineTo(-5, 5));
49+
getElements().add(new LineTo(-size, size));
4550
getElements().add(new MoveTo(0, 0));
46-
getElements().add(new LineTo(-5, -5));
51+
getElements().add(new LineTo(-size, -size));
4752

4853
/* Add the corresponding css class */
4954
styleProxy = new SmartStyleProxy(this);

src/com/brunomnsilva/smartgraph/graphview/SmartGraphEdge.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,13 @@ public interface SmartGraphEdge<E, V> extends SmartStylableNode {
5050
* @see SmartGraphPanel
5151
*/
5252
public Edge<E, V> getUnderlyingEdge();
53+
54+
/**
55+
* Returns the attached arrow of the edge, for styling purposes.
56+
*
57+
* The arrows are only used with directed graphs.
58+
*
59+
* @return arrow reference; null if does not exist.
60+
*/
61+
public SmartStylableNode getStylableArrow();
5362
}

src/com/brunomnsilva/smartgraph/graphview/SmartGraphEdgeCurve.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,9 @@ public void attachArrow(SmartArrow arrow) {
218218
public SmartArrow getAttachedArrow() {
219219
return this.attachedArrow;
220220
}
221+
222+
@Override
223+
public SmartStylableNode getStylableArrow() {
224+
return this.attachedArrow;
225+
}
221226
}

src/com/brunomnsilva/smartgraph/graphview/SmartGraphEdgeLine.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public SmartLabel getAttachedLabel() {
102102
public Edge<E, V> getUnderlyingEdge() {
103103
return underlyingEdge;
104104
}
105+
106+
105107

106108
@Override
107109
public void attachArrow(SmartArrow arrow) {
@@ -132,5 +134,10 @@ public void attachArrow(SmartArrow arrow) {
132134
public SmartArrow getAttachedArrow() {
133135
return this.attachedArrow;
134136
}
137+
138+
@Override
139+
public SmartStylableNode getStylableArrow() {
140+
return this.attachedArrow;
141+
}
135142

136143
}

src/com/brunomnsilva/smartgraph/graphview/SmartGraphPanel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ private void initNodes() {
434434
addEdge(graphEdge, edge);
435435

436436
if (this.edgesWithArrows) {
437-
SmartArrow arrow = new SmartArrow();
437+
SmartArrow arrow = new SmartArrow(this.graphProperties.getEdgeArrowSize());
438438
graphEdge.attachArrow(arrow);
439439
this.getChildren().add(arrow);
440440
}
@@ -605,7 +605,7 @@ The opposite vertex exists in the (di)graph, but we have not yet
605605
SmartGraphEdgeBase graphEdge = createEdge(edge, graphVertexIn, graphVertexOut);
606606

607607
if (this.edgesWithArrows) {
608-
SmartArrow arrow = new SmartArrow();
608+
SmartArrow arrow = new SmartArrow(this.graphProperties.getEdgeArrowSize());
609609
graphEdge.attachArrow(arrow);
610610
this.getChildren().add(arrow);
611611
}
@@ -1001,6 +1001,7 @@ public SmartStylableNode getStylableEdge(E edgeElement) {
10011001
}
10021002
return null;
10031003
}
1004+
10041005

10051006
/**
10061007
* Loads the stylesheet and applies the .graph class to this panel.

src/com/brunomnsilva/smartgraph/graphview/SmartGraphProperties.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class SmartGraphProperties {
6262

6363
private static final boolean DEFAULT_EDGE_USE_ARROW = true;
6464
private static final String PROPERTY_EDGE_USE_ARROW = "edge.arrow";
65+
66+
private static final int DEFAULT_ARROW_SIZE = 5;
67+
private static final String PROPERTY_ARROW_SIZE = "edge.arrowsize";
6568

6669
private static final double DEFAULT_REPULSION_FORCE = 1000;
6770
private static final String PROPERTY_REPULSION_FORCE = "layout.repulsive-force";
@@ -201,6 +204,15 @@ public boolean getUseEdgeArrow() {
201204
return getBooleanProperty(PROPERTY_EDGE_USE_ARROW, DEFAULT_EDGE_USE_ARROW);
202205
}
203206

207+
/**
208+
* Returns a property that indicates the size of the {@link SmartArrow}.
209+
*
210+
* @return corresponding property value
211+
*/
212+
public double getEdgeArrowSize() {
213+
return getDoubleProperty(PROPERTY_ARROW_SIZE, DEFAULT_ARROW_SIZE);
214+
}
215+
204216

205217
private double getDoubleProperty(String propertyName, double defaultValue) {
206218
String p = properties.getProperty(propertyName, Double.toString(defaultValue));

0 commit comments

Comments
 (0)