1
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
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 y.layout.tree.TreeLayouter tLayouter = new y.layout.tree.TreeLayouter();
65 tLayouter.doLayout(tree);
66
67 GIFIOHandler gifIO = new GIFIOHandler();
69 Graph2DView view = gifIO.createDefaultGraph2DView(tree);
70 tree.setCurrentView(view);
71
72 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 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
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