没有可怕的 strings/absolute 文件路径的 GraphStream 样式?

GraphStream styling without hideous strings/absolute file paths?

在 GraphStream 文档中,声明我们可以通过以下方式设置图形样式:

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')");

但是,我对此进行了试验,得出的结论是GraphStream 只支持此属性的绝对文件路径。这是一个问题,因为我最终会将应用程序导出为 JAR 文件,尽管我发现您可以通过执行以下操作来规避该问题:

ClassLoader.getSystemClassLoader().getResource(".").getPath(‌​);

此方法存在不确定性(即它可能不适用于某些机器,例如 Linux 机器 (?))。

在这种情况下,我目前的技巧是将 'stylesheet' 存储为一个长字符串,如下所示:

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; }";

坦率地说,这非常难看。

有没有人知道如何改善这种情况?提前致谢!

使用 and getResource() 是正确的方法。为避免文件系统异常,请尝试以下方法之一:

  • 使用引用的方法之一 here 将每个资源转换为适合 addAttribute()String

  • 加载java.util.Properties with your styles, as shown here and here的一个实例,这样每个样式都可以作为String适合addAttribute().