1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.io;
29  
30  import java.awt.Color;
31  import java.awt.Font;
32  import java.awt.Graphics2D;
33  import java.awt.Rectangle;
34  import java.awt.font.TextLayout;
35  import java.awt.geom.Point2D;
36  import java.awt.geom.Rectangle2D;
37  import java.io.File;
38  import java.io.IOException;
39  
40  import y.base.Edge;
41  import y.base.Node;
42  import y.base.NodeList;
43  import y.io.JPGIOHandler;
44  import y.io.TiledImageOutputHandler;
45  import y.view.Drawable;
46  import y.view.Graph2D;
47  import y.view.Graph2DView;
48  import y.view.hierarchy.HierarchyManager;
49  
50  /**
51   * This class shows how to export a diagram to multiple image tiles. 
52   * Also, this demo shows how to add a title to the exported diagram.
53   * Executing the demo will generate multiple JPG images that make up a 
54   * diagram. Additionally, a HTML file will be produced that displays the
55   * generated image tiles properly arranged in a table. 
56   * Adding a title to the diagram is implemented by enlarging the image
57   * size and adding a title Drawable to the view.   
58   */
59  public class TiledImageDemo
60  {
61    
62    public TiledImageDemo(String imageFileBase)
63    {
64      Graph2D diagram = new Graph2D();
65      
66      generateDiagram(diagram);
67      
68      
69      //output to JPG. Other output handlers can be found in package y.io. 
70      JPGIOHandler jpgIO = new JPGIOHandler();
71      Graph2DView view = jpgIO.createDefaultGraph2DView(diagram);
72      
73      //add title to image
74      Point2D vp = view.getViewPoint2D();
75      view.setSize(view.getWidth(), (int)(view.getHeight()+50));
76      view.setViewPoint2D(vp.getX(), vp.getY()-50/view.getZoom());
77      Rectangle rect = view.getVisibleRect();
78      TitleDrawable td = new TitleDrawable(imageFileBase);
79      td.setFrame(rect.x,rect.y,rect.width,50/view.getZoom());  
80      view.addDrawable(td);
81    
82      diagram.setCurrentView(view);
83      
84      TiledImageOutputHandler tiledIO = new TiledImageOutputHandler(jpgIO);
85      tiledIO.setMaximumTileSize(500,500);
86      tiledIO.setHTMLTableGenerationActive(true);
87      
88      try
89      {
90        File file = new File(imageFileBase + ".html");
91        System.out.println("Writing HTML table for tiled images: " + file.getCanonicalPath());
92        tiledIO.write(diagram, imageFileBase + ".jpg");
93      }
94      catch(IOException ioex)
95      {
96        ioex.printStackTrace();
97      }
98      
99    }
100   
101   /**
102    * Drawable implementation that displays a title for a diagram.
103    */ 
104    static class TitleDrawable extends Rectangle implements Drawable {
105     String title;
106     TitleDrawable(String title) {
107       this.title = title;
108     }
109     public void paint(Graphics2D g)
110     {
111       g.setColor(Color.lightGray);
112       g.fillRect(x,y,width,height);
113       g.setColor(Color.black);
114       Font f = new Font("Dialog", Font.PLAIN, (int)(0.8*height));
115       TextLayout tl = new TextLayout(title,f, g.getFontRenderContext());
116       Rectangle2D rect = tl.getBounds();
117       tl.draw(g, (float)(x+(width-rect.getWidth())/2.0), (float)(y-rect.getY()+(height-rect.getHeight())/2.0));
118     }
119   };
120 
121   /**
122    * Build a tree structure and provide link hyperlink information
123    * for some nodes.
124    */
125   void generateDiagram(Graph2D graph)
126   {
127     HierarchyManager hm = new HierarchyManager(graph);
128 
129     NodeList queue = new NodeList();
130     queue.add(graph.createNode(0,0, 100, 30, "Root"));
131     for(int i = 0; i < 100; i++)
132     {
133       Node root = queue.popNode();
134       Node c1 = graph.createNode(0,0, 80, 30, "c1_" + graph.N());
135       Edge e1 = graph.createEdge(root, c1);
136       Node c2 = graph.createNode(0,0, 60, 30, "c2_" + graph.N());
137       Edge e2 = graph.createEdge(root, c2);
138       queue.add(c2);
139       queue.add(c1);
140     }
141 
142     //layout as a tree. 
143     y.layout.tree.TreeLayouter tLayouter = new y.layout.tree.TreeLayouter(); 
144     tLayouter.doLayout(graph); 
145     
146   }
147   
148   public static void main(String[] args)
149   {
150     TiledImageDemo demo = new TiledImageDemo("TiledImageDemo");
151   }
152  
153 }
154 
155     
156 
157       
158