//rootノードの色設定 final Color fixedColor = Color.red; //マウスで選択したときの色 final Color selectColor = Color.pink; // final Color edgeColor = Color.black; //nodeの色設定 //final Color nodeColor = new Color(250, 220, 100); final Color nodeColor1 = Color.green; final Color nodeColor2 = Color.blue; final Color nodeColor3 = Color.gray; final Color nodeColor4 = Color.magenta; final Color nodeColor5 = Color.orange; final Color nodeColor6 = Color.yellow; final Color nodeColor7 = Color.cyan; final Color nodeColor8 = Color.red;
//node間の線の色の設定 final Color stressColor = Color.darkGray; 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;
offgraphics.setColor(getBackground()); offgraphics.fillRect(0, 0, d.width, d.height); for (int i = 0 ; i < nedges ; i++) { Edge e = edges[i]; int x1 = (int)nodes[e.from].x; int y1 = (int)nodes[e.from].y; int x2 = (int)nodes[e.to].x; int y2 = (int)nodes[e.to].y; int len = (int)Math.abs(Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) - e.len);
//パラメータの取得 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); } }
//rootの配置 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();
if (src == scramble) { //play(getCodeBase(), "audio/computer.au"); Dimension d = getSize(); for (int i = 0 ; i < panel.nnodes ; i++) { Node n = panel.nodes[i]; if (!n.fixed) { n.x = 10 + (d.width-20)*Math.random(); n.y = 10 + (d.height-20)*Math.random(); } } return; }
if (src == shake) { //play(getCodeBase(), "audio/gong.au"); Dimension d = getSize(); for (int i = 0 ; i < panel.nnodes ; i++) { Node n = panel.nodes[i]; if (!n.fixed) { n.x += 80*Math.random() - 40; n.y += 80*Math.random() - 40; } } }
}
public void itemStateChanged(ItemEvent e) { Object src = e.getSource(); boolean on = e.getStateChange() == ItemEvent.SELECTED; if (src == stress) panel.stress = on; }
public String getAppletInfo() { return "Title: GraphLayout \nAuthor: <unknown>"; }
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 each other (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; }
public class GUI extends Applet{
public static void main(String[] args){
GUI app = new GUI();
app.showFrame(GUI.class.getName());
}
public void showFrame(String title){
Frame f = new Frame(title);
WindowListener l = new WindowAdapter(){
public void windowClosing(WindowEvent e){
stop();
destroy();
int NORMAL_STATUS = 0;
System.exit(NORMAL_STATUS);
}
};
f.addWindowListener(l);
init();
start();
f.add("Center",this);
f.pack();
f.setSize(200,200);
f.setVisible(true);
}
public void init()
{
add(getContainer());
}
public void start()
{
}
public Container getContainer()
{
JPanel panel = new JPanel();
panel.add(new JLabel("Hello World."));
return panel;
}
}