2012-05-02 7 views
6

He estado buscando soluciones a mi pregunta, pero nada hace exactamente lo que quiero.JUNG: guardar todo el gráfico (no solo la parte visible) como imagen

Lo que quiero hacer es guardar todo un gráfico JUNG (con vértices personalizados y renderizado de bordes) en una imagen (PNG o JPEG). Cuando guardo VisualizationViewer en una Imagen Buffered, solo toma la parte visible. Quiero guardar el gráfico completo, así que esa no es una opción.

¿Alguien tiene una idea sobre cómo renderizar todo mi gráfico en una imagen?

¡Gracias de antemano!

Respuesta

11

Finalmente encontré la solución a mi problema, usando un VisualizationImageServer. Aquí es un ejemplo de cómo crear una imagen de un gráfico de toda JUNG, para otros que luchan con él:

import edu.uci.ics.jung.visualization.VisualizationImageServer; 

... 

// Create the VisualizationImageServer 
// vv is the VisualizationViewer containing my graph 
VisualizationImageServer<Node, Edge> vis = 
    new VisualizationImageServer<Node, Edge>(vv.getGraphLayout(), 
     vv.getGraphLayout().getSize()); 

// Configure the VisualizationImageServer the same way 
// you did your VisualizationViewer. In my case e.g. 

vis.setBackground(Color.WHITE); 
vis.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>()); 
vis.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<Node, Edge>()); 
vis.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>()); 
vis.getRenderer().getVertexLabelRenderer() 
    .setPosition(Renderer.VertexLabel.Position.CNTR); 

// Create the buffered image 
BufferedImage image = (BufferedImage) vis.getImage(
    new Point2D.Double(vv.getGraphLayout().getSize().getWidth()/2, 
    vv.getGraphLayout().getSize().getHeight()/2), 
    new Dimension(vv.getGraphLayout().getSize())); 

// Write image to a png file 
File outputfile = new File("graph.png"); 

try { 
    ImageIO.write(image, "png", outputfile); 
} catch (IOException e) { 
    // Exception handling 
} 
+0

Y, si usted necesita para ahorrar unos gráficos vectoriales (en lugar de PNG), ver esto: http://stackoverflow.com/questions/8518390/exporting-jung -graphs-to-hi-res-images-preferiblemente-based-based – bikashg

+0

@ dylan202 La imagen guardada no refleja ningún color, forma o visibilidad. ¿Te molestaste por estas cosas y encontraste una solución? – SacJn

0
BufferedImage image = (BufferedImage) vis.getImage(
new Point2D.Double(graph.getGraphLayout().getSize().getWidth()/2, 
graph.getGraphLayout().getSize().getHeight()/2), 
new Dimension(graph.getGraphLayout().getSize())); 

No existe tal método llamado "getGraphLayout" de la clase Gráfico sino de un visualizationviewer .

+0

¡Exactamente! Lo corregí :) – dylan202

1

Mi experiencia con la captura de imágenes de la manera en que lo sugirió dylan202 fue que la calidad de las imágenes no estaba a la altura. Como necesitaba las imágenes para mi presentación.

Otra forma de obtener imágenes de alta calidad de su red Jung es utilizar la biblioteca VectorGraphics de FreeHEP.

Utilicé esta biblioteca para generar imágenes en un archivo pdf. Luego tomé instantáneas de la imagen desde el pdf hasta mi presentación.

JPanel panel = new JPanel(); 
panel.setLayout(new FlowLayout()); 
panel.setBackground(Color.WHITE); 
panel.add(vv); 

Properties p = new Properties(); 
p.setProperty("PageSize","A4"); 

// vv is the VirtualizationViewer 

VectorGraphics g = new PDFGraphics2D(new File("Network.pdf"), vv);     

g.setProperties(p); 
g.startExport(); 
panel.print(g); 
g.endExport(); 

También es posible generar archivos JPEG u otro tipo de archivos. Por ejemplo para generar archivos SVG necesita sólo una línea que desea cambiar:

VectorGraphics g = new SVGGraphics2D(new File("Network.svg"), vv); 

Para más información ver la manual.

el zoom instantánea desde el archivo PDF Zoomed in snapshot from the PDF file

Cuestiones relacionadas