package test;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import javax.security.auth.callback.LanguageCallback;
import javax.swing.JButton;
import javax.swing.JFrame;
import Btree.Node.Node;
public class drawTest extends JFrame implements Runnable, MouseListener {
Container con;
JButton creatBT;
JButton goon;
JButton show;
JButton stop;
JButton next;
JButton last;
JButton out;
JButton restart;
boolean move = false;
Image offScreenImage = null;
private static int FRAME_WIDTH = 800;
private static int FRAME_HEIGHT = 500;
Node root = null;
Thread one ;
public void draw(){
Graphics g = getGraphics();
Node temp = root;
int i = 65;
Stack<Node> s = new Stack<Node>();
while (root != null || !s.empty()) {
while (root != null) {
root.setData(i);
root.draw(g);
Color c = g.getColor();
g.setColor(Color.RED);
g.drawOval(root.getX(), root.getY(), 50, 50);
g.drawString("" + (char) i, root.getX() + 20, root.getY() + 30);
g.setColor(c);
if (root.getLchild() != null) {
g.drawLine(root.getX() + 25, root.getY() + 25, root
.getLchild().getX() + 25,
root.getLchild().getY() + 25);
}
if (root.getRchild() != null) {
g.drawLine(root.getX() + 25, root.getY() + 25, root
.getRchild().getX() + 25,
root.getRchild().getY() + 25);
}
s.push(root);
root = root.getLchild();
i++;
}
if (!s.empty()) {
root = s.pop();
root = root.getRchild();
}
}
g.drawString("先序遍历序列:", 180, 450 );
root = temp;
}
public static Node createTree(int n, int x, int y) // n为树的结点数
{
Node root;
if (n == 0) {
root = null;
} else {
root = new Node(x, y);
int n1 = (int) (Math.random() * 100) % n;
root.setLchild(createTree(n1, x - 120 / (y / 50), y + 80));
root.setRchild(createTree(n - 1 - n1, x + 120 / (y / 50), y + 80));
}
return root;
}
public void lauchFrame() {
con = new Container();
this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
this.setLocation(250, 80);
this.setTitle("Btree");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
creatBT = new JButton("自动建树");
show = new JButton("自动演示");
stop = new JButton("停止");
goon = new JButton("继续");
restart = new JButton("重新开始");
out = new JButton("退出");
creatBT.setBounds(20, 50, 100, 50);
show.setBounds(20, 110, 100, 50);
stop.setBounds(20, 170, 100, 50);
goon.setBounds(20, 230, 100, 50);
//restart.setBounds(20, 290, 100, 50);
out.setBounds(20, 290, 100, 50);
con.add(creatBT);
con.add(show);
con.add(stop);
con.add(goon);
//con.add(restart);
//con.add(last);
con.add(out);
this.setResizable(false);
this.setBackground(Color.GREEN);
this.addKeyListener(new KeyMonitor());
creatBT.addMouseListener(this);
show.addMouseListener(this);
stop.addMouseListener(this);
goon.addMouseListener(this);
//restart.addMouseListener(this);
//last.addMouseListener(this);
out.addMouseListener(this);
this.add(con);
setVisible(true);
}
public static void main(String[] args) {
drawTest dt = new drawTest();
dt.lauchFrame();
// draw();
}
private class KeyMonitor extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:// colorStr = "Color.WHITE";break;
case KeyEvent.VK_RIGHT:
;
break;
}
}
}
public void run() {
try {
int i=0;
Graphics2D g;
Stack<Node> s = new Stack<Node>();
while (this.root != null || !s.empty()) {
while (root != null) {
for (int k = 0; k < 5; k++) {
while(!move){
hangThread();
}
g = (Graphics2D) getGraphics();
g.setStroke(new BasicStroke(2f));
Color c = g.getColor();
Font f = g.getFont();
g.setColor(Color.BLUE);
g.drawOval(root.getX(), root.getY(), 50, 50);
Thread.sleep(500);
g.setColor(Color.RED);
g.drawOval(root.getX(), root.getY(), 50, 50);
g.setColor(c);
g.setFont(new Font("宋体",Font.BOLD,30));
g.drawString(""+(char)root.getData(), 280+i*30, 450 );
g.setFont(f);
}
s.push(root);
root = root.getLchild();
i++;
}
if (!s.empty()) {
root = s.pop();
root = root.getRchild();
}
}
i=0;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void mouseClicked(MouseEvent a) {
if(a.getSource() == creatBT){
if(root == null){
root = createTree(6, 350, 50);
draw();
}else {
repaint();
root = null;
move = false;
}
}
if (a.getSource() == show) {
one = new Thread(this);
move = true;
one.start();
one = null;
} else if (a.getSource() == stop) {
move = false;
}else if(a.getSource() == goon){
move = true;
resumeThread();
}else if(a.getSource() == out){
System.exit(0);
}
}
public synchronized void hangThread() throws InterruptedException{
wait();
}
public synchronized void resumeThread(){
notifyAll();
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}