gpt4 book ai didi

java - 将 JUNG 图写入图像 : can't reliably render complete graph

转载 作者:搜寻专家 更新时间:2023-10-31 20:23:28 26 4
gpt4 key购买 nike

我一直在使用 JUNG 可视化一些简单的图形,我想将其中的几个写入 PNG 文件。不幸的是,图像通常在图形完成绘制之前呈现,这意味着我得到的图形不完整——也就是说,只有少数边缘或节点被绘制的图形——大约有一半的时间。渲染到屏幕效果很好,这就是我如此困惑的部分原因。正如您将在下面看到的,我尝试了一些解决方法,但它们没有帮助。知道我正在使用的 basicVisualizationServer 不会直接为 BufferedImage 绘制任何有用的东西可能会很有用——我尝试这样做时只会得到一个黑色图像。

谢谢!

  public void writeImage(String filename) {
Layout layout = new CircleLayout<V, E>(jungGraph);
layout.setSize(innerSize);
bvs = new BasicVisualizationServer<V,E>(layout);
float strokeWidth = 8f;
bvs.getRenderContext().setVertexShapeTransformer(new ConstantTransformer(new Ellipse2D.Float(-24,-24,48,48)));
bvs.getRenderContext().setArrowDrawPaintTransformer(new ConstantTransformer(Color.black));
bvs.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(strokeWidth)));
bvs.getRenderContext().setEdgeArrowStrokeTransformer(new ConstantTransformer(new BasicStroke(strokeWidth)));
bvs.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<E>());
bvs.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<V>());
bvs.setPreferredSize(viewSize);
//int width = bvs.getWidth(); // Always returns zero
int width = viewFrame.getWidth();
//int height = bvs.getHeight(); // Always returns zero
int height = viewFrame.getHeight();

BufferedImage bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bim.createGraphics();
viewFrame.paintAll(g);

g.dispose();
//this.viewFrame.paintComponents(g);
//try{Thread.sleep(1000);} catch(Exception e) {throw new RuntimeException(e);} // Sleeping doesn't help.
try {
File f = new File(filename);
ImageIO.write(bim,"png",f);
System.out.println("wrote image for " + jungGraph + " to "+ filename+ ":" + f.toString());
//try{Thread.sleep(500);} catch(Exception e) {throw new RuntimeException(e);} // Doesn't help
} catch (Exception e) {
throw new RuntimeException(e);
}
}

最佳答案

您还可以使用 VisualizationImageServer .它是 BasicVisualizationServer 的子类型,添加了 getImage 方法。我在正确渲染图表方面没有遇到任何问题。

你的代码会是这样的:

public void writeImage(String filename) {
Layout layout = new CircleLayout<V, E>(jungGraph);
layout.setSize(innerSize);
bvs = new VisualizationImageServer<V,E>(layout);
// [...]
BufferedImage image = (BufferedImage)bvs.getImage();
}

关于java - 将 JUNG 图写入图像 : can't reliably render complete graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5145331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com