6

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,

posdef
  • 6,498
  • 11
  • 46
  • 94
  • Maybe, you can convert your resulting raster/bitmap image file into a vector image file later using a third-party library like Autotrace [http://autotrace.sourceforge.net/](http://autotrace.sourceforge.net/) – ecle Dec 15 '11 at 11:22
  • Were you able to solve this problem ? Because none of the following answers actually work @posdef – SacJn Mar 20 '17 at 10:01
  • @SacJn the answer i've posted below **did** work at the time, I can't say if the libraries have changed in a way that breaks that solution though. I am not working on that project anymore #orphanproject #programminginacademia :) – posdef Mar 20 '17 at 10:17

4 Answers4

6

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");
    }
}
posdef
  • 6,498
  • 11
  • 46
  • 94
  • 2
    And if you just want to directly save the svg image (without the export dialog), Properties p = new Properties(); p.setProperty("PageSize","A5"); VectorGraphics g = new SVGGraphics2D(new File("Output.svg"), new Dimension(400,300)); g.setProperties(p); g.startExport(); wvv.print(g); g.endExport(); – Bikash Gyawali Jul 18 '13 at 08:30
  • @bikashg: I tried to do that to generate a pdf or jpg file as follows: VectorGraphics g = new SVGGraphics2D(new File("Output.jpg"), new Dimension(400,300)); But it does not work. – 2c00L Oct 24 '14 at 09:06
  • @Ashfaqur: You only created the graphics object with that stament. Now, you need to print it to a file using g.startExport(); wvv.print(g); g.endExport(); – Bikash Gyawali Oct 26 '14 at 09:45
  • I solved the problem, it was actually simple. VectorGraphics g = new PDFGraphics2D(new File("Output.pdf"), new Dimension(400,300)); This creates a pdf with very high resolution graphics. Ofcourse I have the other codes for exporting. – 2c00L Oct 26 '14 at 14:42
  • Also, need to have this : BasicVisualizationServer vv = new BasicVisualizationServer(layout); VisualizationImageServer wvv = new VisualizationImageServer(vv.getGraphLayout(), vv.getGraphLayout().getSize()); – Bikash Gyawali Jan 05 '15 at 11:20
1

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

You might wanna use Batik for that : http://xmlgraphics.apache.org/batik/using/svg-generator.html

JRe
  • 31
  • 1
  • 3
0

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