将 JUNG 图导出为高分辨率图像(最好基于矢量)

发布于 2024-12-21 11:33:52 字数 1216 浏览 1 评论 0原文

在我的一个项目中,我使用 JUNG2 来可视化一个非常大的多父层次结构图,并显示在小程序中。我需要将图表的全部/部分导出为高分辨率静态图像,因为屏幕截图在打印时看起来很丑陋(特别是如果图表已缩小)。

我当前使用的代码如下:

public void writeToDisk(File saveToFolder, String filename) {
    //Dimension loDims = getGraphLayout().getSize();
    Dimension vsDims = getSize();

    int width = vsDims.width;
    int height = vsDims.height;
    Color bg = getBackground();

    BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
    Graphics2D graphics = im.createGraphics();
    graphics.setColor(bg);
    graphics.fillRect(0,0, width, height);
    paintComponent(graphics);

    try{
       ImageIO.write(im,"png",new File(saveToFolder,filename));
    }catch(Exception e){
        e.printStackTrace();
    }
}

这会创建分辨率不是特别高的 PNG 图像。所以我的问题如下:

  1. 是否可以将PNG导出分辨率提高到300 dpi?
  2. 是否可以将图形或任何与此相关的 Swing 组件导出为基于矢量的格式,例如 EPS、PDF 或 SVG,而不会遇到太多麻烦?我找到了几个库(VectorGraphics2DFreeHEP) 用于在 Java 中管理基于矢量的图像,但是我不确定使用它们是否意味着我必须“重新绘制”每个顶点和边在图中。这显然不是很理想...
  3. 还有其他我可能错过的选择吗?

提前致谢,

In one of my projects I use JUNG2 to visualize a very large multiple-parent hierarchy graph, displayed in an applet. I would need to export the whole/parts of the graph to high resolution still images, since screenshots look hideous when printed (especially if the graph has been zoomed out).

The code I use currently is as follows:

public void writeToDisk(File saveToFolder, String filename) {
    //Dimension loDims = getGraphLayout().getSize();
    Dimension vsDims = getSize();

    int width = vsDims.width;
    int height = vsDims.height;
    Color bg = getBackground();

    BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
    Graphics2D graphics = im.createGraphics();
    graphics.setColor(bg);
    graphics.fillRect(0,0, width, height);
    paintComponent(graphics);

    try{
       ImageIO.write(im,"png",new File(saveToFolder,filename));
    }catch(Exception e){
        e.printStackTrace();
    }
}

This creates PNG images which are not particularly high resolution. So my questions are as follows:

  1. Is it possible to push up the PNG export resolution to 300 dpi?
  2. Is it possible to export the graph, or any swing component for that matter, to vector based formats such as EPS, PDF or SVG without too much hassle? I have found several libraries (VectorGraphics2D,FreeHEP) for managing vector based images in Java, however I am not sure if using them would mean that I have to "re-draw" each vertex and edge in the graph. That's obviously not very desirable...
  3. Are there any other alternatives which I might have missed?

Thanks in advance,

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

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

发布评论

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

评论(4

孤凫 2024-12-28 11:33:53

您可以使用 Xchart,然后使用 vectorgraphics2d 将图片导出为 SVG 或 PDF

you can use Xchart and then export pictures using vectorgraphics2d to SVG or PDF

£冰雨忧蓝° 2024-12-28 11:33:52

感谢您的建议,但我已经设法让 FreeHEP 矢量图形库 按照我想要的方式工作到。我分享下面的代码,以防有人遇到同样的问题。

上面提到的库有一个非常好的内置导出菜单,它可以处理导出到一堆不同的格式。修改后的“ModelGraphMouse”类

protected void handlePopup(MouseEvent e) {
        final VisualizationViewer<MyNode, MyEdge> vv = (VisualizationViewer<MyNode, MyEdge>)e.getSource();
        Point2D p = e.getPoint();
        GraphElementAccessor<MyNode, MyEdge> pickSupport = vv.getPickSupport();
        if(pickSupport != null) {
            final MyNode v = pickSupport.getVertex(vv.getGraphLayout(), p.getX(), p.getY());

            // if clicked on a vertex -> show info popup
            // else show contexual menu
            if(v != null) {
                JFrame popup = new JFrame("Node: " + v.getId());
                popup.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                ...
            } else{
                JPopupMenu menu = new JPopupMenu();
                JMenuItem exportGraphMenuItem = new JMenuItem("Export graph to vector image...");
                exportGraphMenuItem.addActionListener(new ExportActionListener((WritingVisualizationViewer<V, E>) vv));
                menu.add(exportGraphMenuItem);
                menu.show(e.getComponent(), e.getX(), e.getY());
            } 
        }
    }

和操作侦听器的代码摘录:

    public class ExportActionListener implements ActionListener{

    private VisualizationViewer<V, E> wvv;
    public ExportActionListener(VisualizationViewer<V, E> vv) {
        this.wvv = vv;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ExportDialog export = new ExportDialog();
        export.showExportDialog(wvv, "Export view as ...", wvv, "export");
    }
}

Thanks for the suggestions but I have managed to get FreeHEP Vector Graphics library working the way I want to. I am sharing the code below in case anyone runs into the same questions.

The above-named library has a very nice built-in export menu, which handles the export to a bunch of different formats. Code excerpt from the modified ´ModelGraphMouse´ class:

protected void handlePopup(MouseEvent e) {
        final VisualizationViewer<MyNode, MyEdge> vv = (VisualizationViewer<MyNode, MyEdge>)e.getSource();
        Point2D p = e.getPoint();
        GraphElementAccessor<MyNode, MyEdge> pickSupport = vv.getPickSupport();
        if(pickSupport != null) {
            final MyNode v = pickSupport.getVertex(vv.getGraphLayout(), p.getX(), p.getY());

            // if clicked on a vertex -> show info popup
            // else show contexual menu
            if(v != null) {
                JFrame popup = new JFrame("Node: " + v.getId());
                popup.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                ...
            } else{
                JPopupMenu menu = new JPopupMenu();
                JMenuItem exportGraphMenuItem = new JMenuItem("Export graph to vector image...");
                exportGraphMenuItem.addActionListener(new ExportActionListener((WritingVisualizationViewer<V, E>) vv));
                menu.add(exportGraphMenuItem);
                menu.show(e.getComponent(), e.getX(), e.getY());
            } 
        }
    }

and the action listener:

    public class ExportActionListener implements ActionListener{

    private VisualizationViewer<V, E> wvv;
    public ExportActionListener(VisualizationViewer<V, E> vv) {
        this.wvv = vv;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ExportDialog export = new ExportDialog();
        export.showExportDialog(wvv, "Export view as ...", wvv, "export");
    }
}
彻夜缠绵 2024-12-28 11:33:52

基本上一个PNG就足够了。 BufferedImage 中的分辨率维度是像素,而不是 dpi。因此,您需要将宽度高度加倍/三倍才能获得更好的分辨率。

Graphics2D 也可以针对 JUNG 图进行缩放。

Basically a PNG suffices. The dimension of resolution in a BufferedImage is pixels, not dpi. So you need to double/triple your width and height to receive a better resolution.

Graphics2D could scale too for the JUNG graphs.

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