JUNG:将图形保存到图像文件中

发布于 2024-12-27 23:26:12 字数 205 浏览 1 评论 0原文

我使用 JUNG 来可视化我的图表/网络。 现在我想将图形(如 VisualizationViewer 中所示)保存在图像文件中。我使用 VisualizationViewer(扩展 JPanel)的 Paint() / PaintAll() 函数。但使用此功能时,图像中只有 VisualizationViewer 中实际显示的部分(例如放大后)。我想绘制所有顶点和边。有没有办法绘制所有元素?

I use JUNG to visualize my graph / network.
Now i want to save the graph (as seen in the VisualizationViewer) in a image file. I use the paint() / paintAll() function of the VisualizationViewer (who extend JPanel). But with this function, only the part who is actually shown in the VisualizationViewer (for example after zooming in) is in the image. I want to draw all Vertexes and Edges. Is there a way to draw all Elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几味少女 2025-01-03 23:26:12

使用 freeHEP 库 和 JUNG 的 VisualizationImageServer 的解决方案:

private void doSaveAs() throws IOException {
    // instantiate and configure image-able visualization viewer
    VisualizationImageServer<Vertex, Edge> vis =
            new VisualizationImageServer<Vertex, Edge>(this.visViewer.getGraphLayout(),
                                                       this.visViewer.getGraphLayout().getSize());

    setUpAppearance(vis);

    ExportDialog export = new ExportDialog();
    export.showExportDialog(vis, "Export view as ...", vis, "export");
}

我找到了 ,这将为用户打开一个导出对话框,可以在其中选择目录和文件类型。

在此代码段中,ExportDialog 是 org.freehep.graphicsbase.util.export.ExportDialog,您必须以某种方式到达构建路径,在我的例子中,通过添加 freehep-graphicsio 使用 Maven 到我的 pom 文件。

字段 this.visViewer 包含常规 VisualizationViewer 实例,您也可以使用它来显示图形。

setUpAppearance(vis); 方法执行的设置与我在 VisualizationViewer 实例上执行的设置相同,用于显示目的。这是一个示例,详细信息可能会因您而异:

private void setUpAppearance(BasicVisualizationServer<Vertex, Edge> vis) {
    vis.setBackground(BGCOLOR);
    vis.setPreferredSize(new Dimension(1500, 600)); // Sets the viewing area

    // modify vertices
    vis.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    vis.getRenderContext().setVertexFontTransformer(Transformers.vertexFontTransformer);
    vis.getRenderContext().setVertexShapeTransformer(Transformers.vertexShapeTransformer);
    vis.getRenderContext().setVertexFillPaintTransformer(Transformers.vertexFillPaintTransformer);
    vis.getRenderContext().setVertexDrawPaintTransformer(Transformers.vertexDrawPaintTransformer);
    vis.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);

    // modify edges
    vis.getRenderContext().setEdgeShapeTransformer(Transformers.edgeShapeTransformer);
    vis.getRenderContext().setEdgeDrawPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setArrowDrawPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setArrowFillPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setEdgeArrowPredicate(Transformers.edgeArrowPredicate);
    vis.getRenderContext().setEdgeStrokeTransformer(Transformers.edgeStrokeHighlightingTransformer);
}

作为最后一步,您需要确定何时应调用 doSaveAs。例如,您可以为此在 UI 上添加一个按钮。

I found a solution using the freeHEP library and JUNG's VisualizationImageServer:

private void doSaveAs() throws IOException {
    // instantiate and configure image-able visualization viewer
    VisualizationImageServer<Vertex, Edge> vis =
            new VisualizationImageServer<Vertex, Edge>(this.visViewer.getGraphLayout(),
                                                       this.visViewer.getGraphLayout().getSize());

    setUpAppearance(vis);

    ExportDialog export = new ExportDialog();
    export.showExportDialog(vis, "Export view as ...", vis, "export");
}

When called, this will open an export dialog to the user, where directory and filetype can be selected.

In this snippet, ExportDialog is org.freehep.graphicsbase.util.export.ExportDialog, which you would have to somehow get to your build path, in my case using maven by adding freehep-graphicsio to my pom file.

The field this.visViewer contains your regular VisualizationViewer instance, that you would also use for displaying your graph.

The method setUpAppearance(vis); performs the same setup that I do on the VisualizationViewer instance for displaying purposes. Here's an example, the details will probably vary for you:

private void setUpAppearance(BasicVisualizationServer<Vertex, Edge> vis) {
    vis.setBackground(BGCOLOR);
    vis.setPreferredSize(new Dimension(1500, 600)); // Sets the viewing area

    // modify vertices
    vis.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    vis.getRenderContext().setVertexFontTransformer(Transformers.vertexFontTransformer);
    vis.getRenderContext().setVertexShapeTransformer(Transformers.vertexShapeTransformer);
    vis.getRenderContext().setVertexFillPaintTransformer(Transformers.vertexFillPaintTransformer);
    vis.getRenderContext().setVertexDrawPaintTransformer(Transformers.vertexDrawPaintTransformer);
    vis.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);

    // modify edges
    vis.getRenderContext().setEdgeShapeTransformer(Transformers.edgeShapeTransformer);
    vis.getRenderContext().setEdgeDrawPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setArrowDrawPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setArrowFillPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setEdgeArrowPredicate(Transformers.edgeArrowPredicate);
    vis.getRenderContext().setEdgeStrokeTransformer(Transformers.edgeStrokeHighlightingTransformer);
}

As a last step you need to figure out when doSaveAs should be called. For instance you could add a Button on the UI for that.

掀纱窥君容 2025-01-03 23:26:12

我对 JUNG 一无所知,但它只是扩展了 JPanel 来进行绘画,那么您应该能够使用 屏幕图像 类,用于创建任何组件的图像。

I don't know anything about JUNG, but it is just extends a JPanel to do the painting then you should be able to use the Screen Image class to create an image of any component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文