In the GraphStream documentation, it is stated that we can style a graph by the following ways:
This attribute must be stored on a graph and takes as value either the style sheet itself as a string of characters or the URL of a file, local, distant or packaged in a jar.
You can for example change the background color of the graph using a style sheet given as a string this way:
graph.addAttribute("ui.stylesheet", "graph { fill-color: red; }");
But you can also specify an URL:
graph.addAttribute("ui.stylehseet", "url('http://www.deep.in/the/site/mystylesheet')");
Or:
graph.addAttribute("ui.stylesheet", "url(file:///somewhere/over/the/rainbow/stylesheet')");
However, I experimented with this, and came to the conclusion that GraphStream only supports absolute file paths for this attribute. This is an issue since I will ultimately be exporting the application as a JAR file, and although I have found that you can circumvent the issue by doing something like:
ClassLoader.getSystemClassLoader().getResource(".").getPath();
there is uncertainty associated with this method (i.e. it may not work on certain machines, such as Linux machines (?)).
In which case, my current hack is to store the 'stylesheet' as a long string, something like this:
private static final String GRAPH_DISPLAY_STYLESHEET =
"graph { fill-color: white; }" +
"node {" +
"fill-color: black;" +
"shape: rounded-box;" +
"shadow-mode: plain;" +
"shadow-color: #C8C8C8;" +
"shadow-width: 4;" +
"shadow-offset: 4, -4;" +
"text-background-mode: rounded-box;" +
"text-padding: 5;" +
"text-background-color: black;" +
"text-color: white;" +
"text-size: 20;" +
"size-mode: fit;}" +
"edge { fill-color: black; }";
which is frankly very unsightly.
Does anyone have any ideas about how to improve this situation? Thanks in advance!
Using an embedded-resource and getResource()
is the way to go. To avoid file system vagaries, try one of these approaches:
Use one of the approaches adduced here to convert each resource to a String
suitable for addAttribute()
.
Load an instance of java.util.Properties
with your styles, as shown here and here, so that each style is available as a String
suitable for addAttribute()
.