1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.io;
15  
16  import java.awt.Color;
17  import java.awt.Font;
18  import java.awt.Graphics2D;
19  import java.awt.Rectangle;
20  import java.awt.font.TextLayout;
21  import java.awt.geom.Point2D;
22  import java.awt.geom.Rectangle2D;
23  import java.io.File;
24  import java.io.IOException;
25  
26  import y.base.Edge;
27  import y.base.Node;
28  import y.base.NodeList;
29  import y.io.JPGIOHandler;
30  import y.io.TiledImageOutputHandler;
31  import y.view.Drawable;
32  import y.view.Graph2D;
33  import y.view.Graph2DView;
34  import y.view.hierarchy.HierarchyManager;
35  
36  /**
37   * This class shows how to export a diagram to multiple image tiles. 
38   * Also, this demo shows how to add a title to the exported diagram.
39   * Executing the demo will generate multiple JPG images that make up a 
40   * diagram. Additionally, a HTML file will be produced that displays the
41   * generated image tiles properly arranged in a table. 
42   * Adding a title to the diagram is implemented by enlarging the image
43   * size and adding a title Drawable to the view.   
44   */
45  public class TiledImageDemo
46  {
47    
48    public TiledImageDemo(String imageFileBase)
49    {
50      Graph2D diagram = new Graph2D();
51      
52      generateDiagram(diagram);
53      
54      
55      //output to JPG. Other output handlers can be found in package y.io. 
56      JPGIOHandler jpgIO = new JPGIOHandler();
57      Graph2DView view = jpgIO.createDefaultGraph2DView(diagram);
58      
59      //add title to image
60      Point2D vp = view.getViewPoint2D();
61      view.setSize(view.getWidth(), (int)(view.getHeight()+50));
62      view.setViewPoint2D(vp.getX(), vp.getY()-50/view.getZoom());
63      Rectangle rect = view.getVisibleRect();
64      TitleDrawable td = new TitleDrawable(imageFileBase);
65      td.setFrame(rect.x,rect.y,rect.width,50/view.getZoom());  
66      view.addDrawable(td);
67    
68      diagram.setCurrentView(view);
69      
70      TiledImageOutputHandler tiledIO = new TiledImageOutputHandler(jpgIO);
71      tiledIO.setMaximumTileSize(500,500);
72      tiledIO.setHTMLTableGenerationActive(true);
73      
74      try
75      {
76        File file = new File(imageFileBase + ".html");
77        System.out.println("Writing HTML table for tiled images: " + file.getCanonicalPath());
78        tiledIO.write(diagram, imageFileBase + ".jpg");
79      }
80      catch(IOException ioex)
81      {
82        ioex.printStackTrace();
83      }
84      
85    }
86    
87    /**
88     * Drawable implementation that displays a title for a diagram.
89     */ 
90     static class TitleDrawable extends Rectangle implements Drawable {
91      String title;
92      TitleDrawable(String title) {
93        this.title = title;
94      }
95      public void paint(Graphics2D g)
96      {
97        g.setColor(Color.lightGray);
98        g.fillRect(x,y,width,height);
99        g.setColor(Color.black);
100       Font f = new Font("Dialog", Font.PLAIN, (int)(0.8*height));
101       TextLayout tl = new TextLayout(title,f, g.getFontRenderContext());
102       Rectangle2D rect = tl.getBounds();
103       tl.draw(g, (float)(x+(width-rect.getWidth())/2.0), (float)(y-rect.getY()+(height-rect.getHeight())/2.0));
104     }
105   };
106 
107   /**
108    * Build a tree structure and provide link hyperlink information
109    * for some nodes.
110    */
111   void generateDiagram(Graph2D graph)
112   {
113     HierarchyManager hm = new HierarchyManager(graph);
114 
115     NodeList queue = new NodeList();
116     queue.add(graph.createNode(0,0, 100, 30, "Root"));
117     for(int i = 0; i < 100; i++)
118     {
119       Node root = queue.popNode();
120       Node c1 = graph.createNode(0,0, 80, 30, "c1_" + graph.N());
121       Edge e1 = graph.createEdge(root, c1);
122       Node c2 = graph.createNode(0,0, 60, 30, "c2_" + graph.N());
123       Edge e2 = graph.createEdge(root, c2);
124       queue.add(c2);
125       queue.add(c1);
126     }
127 
128     //layout as a tree. 
129     y.layout.tree.TreeLayouter tLayouter = new y.layout.tree.TreeLayouter(); //@VIEW_EXCLUSION@
130     tLayouter.doLayout(graph); //@VIEW_EXCLUSION@
131     
132   }
133   
134   public static void main(String args[])
135   {
136     TiledImageDemo demo = new TiledImageDemo("TiledImageDemo");
137   }
138  
139 }
140 
141     
142 
143       
144