/Users/lyon/j4p/src/graphics/graph/Nodes.java

1    package graphics.graph; 
2     
3    import classUtils.putils.ClassPathUtils; 
4     
5    import java.io.File; 
6    import java.util.Vector; 
7     
8    /** 
9     *  The responsibility of this class is to maintain 
10    *  a collection of all of the nodes and be able to 
11    *  return any particular node or collection of nodes 
12    *  upon request, 
13    *  This class has been modified to store the nodes in 
14    *  the Vector nodeVector rather than the array nodes[]. 
15    */ 
16    
17   public class Nodes { 
18    
19    
20       private Vector nodeVector = new Vector(); 
21    
22       public Node getRandomNode() { 
23           Node n = getNode(getRandomNodeNumber()); 
24           return n; 
25       } 
26    
27       private int getRandomNodeNumber() { 
28           return (int) (Math.random() * getNumberOfNodes()); 
29       } 
30    
31       public Node getNode(int i) { 
32   //      return nodes[i]; 
33           return (Node) nodeVector.elementAt(i); 
34       } 
35    
36       int addNode(String lbl) { 
37           Class c = null; 
38           try { 
39               c = Class.forName(lbl); 
40           } catch (ClassNotFoundException cnfe) { 
41               // so deep in the code, noone will ever 
42               // find this...shhh 
43               // Java hack, alert! 
44               // if a file name is given, we will have to 
45               // parse the bytecodes to the class file 
46               // in order to extract the class. 
47               // Wacky stuff, huh? 
48               //  - Doug 
49    
50               return addNodeModified(lbl); 
51           } 
52           Node n = new Node(lbl, c); 
53           n.setX(50 + 380 * Math.random()); 
54           n.setY(50 + 380 * Math.random()); 
55           nodeVector.addElement(n); 
56           return nodeVector.size() - 1; 
57       } 
58    
59       int addNodeModified(String lbl) { 
60           Class c = null; 
61           String className = ClassPathUtils.classFileToClassName(new File(lbl)); 
62           try { 
63               c = Class.forName(className); 
64           } catch (ClassNotFoundException cnfe) { 
65               System.out.println( 
66                       "graphics.graph.Nodes.addNodeModified; can't find class:" + lbl); 
67           } 
68           Node n = new Node(lbl, c); 
69           n.setX(50 + 380 * Math.random()); 
70           n.setY(50 + 380 * Math.random()); 
71           nodeVector.addElement(n); 
72           return nodeVector.size() - 1; 
73       } 
74    
75       public int getNumberOfNodes() { 
76           return nodeVector.size(); 
77       } 
78    
79       /** 
80        * 
81        */ 
82       public void preturbJRelativetoI(int i, Node n1) { 
83           double dx = 0; 
84           double dy = 0; 
85           for (int j = 0; j < getNumberOfNodes(); j++) { 
86               if (i == j) continue; 
87               Node n2 = getNode(j); 
88    
89               double vx = n1.getX() - n2.getX(); 
90               double vy = n1.getY() - n2.getY(); 
91   // len = distance**2 between 
92   // the nodes. 
93               double len = vx * vx + vy * vy; 
94   //if (len == 0) { 
95   //dx += Math.random(); 
96   //dy += Math.random(); 
97    
98   //} else 
99               if (len < 100 * 100) { 
100                  dx += vx / len; 
101                  dy += vy / len; 
102              } 
103          } 
104   
105          double dlen = dx * dx + dy * dy; 
106          int interNodePixelSpread = 10; //pixels 
107          if (dlen > 0) { 
108              dlen = Math.sqrt(dlen) / interNodePixelSpread; 
109              n1.setDx(n1.getDx() + dx / dlen); 
110              n1.setDy(n1.getDy() + dy / dlen); 
111          } 
112   
113      } 
114   
115      public Node[] getFixedNodes() { 
116          Vector v = new Vector(); 
117          for (int i = 0; i < getNumberOfNodes(); i++) 
118              if (getNode(i).isFixed()) 
119                  v.addElement(getNode(i)); 
120          Node na[] = new Node[v.size()]; 
121          v.copyInto(na); 
122          return na; 
123      } 
124   
125      int findNode(String lbl) { 
126          for (int i = 0; i < getNumberOfNodes(); i++) { 
127              if (getNode(i).getLbl().equals(lbl)) 
128                  return i; 
129          } 
130          return addNode(lbl); 
131      } 
132  }