// An example of Force Directed Graphing using OO parent/child relationships // by Nathan Ramella (nar@hush.com) // // Original source used from GraphToy2 found at http://www.cricketschirping.com/weblog/?p=966 // by banksean import traer.physics.*; import traer.animation.*; public class Graph { Node selectedNode; Node dragNode; Node hoverNode; Node root = null; ParticleSystem ps; public Graph() { // Zero gravity, mid-to-low drag ps = new ParticleSystem(0.0, 0.0, 0.0, .1); // Make our root node, assign it the particle system // to give to our children. root = new Node("root", ps); root.getParticle().makeFixed(); } public Node getRoot() { return root; } public void setHoverNode(Node n) { hoverNode = n; } public void unsetHoverNode() { hoverNode = null; } public void setDragNode(Node n) { dragNode = n; } public Node getDragNode() { return dragNode; } public ArrayList getAllNodes(Node s) { ArrayList nodes = new ArrayList(); if ((s != null) && (s.hasChildren() == true)) { for (Iterator it=s.getChildren().iterator(); it.hasNext(); ) { Node n = (Node) it.next(); if (n.hasChildren()) { nodes.addAll(getAllNodes(n)); } else { nodes.add(n); } } } nodes.add(s); return nodes; } public void tick() { ps.advanceTime(1); } public void draw() { ArrayList nodes = new ArrayList(); nodes.addAll(getAllNodes(root)); if (nodes.size() > 0) { for (Iterator it=nodes.iterator(); it.hasNext(); ) { Node n = (Node) it.next(); n.draw(); } } root.draw(); } }