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

1    package graphics.graph; 
2     
3     
4    public class EdgesManager { 
5        private StressBean sb = StressBean.getStressBean(); 
6        private EdgesBean eb = EdgesBean.getEdgesBean(); 
7        private ColorPanel cp = GraphFactory.getColorPanel(); 
8        private Nodes nodes = GraphFactory.getNodes(); 
9        private ImageUtil imageUtil; 
10       public ControlPanel controlPanel; 
11    
12       private ImageUtil getImageUtil() 
13               /** 
14                *  This is created using laissez-faire initialization 
15                *  because you cannot reference this in the constructor 
16                */ { 
17           if (imageUtil == null) { 
18               imageUtil = GraphFactory.getImageUtil(); 
19           } 
20           return imageUtil; 
21       } 
22    
23       public void drawArcAndString(int i) { 
24           // Edge e = edges[i]; 
25           Edge e = eb.getEdge(i); 
26    
27           Node fromNode = nodes.getNode(e.getFrom()); 
28           int x1 = (int) fromNode.getX(); 
29           int y1 = (int) fromNode.getY(); 
30           Node toNode = nodes.getNode(e.getTo()); 
31           int x2 = (int) toNode.getX(); 
32           int y2 = (int) toNode.getY(); 
33           int dx = (x1 - x2); 
34           int dy = (y1 - y2); 
35           int len = (int) Math.abs(Math.sqrt(dx * dx + dy * dy) - e.getLen()); 
36           getImageUtil().getOffGraphics().setColor((len < 10) 
37                   ? cp.getArcColor1() 
38                   : (len < 20 ? cp.getArcColor2() : cp.getArcColor3())); 
39           getImageUtil().getOffGraphics().drawLine(x1, y1, x2, y2); 
40           if (sb.getStress()) 
41               drawArcString(len, x1, x2, y1, y2); 
42       } 
43    
44       private void drawArcString(int len, int x1, int x2, int y1, int y2) { 
45           String lbl = String.valueOf(len); 
46           getImageUtil().getOffGraphics().setColor(cp.getStressColor()); 
47           getImageUtil().getOffGraphics().drawString( 
48                   lbl, x1 + (x2 - x1) / 2, y1 + (y2 - y1) / 2); 
49           getImageUtil().getOffGraphics().setColor(cp.getEdgeColor()); 
50       } 
51    
52       public void setupEdges() { 
53           for (int i = 0; i < eb.getNumberOfEdges(); i++) { 
54               // Edge e = edges[i]; 
55               Edge e = eb.getEdge(i); 
56               double vx = nodes.getNode(e.getTo()).getX() 
57                       - nodes.getNode(e.getFrom()).getX(); 
58               double vy = nodes.getNode(e.getTo()).getY() 
59                       - nodes.getNode(e.getFrom()).getY(); 
60               double len = Math.sqrt(vx * vx + vy * vy); 
61               len = (len == 0) ? .0001 : len; 
62               double f = (e.getLen() - len) 
63                       / (len * 3); 
64               double dx = f * vx; 
65               double dy = f * vy; 
66    
67               nodes.getNode(e.getTo()).incrementDxDy(dx, dy); 
68               nodes.getNode(e.getFrom()).incrementDxDy(-dx, -dy); 
69           } 
70       } 
71   }