int nodeCount; Node[] nodes = new Node[100]; HashMap nodeTable = new HashMap( ); int edgeCount; Edge[] edges = new Edge[500]; static final color nodeColor = #F0C070; static final color selectColor = #FF3030; static final color fixedColor = #FF8080; static final color edgeColor = #000000; PFont font; void setup( ) { size(600, 600); loadData( ); font = createFont("SansSerif", 10); textFont(font); smooth( ); } void loadData( ) { addEdge("joe", "food"); addEdge("joe", "dog"); addEdge("joe", "tea"); addEdge("joe", "cat"); addEdge("joe", "table"); addEdge("table", "plate"); addEdge("plate", "food"); addEdge("food", "mouse"); addEdge("food", "dog"); addEdge("mouse", "cat"); addEdge("table", "cup"); addEdge("cup", "tea"); addEdge("dog", "cat"); addEdge("cup", "spoon"); addEdge("plate", "fork"); addEdge("dog", "flea1"); addEdge("dog", "flea2"); addEdge("flea1", "flea2"); addEdge("plate", "knife"); } void addEdge(String fromLabel, String toLabel) { Node from = findNode(fromLabel); Node to = findNode(toLabel); Edge e = new Edge(from, to); if (edgeCount == edges.length) { edges = (Edge[]) expand(edges); } edges[edgeCount++] = e; } Node findNode(String label) { label = label.toLowerCase( ); Node n = (Node) nodeTable.get(label); if (n == null) { return addNode(label); } return n; } Node addNode(String label) { Node n = new Node(label); if (nodeCount == nodes.length) { nodes = (Node[]) expand(nodes); } nodeTable.put(label, n); nodes[nodeCount++] = n; return n; } void draw( ) { background(255); for (int i = 0 ; i < edgeCount ; i++) { edges[i].relax( ); } for (int i = 0; i < nodeCount; i++) { nodes[i].relax( ); } for (int i = 0; i < nodeCount; i++) { nodes[i].update( ); } for (int i = 0 ; i < edgeCount ; i++) { edges[i].draw( ); } for (int i = 0 ; i < nodeCount ; i++) { nodes[i].draw( ); } } Node selection; void mousePressed( ) { // Ignore anything greater than this distance. float closest = 20; for (int i = 0; i < nodeCount; i++) { Node n = nodes[i]; float d = dist(mouseX, mouseY, n.x, n.y); if (d < closest) { selection = n; closest = d; } } if (selection != null) { if (mouseButton == LEFT) { selection.fixed = true; } else if (mouseButton == RIGHT) { selection.fixed = false; } } } void mouseDragged( ) { if (selection != null) { selection.x = mouseX; selection.y = mouseY; } } void mouseReleased( ) { selection = null; }