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