KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Grapher


1 import java.io.*;
2 import java.net.*;
3 import java.util.*;
4 import java.awt.*;
5 import javax.swing.*;
6
7 public class Grapher extends Canvas
8 {
9
10 public String JavaDoc files[] = new String JavaDoc[] {"JFtp.java", "LoadSet.java","EventCollector.java", "EventProcessor.java", "FtpEvent.java","DirCellRenderer.java","DirPanel.java",
11 "Displayer.java","HostChooser.java","HostList.java","Properties.java","Updater.java","GUIDefaults.java","HPasswordField.java",
12 "DataConnection.java","FtpClient.java","FtpConnection.java","FtpConstants.java","FtpServerSocket.java","FtpURLConnection.java",
13 "FtpURLStreamHandler.java","JConnection.java","LocalIO.java","Log.java","Log4JLogger.java","Logger.java","SystemLogger.java",
14 "CommandLine.java","SaveSet.java","Settings.java","Acceptor.java","Event.java","EventHandler.java","FtpEventConstants.java",
15 "FtpEventHandler.java","AutoRemover.java","Creator.java","DirCanvas.java","DirEntry.java","DirLister.java","DownloadList.java",
16 "LoadPanel.java","PathChanger.java","RemoteCommand.java","Remover.java","RemoverQuery.java","ResumeDialog.java",
17 "StatusCanvas.java","StatusPanel.java","Template.java","HFrame.java","HImage.java","HImageButton.java","HPanel.java",
18 "HTextField.java","ConnectionHandler.java","ConnectionListener.java","FtpServer.java","Transfer.java","StringUtils.java" };
19
20 public String JavaDoc prefix = "/home/cdemon/JFtp/j-ftp/src/java/net/sf/jftp/";
21 public String JavaDoc[] paths = new String JavaDoc[] { prefix, prefix+"gui/", prefix+"net/", prefix+"util/", prefix+"config/", prefix+"gui/framework",
22                                    prefix+"event/" };
23
24 public Hashtable table = new Hashtable();
25 public Hashtable pool = new Hashtable();
26
27 public static int width = 800;
28 public static int height = 600;
29
30 public Grapher()
31 {
32  setSize(width,height);
33
34  try
35  {
36     for(int i=0; i<files.length; i++)
37     {
38         File f = getFile(files[i]);
39
40         if(f != null && f.exists())
41         {
42             for(int j=0; j<files.length; j++)
43             {
44                 int x = countRelations(f, files[j]);
45                 if(x > 0) table.put(files[i] + ":" +files[j].substring(0, files[j].indexOf(".java")), new String JavaDoc(""+x));
46             }
47         }
48     }
49
50  show();
51  repaint();
52  }
53  catch(Exception JavaDoc ex)
54  {
55     ex.printStackTrace();
56  }
57 }
58
59 public void paint(Graphics g)
60 {
61     // init
62
g.setColor(Color.white);
63     g.fillRect(0,0,getSize().width, getSize().height);
64     g.setColor(Color.blue);
65     g.setFont(new Font("serif", Font.BOLD, 14));
66
67     // points
68
Random r = new Random();
69
70     for(int i=0; i<files.length; i++)
71     {
72         while(true)
73         {
74             int x = r.nextInt(width-200);
75             int y = r.nextInt(height-20);
76             if(y<30) y=30;
77
78             if(check(x, y))
79             {
80                 //System.out.println("adding: " + files[i]);
81
String JavaDoc tmp = files[i].substring(0,files[i].indexOf(".java"));
82                 Point p = new Point(x, y);
83                 pool.put(tmp, p);
84
85                 linkPoints(g, p);
86
87                 //g.drawString(tmp, x, y);
88

89                 break;
90             }
91         }
92     }
93
94     g.setColor(Color.blue);
95
96     for(int i=0; i<files.length; i++)
97     {
98         String JavaDoc tmp = files[i].substring(0,files[i].indexOf(".java"));
99         Point p2 = (Point) pool.get(tmp);
100         if(p2 == null) continue;
101         //g.setColor(new Color(255,180,180));
102
//g.fillRect((int) p2.getX(), (int) p2.getY()-12, 80, 15);
103
//g.setColor(Color.blue);
104
g.drawString(tmp, (int) p2.getX(), (int) p2.getY());
105     }
106
107 }
108
109 public void linkPoints(Graphics g, Point p)
110 {
111     // fill
112

113     Enumeration k = table.keys();
114     Enumeration e = table.elements();
115     String JavaDoc xk = null;
116     String JavaDoc file = null;
117     String JavaDoc link = null;
118
119     while(k.hasMoreElements())
120     {
121         xk = (String JavaDoc) k.nextElement();
122         int x = Integer.parseInt((String JavaDoc)e.nextElement());
123
124         file = xk.substring(0, xk.indexOf(":"));
125         file = file.substring(0,file.indexOf(".java"));
126         link = xk.substring( xk.indexOf(":")+1);
127
128         //System.out.println("<" + file + "> " + "(" + link + ")" + " - " + x);
129
Point x2 = (Point) pool.get(file);
130         if(x2 == null) continue;
131
132         if(x > 0)
133         {
134             //for(int y=0; y<x; y++)
135
//{
136
int color = 255 - x*10 +40;
137                 if(color < 0) color=0;
138                 if(color > 255) color = 255;
139
140                 g.setColor(new Color(color,color,color));
141                 if(color < 255) g.drawLine(((int)p.getX()+20),((int)p.getY()+15),((int)x2.getX()+20),((int)x2.getY()+15));
142             //}
143
}
144
145     }
146 }
147
148
149 public boolean check(int x, int y)
150 {
151     Enumeration e = pool.elements();
152     Point d = null;
153     int a;
154     int b;
155
156     while(e.hasMoreElements())
157     {
158         d = (Point) e.nextElement();
159         a = (int) d.getX();
160         b = (int) d.getY();
161
162         if(a > x-100 && a < x+100) {
163             if(b > y-20 && b < y +20)
164             {
165                 return false;
166             }
167         }
168     }
169
170     return true;
171 }
172
173 public int countRelations(File f, String JavaDoc what) throws IOException
174 {
175     int x = 0;
176     String JavaDoc tmp;
177     what = what.substring(0, what.indexOf(".java"));
178
179     URL url = f.toURL();
180     DataInputStream in = new DataInputStream(new BufferedInputStream(url.openStream()));
181
182     while(true)
183     {
184         tmp = in.readLine();
185         //System.out.println(f.getAbsolutePath() + ": " + tmp + ": " +what);
186
if(tmp == null) break;
187         if(tmp.indexOf(what) >= 0) x++;
188     }
189
190     in.close();
191
192     return x;
193 }
194
195 public File getFile(String JavaDoc name)
196 {
197     for(int i=0; i<paths.length; i++)
198     {
199         File f = new File(paths[i]+name);
200         if(f.exists()) return f;
201     }
202
203     return null;
204 }
205
206 public static void main(String JavaDoc argv[])
207 {
208     Grapher g = new Grapher();
209     JFrame j = new JFrame();
210     j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
211     j.getContentPane().setLayout(new BorderLayout(5,5));
212     j.setSize(width+10,height+25);
213     j.getContentPane().add("Center",g);
214     j.show();
215 }
216
217 }
218
Popular Tags