Exporting JUNG graphs to hi-res images (preferably vector based)

蹲街弑〆低调 提交于 2019-11-27 14:12:42

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!