1. 程式人生 > >[Java]遞迴畫樹

[Java]遞迴畫樹

package cn.test.javase;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaintTree {
	private static JPanel panel = null;
	private static ArrayList<Branch> list = new ArrayList<Branch>();
	private static int RANGE = 128;

	public static void main(String[] args) {
		JFrame frame = JFrameFactory.createJFrame("PaintTree");
		panel = new JPanel() {
			private static final long serialVersionUID = -3255059852702250875L;

			public void paint(Graphics g) {
				Graphics2D d = (Graphics2D)g;
				if (!(list == null || list.size() == 0)) {
					for (Branch branch : list) {
						d.setStroke(new BasicStroke((float) ((8 - branch.level) * 0.5)));
						d.setColor(Color.black);
						d.drawLine(branch.start.x, branch.start.y,
								branch.end.x, branch.end.y);
					}
				}
			}
		};
		frame.add(panel);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		list.add(new Branch(0, new Point(250, 400), new Point(250, 300)));
		createTree(list, 1, 1, 8);
		panel.repaint();
	}

	private static void createTree(ArrayList<Branch> list, int direction,
			int level, int levelAll) {
		// 生成樹
		// 1 23 4567
		if (level == levelAll) {
			return;
		} else {
			for (int i = 0; i < Math.pow(2, level); i++) {
				int position = (int) (Math.pow(2, level) - 1 + i);
				Branch parent = list.get(position / 2);
				int way = level == 1 ? 0 : new Random().nextInt(4);
				list.add(new Branch(
						level,
						parent.end,
						way == 0 ? new Point(parent.end.x
								- new Random().nextInt(RANGE), parent.end.y
								- new Random().nextInt(RANGE))
								: way == 1 ? new Point(parent.end.x
										+ new Random().nextInt(RANGE),
										parent.end.y
												+ new Random().nextInt(RANGE))
										: way == 2 ? new Point(parent.end.x
												- new Random().nextInt(RANGE),
												parent.end.y
														+ new Random()
																.nextInt(RANGE))
												: new Point(
														parent.end.x
																+ new Random()
																		.nextInt(RANGE),
														parent.end.y
																- new Random()
																		.nextInt(RANGE))));
				list.add(new Branch(
						level,
						parent.end,
						way == 0 ? new Point(parent.end.x
								+ new Random().nextInt(RANGE), parent.end.y
								- new Random().nextInt(RANGE))
								: way == 1 ? new Point(parent.end.x
										- new Random().nextInt(RANGE),
										parent.end.y
												+ new Random().nextInt(RANGE))
										: way == 2 ? new Point(parent.end.x
												- new Random().nextInt(RANGE),
												parent.end.y
														- new Random()
																.nextInt(RANGE))
												: new Point(
														parent.end.x
																+ new Random()
																		.nextInt(RANGE),
														parent.end.y
																+ new Random()
																		.nextInt(RANGE))));
			}
			RANGE *= 0.618;
			createTree(list, direction, level + 1, levelAll);
		}
	}
}

class Branch {
	public int level;// 序號
	public Point start;// 起始點
	public Point end;// 重點

	public Branch(int level, Point start, Point end) {
		super();
		this.level = level;
		this.start = start;
		this.end = end;
	}
}