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

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

我目前使用的代码如下:

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. 是否可以将图形或任何摆动组件导出到基于矢量的格式(如EPS,PDF或SVG)而不会有太多麻烦? 我发现了几个用于在Java中管理基于矢量的图像的库( VectorGraphics2D , FreeHEP ),但我不确定是否使用它们意味着我必须“重新绘制”图中的每个顶点和边缘。 这显然不是很理想……
  3. 还有其他我可能错过的选择吗?

提前致谢,

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

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

 protected void handlePopup(MouseEvent e) { final VisualizationViewer vv = (VisualizationViewer)e.getSource(); Point2D p = e.getPoint(); GraphElementAccessor 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) vv)); menu.add(exportGraphMenuItem); menu.show(e.getComponent(), e.getX(), e.getY()); } } } 

和动作监听器:

  public class ExportActionListener implements ActionListener{ private VisualizationViewer wvv; public ExportActionListener(VisualizationViewer vv) { this.wvv = vv; } @Override public void actionPerformed(ActionEvent e) { ExportDialog export = new ExportDialog(); export.showExportDialog(wvv, "Export view as ...", wvv, "export"); } } 

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

对于JUNG图,Graphics2D也可以扩展。

您可能想要使用Batik: http : //xmlgraphics.apache.org/batik/using/svg-generator.html

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