| ImageMapDemo.java |
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.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34
35 import y.base.Edge;
36 import y.base.Node;
37 import y.base.NodeList;
38 import y.io.GIFIOHandler;
39 import y.io.ImageMapOutputHandler;
40 import y.io.LinkInfo;
41 import y.io.LinkMap;
42 import y.view.Graph2D;
43 import y.view.Graph2DView;
44
45 /**
46 * This class shows how to generate an image and a hyperlinked
47 * HTML image map of a graph.
48 *
49 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/image_export#image_maps" target="_blank">Section Image Maps</a> in the yFiles for Java Developer's Guide
50 */
51 public class ImageMapDemo
52 {
53
54 public ImageMapDemo(String imageFileName, String htmlFileName)
55 {
56 Graph2D tree = new Graph2D();
57
58 LinkMap linkMap = new LinkMap();
59
60 buildTreeFromData(tree, linkMap);
61
62 //layout as a tree. Other tree layout algorithms can be found
63 //in package y.layout.tree.
64 y.layout.tree.TreeLayouter tLayouter = new y.layout.tree.TreeLayouter();
65 tLayouter.doLayout(tree);
66
67 //output to GIF. Other output handlers can be found in package y.io.
68 GIFIOHandler gifIO = new GIFIOHandler();
69 Graph2DView view = gifIO.createDefaultGraph2DView(tree);
70 tree.setCurrentView(view);
71
72 //use ImageMapOutputHandler to generate an html image map that matches
73 //the generated image.
74 ImageMapOutputHandler htmlIO = new ImageMapOutputHandler();
75 linkMap.setMapName("image");
76 htmlIO.setReferences(linkMap);
77
78 try
79 {
80 File file = new File(imageFileName);
81 System.out.println("Writing GIF to " + file.getCanonicalPath());
82 gifIO.write(tree, imageFileName);
83
84 file = new File(htmlFileName);
85 System.out.println("Writing HTML to " + file.getCanonicalPath());
86
87 PrintWriter htmlOut = new PrintWriter(new FileWriter(htmlFileName));
88 String htmlMap = htmlIO.createHTMLString(tree);
89
90 //create valid html page that can be displayed in a browser.
91 htmlOut.println(
92 "<html>\n<head></head>\n<body>" +
93 htmlMap + "\n" +
94 "<img src=" + imageFileName + " usemap=\"#image\" border=\"0\">\n" +
95 "</body></html>");
96 htmlOut.close();
97 }
98 catch(IOException ioex)
99 {
100 ioex.printStackTrace();
101 }
102 }
103
104 /**
105 * Build a tree structure and provide link hyperlink information
106 * for some nodes.
107 */
108 void buildTreeFromData(Graph2D graph, LinkMap linkMap)
109 {
110 NodeList queue = new NodeList();
111 queue.add(graph.createNode(0,0, 100, 30, "Root"));
112 for(int i = 0; i < 10; i++)
113 {
114 Node root = queue.popNode();
115 LinkInfo link = new LinkInfo();
116 link.setAttribute(LinkInfo.HTML_REFERENCE, "http://www.yworks.com");
117 link.setAttribute(LinkInfo.HTML_ALT, "Visit yWorks");
118 link.setAttribute(LinkInfo.HTML_TITLE, "Visit yWorks");
119 linkMap.put(root, link);
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
125 linkMap.put(e1, link);
126 linkMap.put(e2, link);
127
128 queue.add(c2);
129 queue.add(c1);
130 }
131 }
132
133 public static void main(String[] args)
134 {
135 ImageMapDemo demo = new ImageMapDemo("ImageMapDemo.gif","ImageMapDemo.html");
136 }
137
138 }
139
140
141
142
143