hi all, I've got this java applet code which is a graph including nodes and edges, I'm just wondering anyone know how to implement the code so that everytime we click on a node, only the nearest nodes are visible, the rest is invisible:
final Color fixedColor = Color.red; final Color selectColor = Color.pink; final Color edgeColor = Color.black; final Color nodeColor = new Color(250, 220, 100); final Color arcColor1 = Color.black; final Color arcColor2 = Color.pink; final Color arcColor3 = Color.red;
public void paintNode(Graphics g, Node n, FontMetrics fm) { int x = (int)n.x; int y = (int)n.y; g.setColor((n == pick) ? selectColor : (n.fixed ? fixedColor : nodeColor)); int w = fm.stringWidth(n.lbl) + 10; int h = fm.getHeight() + 4; g.fillRect(x - w/2, y - h / 2, w, h); g.setColor(Color.black); g.drawRect(x - w/2, y - h / 2, w-1, h-1); g.drawString(n.lbl, x - (w-10)/2, (y - (h-4)/2) + fm.getAscent()); }
public void start() { relaxer = new Thread(this); relaxer.start(); }
public void stop() { relaxer = null; }
}
public class Graph extends Applet implements ActionListener, ItemListener {
GraphPanel panel; Panel controlPanel;
public void init() { setLayout(new BorderLayout());
panel = new GraphPanel(this); add("Center", panel); controlPanel = new Panel();
String edges = getParameter("edges"); for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) { String str = t.nextToken(); int i = str.indexOf('-'); if (i > 0) { int len = 50; int j = str.indexOf('/'); if (j > 0) { len = Integer.valueOf(str.substring(j+1)).intValue(); str = str.substring(0, j); } panel.addEdge(str.substring(0,i), str.substring(i+1), len); } } Dimension d = getSize(); String center = getParameter("center"); if (center != null){ Node n = panel.nodes[panel.findNode(center)]; n.x = d.width / 2; n.y = d.height / 2; n.fixed = true; } }
public void destroy() { remove(panel); remove(controlPanel); }
public void start() { panel.start(); }
public void stop() { panel.stop(); }
public void actionPerformed(ActionEvent e) { Object src = e.getSource(); }
public void itemStateChanged(ItemEvent e) { Object src = e.getSource(); boolean on = e.getStateChange() == ItemEvent.SELECTED; }
public String[][] getParameterInfo() { String[][] info = { {"edges", "delimited string", "A comma-delimited list of all the edges. It takes the form of 'C-N1,C-N2,C-N3,C-NX,N1-N2/M12,N2-N3/M23,N3-NX/M3X,...' where C is the name of center node (see 'center' parameter) and NX is a node attached to the center node. For the edges connecting nodes to eachother (and not to the center node) you may (optionally) specify a length MXY separated from the edge name by a forward slash."}, {"center", "string", "The name of the center node."} }; return info; }
}
AND THIS IS THE CODE FOR THE HTML which run the applet:
<html> <head> <title>Assignment 2</title> </head> <body> <object codebase="." code="Graph.class" width=400 height=400> <param name=edges value="The_World-Asia/180,The_World-Europe/180,The_World-Africa/180,The_World-S outh_America/180,The_World-North_America/180,The_World-Australasia/180,Asia-Beij ing/90,Asia-Tokyo/90,Asia-Hong_Kong/90,Asia-New_Delhi/90,Asia-Manila/90,Asia-Jak arta/90,North_America-Honolulu/90,North_America-San_Francisco/90,North_America-L os_Angeles/90,North_America-Chicago/90,North_America-New_York/90,North_America-W ashington/90,South_America-Lima/90,South_America-Santiago/90,South_America-Rio_D e_Janeiro/90,Europe-London/90,Europe-Paris/90,Europe-Rome/90,Europe-Moscow/90,Af rica-Cairo/90,Africa-Nairobi/90,Africa-Cape_Town/90,Australasia-Sydney/90,Austra lasia-Melbourne/90,Australasia-Perth/90,Australasia-Auckland/90"> <param name=center value="The_World"> alt="Your browser probably doesn't understand the <OBJECT> tag so it isn't running the applet or perhaps you need a Java Plugin" Your browser is completely ignoring the <OBJECT> tag! </object> <hr> </body> </html>