001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.main.saveload.image;
005    
006    import graphlab.graph.graph.AbstractGraphRenderer;
007    import graphlab.graph.graph.GraphModel;
008    
009    import javax.imageio.ImageIO;
010    import java.awt.*;
011    import java.awt.image.BufferedImage;
012    import java.awt.image.RenderedImage;
013    import java.io.File;
014    import java.io.IOException;
015    
016    
017    /**
018     * @author Azin Azadi
019     */
020    public class Image {
021    //    public static void SaveGraph2Image(GraphModel g){
022        //
023        //    }
024        static boolean debug = false;
025    
026        public static BufferedImage Graph2Image(AbstractGraphRenderer gv, GraphModel gm) {
027            Rectangle r = gm.getZoomedBounds().getBounds();
028            if (debug) System.out.println("start");
029            //todo: the following line is depended on graph view to be a swing component
030            BufferedImage bi = gv.getGraphicsConfiguration().createCompatibleImage(r.width, r.height);
031            if (debug) System.out.println("bi created");
032            Graphics2D gr = bi.createGraphics();
033            gr.translate(-r.x, -r.y);
034    //        if (gv.getMiny() < 0)
035            gr.translate(gv.getMinx(), gv.getMiny());
036            gv.paint((Graphics) gr);
037            if (debug) System.out.println("bi painted");
038            gr.translate(r.x, r.y);
039            return bi;
040        }
041    
042        public static void save(RenderedImage image, File ff, String extension) {
043            try {
044                if (debug) System.out.println("writing started");
045                ImageIO.write(image, extension, ff);
046                if (debug) System.out.println("writing finished");
047            } catch (IOException e) {
048                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
049            }
050    
051    //        fc.setTitle("SaveImage dijkstra.Image As ...");
052            //      fc.sa
053    
054        }
055    }