public static void main(String[] args)
{
final JPanel panel1= new JPanel();
panel1.setLayout(new FlowLayout());
JButton b1 = new JButton( "画树 ");
panel1.add(b1);
b1.addActionListener(new ActionListener()
{
BitTree theTree = new BitTree();
public void actionPerformed(ActionEvent e)
{
theTree.root = theTree.setup(1,320,160);
Node2 node = theTree.root;
String tree_ch = String.valueOf(node.ch);
int circle_x = node.x;
int circle_y = node.y;
int line_x = circle_x + 15;
int line_y = circle_y + 40;
Graphics d_circle = panel1.getGraphics();
Graphics d_string = panel1.getGraphics();
Graphics d_line = panel1.getGraphics();
d_circle.setColor(Color.white);
d_circle.fillOval(circle_x,circle_y,40,40);
d_string.setColor(Color.red);
d_string.drawString(tree_ch,circle_x + 15,circle_y + 20);
d_line.drawLine(line_x,line_y,185,100);
}
});
JFrame test= new cooky_jframe();
test.setSize(640,480);
test.setContentPane(panel1);
test.setVisible(true);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
这里是画树的一个节点。。。不过为什么我画的节点都是创建树时最后创建的那个节点????????
下面是创建树部分
public static Node2 root;
public final char[] treeLine = { 'a ', 'b ', '# '};
//用于标志二叉树节点在数组中的存储位置,以便在创建二叉树时能够找到节点对应的数据。
static int index = 0;
public Node2 setup(int h,int t,int w)
{
if (index > = treeLine.length)
return root = null;
if (treeLine[index] == '# ')
return root = null;
if (treeLine[index] == ' ')
return root = null;
root = new Node2(treeLine[index],t,h*50);
index = index * 2 + 1;
// System.out.println(h);
/// System.out.println(t);
// System.out.println(w);
root.left = setup(h+1,t-w,w/2);