Empty image created using saveImage

Help
Nicky123
2013-02-15
2013-05-29
  • Nicky123

    Nicky123 - 2013-02-15

    Empty images are created when saving graph to image using saveImage.

    I iterate an object List to create a graph of  each object's hierarchy.
    This hierarchy is drawn as Graph and added to Visualization.
    The display is then converted to an image using saveImage.

    Few images created are blank with no graphs and some have incomplete graph.

    Display and Visualization objects are created once.
    For every object
    1) new Graph is created and assigned to Visualization.
    2) Visualization run method is called.
    3) Display object is then saved into file using saveImage

    Please provide your help.

     
  • Björn Kruse

    Björn Kruse - 2013-02-16

    Visualization.run() activates some threading stuff, so maybe not everything is in place (and drawn) when you call Display.saveImage?
    Have you tried to do this with a little delay?

    best regards, Björn

     
  • Nicky123

    Nicky123 - 2013-02-18

    Hi,

    I tried your suggestion, but it didn't worked. All images are blank.
    I have pasted the code below.

    // this is the constructor
    public PrefuseTest1(String imageDirPath) {
            System.setProperty("java.awt.headless", "true");        
            m_vis = new Visualization();
            EdgeRenderer edgeR = new EdgeRenderer(prefuse.Constants.EDGE_TYPE_LINE,
                    prefuse.Constants.EDGE_ARROW_FORWARD);
            LabelRenderer nodeLabelRenderer = new LabelRenderer();
            nodeLabelRenderer.setHorizontalPadding(1);
            nodeLabelRenderer.setVerticalPadding(1);
            DefaultRendererFactory drf = new DefaultRendererFactory(
                    nodeLabelRenderer);
            m_vis.setRendererFactory(drf);
            drf = (DefaultRendererFactory) m_vis.getRendererFactory();
            ((LabelRenderer) drf.getDefaultRenderer()).setTextField("label");
            drf.setDefaultEdgeRenderer(edgeR);
            int[] palette = new int[] { ColorLib.rgb(105, 105, 105) };
            ShapeAction nodesShape = new ShapeAction(graphNodes,
                    Constants.SHAPE_RECTANGLE);
            SizeAction nodesSize = new SizeAction(graphNodes, 1.5);
            ColorAction nodesStroke = new ColorAction(graphNodes,
                    VisualItem.STROKECOLOR);
            nodesStroke.setDefaultColor(ColorLib.gray(100));
            DataColorAction nodesFillColor = new DataColorAction(graphNodes,
                    "label", Constants.NOMINAL, VisualItem.FILLCOLOR, palette);
            ColorAction nodesTextColor = new ColorAction(graphNodes,
                    VisualItem.TEXTCOLOR, ColorLib.rgb(245, 245, 245));
            ColorAction edgesStrokeColor = new ColorAction(graphEdges,
                    VisualItem.STROKECOLOR, ColorLib.gray(200));
            ColorAction edgesArrowFillColor = new ColorAction(graphEdges,
                    VisualItem.FILLCOLOR, ColorLib.gray(200));
            NodeLinkTreeLayout treeLayout = new NodeLinkTreeLayout(graphName);
            // treeLayout.setLayoutAnchor(new Point2D.Double(300, 80));
            treeLayout.setOrientation(Constants.ORIENT_TOP_BOTTOM);
            ActionList layout = new ActionList(Activity.DEFAULT_STEP_TIME);
            layout.add(treeLayout);
            // layout.add(new TreeRootAction(graph));
            layout.add(nodesStroke);
            layout.add(nodesShape);
            layout.add(nodesSize);
            layout.add(nodesFillColor);
            layout.add(nodesTextColor);
            layout.add(edgesStrokeColor);
            layout.add(edgesArrowFillColor);
            // m_vis.putAction("color", getDisplayActions());
            m_vis.putAction("layout", layout);
            display = new Display(m_vis);
            display.setSize(800, 500);
            display.setForeground(Color.GRAY);
            display.setBackground(Color.WHITE);
        }
    // method for saving image
    public void addGraphToVisualization(Graph graph, String fileName) {
            m_vis.addGraph("graph", graph);
            m_vis.runAfter("layout", 2000);
            try {
                OutputStream out = new BufferedOutputStream(new FileOutputStream(
                        fileName));
                display.saveImage(out, "jpg", 1);
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            m_vis.removeGroup("group");
            m_vis.cancel("color");
            m_vis.cancel("layout");
            m_vis.reset();
            graph.clear();
            graph = null;       
        }
    

    the main method has a list of object which is iterated and a new graph is created for each object in the list, which is then provided to

    addGraphToVisualization
    
     
  • Björn Kruse

    Björn Kruse - 2013-02-19

    m_vis.addGraph("graph", graph); m_vis.runAfter("layout", 2000);

    try { OutputStream out = new BufferedOutputStream(new FileOutputStream( fileName)); display.saveImage(out, "jpg", 1); out.flush(); out.close();

    }

    Vis.runAfter will Run all that stuff After two seconds (in a separate thread).
    I guess your Image is captured too early.

    With the little delay I meant a simple thread.sleep before the display.saveImage() call.

    Best regards, Björn

     
  • Nicky123

    Nicky123 - 2013-02-20

    Hello,

    Your solution did worked. Added

    Thread.sleep(500L); [\code]
     All images are now created.
    Thanks for your instant reply.
    
     
  • Nicky123

    Nicky123 - 2013-02-22

    Hello,

    Now i need to set Display window dynamically. Because some images are small and some are big.
    I need to capture whole graph into image.

    Currently i have set the display window as display.setSize(800, 500);
    due to this not entire graph is captured and the image is incomplete.

    I need to change it depending on the graph size.

    I need your help in this.

     
  • Björn Kruse

    Björn Kruse - 2013-02-22

    What about zooming in/out using display.zoom (or zoomAbs)?
    Also display.setHighQuality() might help you.

    display.getItemBounds() may help to see if all items are within the display.

    regards, Björn

     
  • Nicky123

    Nicky123 - 2013-02-28

    Thanks Björn for your suggestion. After changing Graph to Tree and using the zooming effect the images are now captured as desired.

    Thanks.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks