将JUNG图导出为高分辨率图像(最好是基于矢量的) [英] Exporting JUNG graphs to hi-res images (preferably vector based)

查看:142
本文介绍了将JUNG图导出为高分辨率图像(最好是基于矢量的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个项目中,我使用JUNG2来显示一个非常大的多父层次结构图,显示在一个applet中。我需要将图形的整个/部分导出为高分辨率静止图像,因为打印时屏幕截图看起来很可怕(特别是如果图形已经缩小)。

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();
    }
}

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

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


  1. 是否可以将PNG导出分辨率提高到300 dpi?

  2. 是否可以将图形或任何摆动组件导出到基于矢量的格式(如EPS,PDF或SVG)而不会有太多麻烦?我找到了几个库( VectorGraphics2D FreeHEP )用于管理Java中基于矢量的图像,但我不确定是否使用它们意味着我必须重新绘制每个顶点和边缘在图中。这显然不是很理想...

  3. 我可能错过了其他任何替代方案吗?

  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?

提前致谢,

推荐答案

感谢您的建议,但我设法得到 FreeHEP矢量图形库以我想要的方式工作。我正在分享下面的代码,以防有人遇到相同的问题。

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.

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

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());
            } 
        }
    }

和动作监听器:

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

这篇关于将JUNG图导出为高分辨率图像(最好是基于矢量的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆