View Javadoc
1   /*
2   * Copyright (c) 2003, The JUNG Authors 
3   *
4   * All rights reserved.
5   *
6   * This software is open-source under the BSD license; see either
7   * "license.txt" or
8   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9   */
10  package edu.uci.ics.jung.visualization;
11  
12  import java.awt.Dimension;
13  import java.awt.Graphics2D;
14  import java.awt.Image;
15  import java.awt.RenderingHints;
16  import java.awt.geom.Point2D;
17  import java.awt.image.BufferedImage;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import edu.uci.ics.jung.algorithms.layout.Layout;
22  
23  /**
24   * A class that could be used on the server side of a thin-client application. It creates the jung
25   * visualization, then produces an image of it.
26   * @author tom
27   *
28   * @param <V> the vertex type
29   * @param <E> the edge type
30   */
31  @SuppressWarnings("serial")
32  public class VisualizationImageServer<V,E> extends BasicVisualizationServer<V,E> {
33  
34      Map<RenderingHints.Key, Object> renderingHints = new HashMap<RenderingHints.Key, Object>();
35      
36      /**
37       * Creates a new instance with the specified layout and preferred size.
38       * 
39       * @param layout the Layout instance; provides the vertex locations
40       * @param preferredSize the preferred size of the image
41       */
42      public VisualizationImageServer(Layout<V,E> layout, Dimension preferredSize) {
43          super(layout, preferredSize);
44          setSize(preferredSize);
45          renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
46          addNotify();
47      }
48      
49      public Image getImage(Point2D center, Dimension d) 
50      {
51          int width = getWidth();
52          int height = getHeight();
53          
54          float scalex = (float)width/d.width;
55          float scaley = (float)height/d.height;
56          try 
57          {
58              renderContext.getMultiLayerTransformer().getTransformer(Layer.VIEW).scale(scalex, scaley, center);
59      
60              BufferedImage bi = new BufferedImage(width, height,
61                      BufferedImage.TYPE_INT_RGB);
62              Graphics2D graphics = bi.createGraphics();
63              graphics.setRenderingHints(renderingHints);
64              paint(graphics);
65              graphics.dispose();
66              return bi;
67          } finally {
68          	renderContext.getMultiLayerTransformer().getTransformer(Layer.VIEW).setToIdentity();
69          }
70      }
71  }