将JUNG图写入图像:无法可靠地呈现完整的图形 [英] writing JUNG graphs to images: can't reliably render complete graph

查看:102
本文介绍了将JUNG图写入图像:无法可靠地呈现完整的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been using JUNG to visualize some simple graphs, and I'd like to write several of them to a PNG file. Unfortunately, the images often appear to render before the graph is finished painting, meaning that I get incomplete graphs -- that is, graphs where only a hanfdul of edges or nodes are drawn -- about half of the time. Rendering to screen works fine, which is part of why I'm so puzzled. As you'll see below, I tried a couple of workarounds, but they didn't help. It may be useful to know that the basicVisualizationServer I'm using won't paint anything useful to the BufferedImage directly -- I just get a black image when I try that.

谢谢!

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


推荐答案

我们通常希望保存被操纵图形的状态。我们按照我们喜欢的方式缩放和定位组件,然后我们制作容器的照片。这可以这样实现:

We usually want to save the state of a manipulated graph. We zoom and position the components the way we like and then we make a picture of the container. This can be achieved like this:


  1. 从优秀的 Rob Camick的博客

  2. 将JPanel传递到JScrollPane或任何组件中将您的JUNG2图形托管到ScreenImage.createImage以创建图像。

  1. Get the ScreenImage Class from the excellent Rob Camick's blog
  2. Pass the JPanel inside your JScrollPane, or any Component that hosts your JUNG2 graph to ScreenImage.createImage in order to create an image.

private void writeToImageFile(String imageFileName) {

   BufferedImage bufImage = ScreenImage.createImage((JComponent) jPanel1);
   try {
       File outFile = new File(imageFileName);
       ImageIO.write(bufImage, "png", outFile);
       System.out.println("wrote image to " + imageFileName);
   } catch (Exception e) {
       System.out.println("writeToImageFile(): " + e.getMessage());
   }
}


  • 另请阅读上述其他主题博客: - )

  • Read also other topics of the above mentioned blog :-)

    这篇关于将JUNG图写入图像:无法可靠地呈现完整的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆