将 JUNG 图导出为高分辨率图像(最好基于矢量)
在我的一个项目中,我使用 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 图像。所以我的问题如下:
- 是否可以将PNG导出分辨率提高到300 dpi?
- 是否可以将图形或任何与此相关的 Swing 组件导出为基于矢量的格式,例如 EPS、PDF 或 SVG,而不会遇到太多麻烦?我找到了几个库(VectorGraphics2D,FreeHEP) 用于在 Java 中管理基于矢量的图像,但是我不确定使用它们是否意味着我必须“重新绘制”每个顶点和边在图中。这显然不是很理想...
- 还有其他我可能错过的选择吗?
提前致谢,
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:
- Is it possible to push up the PNG export resolution to 300 dpi?
- 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...
- Are there any other alternatives which I might have missed?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Xchart,然后使用 vectorgraphics2d 将图片导出为 SVG 或 PDF
you can use Xchart and then export pictures using vectorgraphics2d to SVG or PDF
感谢您的建议,但我已经设法让 FreeHEP 矢量图形库 按照我想要的方式工作到。我分享下面的代码,以防有人遇到同样的问题。
上面提到的库有一个非常好的内置导出菜单,它可以处理导出到一堆不同的格式。修改后的“ModelGraphMouse”类
和操作侦听器的代码摘录:
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:
and the action listener:
基本上一个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
andheight
to receive a better resolution.Graphics2D could scale too for the JUNG graphs.
您可能想使用蜡染:http://xmlgraphics.apache.org/batik /using/svg-generator.html
You might wanna use Batik for that : http://xmlgraphics.apache.org/batik/using/svg-generator.html