/Users/lyon/j4p/src/graphics/graph/RelaxThreadMgr.java
|
1 package graphics.graph;
2
3 import java.awt.*;
4
5 public class RelaxThreadMgr implements Runnable
6 /**
7 * This class used to be the RelaxPanel and before that all of the
8 * code here was included in the GraphPanel.
9 * Its responsibility is to maintain a Thread that will randomly
10 * "relax" or move the nodes around once every 1/10 of a second.
11 * A Component is passed in as part of the Constructor. After the
12 * appropriately "relaxing" has been calculated, a message is sent
13 * to repaint the component.
14 */ {
15 private boolean random;
16 private Nodes nodes = GraphFactory.getNodes();
17 private Thread relaxerThread = new Thread(this);
18 private EdgesManager em = GraphFactory.getEdgesManager();
19 private Component component = null;
20
21 public RelaxThreadMgr(Component c) {
22 component = c;
23 }
24
25 private void relaxer(Thread me) {
26 while (relaxerThread == me) {
27 relax();
28 if (random && (Math.random() < 0.03)) {
29 Node n = nodes.getRandomNode();
30 if (!n.isFixed())
31 Node.nIsNotFixed(n);
32 }
33 try {
34 Thread.sleep(100);
35 } catch (InterruptedException e) {
36 break;
37 }
38 }
39 }
40
41 public void setRandom(boolean b) {
42 random = b;
43 }
44
45 synchronized void relax() {
46 em.setupEdges();
47 for (int i = 0; i < nodes.getNumberOfNodes(); i++)
48 nodes.preturbJRelativetoI(i, nodes.getNode(i));
49 for (int i = 0; i < nodes.getNumberOfNodes(); i++)
50 nodes.getNode(i).nugeNode(
51 component.getSize());
52 component.repaint();
53 }
54
55 public void start() {
56 relaxerThread = new Thread(this);
57 relaxerThread.start();
58 }
59
60 public void run() {
61 relaxer(Thread.currentThread());
62 }
63 }