1
14 package demo.io;
15
16 import java.io.File;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.io.PrintWriter;
20
21 import y.base.Edge;
22 import y.base.Node;
23 import y.base.NodeList;
24 import y.io.GIFIOHandler;
25 import y.io.ImageMapOutputHandler;
26 import y.io.LinkInfo;
27 import y.io.LinkMap;
28 import y.view.Graph2D;
29 import y.view.Graph2DView;
30
31
35 public class ImageMapDemo
36 {
37
38 public ImageMapDemo(String imageFileName, String htmlFileName)
39 {
40 Graph2D tree = new Graph2D();
41
42 LinkMap linkMap = new LinkMap();
43
44 buildTreeFromData(tree, linkMap);
45
46 y.layout.tree.TreeLayouter tLayouter = new y.layout.tree.TreeLayouter(); tLayouter.doLayout(tree);
51 GIFIOHandler gifIO = new GIFIOHandler();
53 Graph2DView view = gifIO.createDefaultGraph2DView(tree);
54 tree.setCurrentView(view);
55
56 ImageMapOutputHandler htmlIO = new ImageMapOutputHandler();
59 linkMap.setMapName("image");
60 htmlIO.setReferences(linkMap);
61
62 try
63 {
64 File file = new File(imageFileName);
65 System.out.println("Writing GIF to " + file.getCanonicalPath());
66 gifIO.write(tree, imageFileName);
67
68 file = new File(htmlFileName);
69 System.out.println("Writing HTML to " + file.getCanonicalPath());
70
71 PrintWriter htmlOut = new PrintWriter(new FileWriter(htmlFileName));
72 String htmlMap = htmlIO.createHTMLString(tree);
73
74 htmlOut.println(
76 "<html>\n<head></head>\n<body>" +
77 htmlMap + "\n" +
78 "<img src=" + imageFileName + " usemap=\"#image\" border=\"0\">\n" +
79 "</body></html>");
80 htmlOut.close();
81 }
82 catch(IOException ioex)
83 {
84 ioex.printStackTrace();
85 }
86 }
87
88
92 void buildTreeFromData(Graph2D graph, LinkMap linkMap)
93 {
94 NodeList queue = new NodeList();
95 queue.add(graph.createNode(0,0, 100, 30, "Root"));
96 for(int i = 0; i < 10; i++)
97 {
98 Node root = queue.popNode();
99 LinkInfo link = new LinkInfo();
100 link.setAttribute(LinkInfo.HTML_REFERENCE, "http://www.yworks.com");
101 link.setAttribute(LinkInfo.HTML_ALT, "Visit yWorks");
102 link.setAttribute(LinkInfo.HTML_TITLE, "Visit yWorks");
103 linkMap.put(root, link);
104 Node c1 = graph.createNode(0,0, 80, 30, "c1_" + graph.N());
105 Edge e1 = graph.createEdge(root, c1);
106 Node c2 = graph.createNode(0,0, 60, 30, "c2_" + graph.N());
107 Edge e2 = graph.createEdge(root, c2);
108
109 linkMap.put(e1, link);
110 linkMap.put(e2, link);
111
112 queue.add(c2);
113 queue.add(c1);
114 }
115 }
116
117 public static void main(String args[])
118 {
119 ImageMapDemo demo = new ImageMapDemo("ImageMapDemo.gif","ImageMapDemo.html");
120 }
121
122 }
123
124
125
126
127