⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawlabelpanel.java~51~

📁 源程序(包括最初的版本
💻 JAVA~51~
字号:
package treeandbtreedemo;

/**
 * <p>Title: 画板</p>
 * <p>Description: 通过对的自定义的一个圆形标签的定位,添加,删除等操作来画图</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 * @4.2(2005.7.11)另一个画板通过添加节点图标来画图
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class DrawLabelPanel
    extends JPanel {
  Table t; //表格数据
  int current = -1; //控制遍历时所要画的节点
  //int r; //圆圈半径为10
  int index = -1; //表格数组的序号。
  CircleNode circles[]; //树的圆形节点
  CircleNode bottomCircle[]; //遍历时底部的节点

  public DrawLabelPanel() {
    t = new Table();
    current = -1;
    index = -1;
    circles = new CircleNode[100];
    bottomCircle = new CircleNode[100];
    this.setLayout(null);
  }

  /**重载paintComponent(Graphics g)方法画图*/
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    //画出孩子节点与双亲之间的连线
    for (int i = 1; i < t.number; i++) {
      int pa = t.tables[i].parents;
      if (circles[i] != null && circles[pa] != null) {
        if (circles[i].getParent() == this && circles[pa].getParent() == this) {
          g.setColor(Color.BLUE);
          g.drawLine(circles[i].getX() + t.r, circles[i].getY() + t.r,
                     circles[pa].getX() + t.r, circles[pa].getY() + t.r);
        }
      }
    }
  }

  /**初始化一棵树,显示树的图形化式样*/
  public void initDrawLabelPanel() {
    for (int i = 0; i < t.number; i++) {
      circles[i] = new CircleNode(t.tables[i].name.toString(), t.r, false);
      this.add(circles[i]);
      circles[i].setBounds(t.tables[i].getX() + this.getWidth() / 2 - t.r,
                           t.tables[i].getY() + this.getHeight() / 8 - t.r,
                           t.r * 2, t.r * 2);
      //this.add(circles[i]);
      this.validate();

    }
    this.repaint();
    for (int j = 0; j < t.number; j++) {
      bottomCircle[j] = new CircleNode(circles[j].name,
                                       circles[j].radius,
                                       true);
    }
  }

  /**画遍历时底部的的节点*/
  public void paintBottomNode() {
    int w = 40;
    if (index == 0) {
      bottomCircle[current].setBounds(w - t.r,
                                      this.getHeight() - 20 - t.r,
                                      t.r * 2, t.r * 2);
      this.add(bottomCircle[current]);
      this.validate();
    }
    else if (current < t.number) {
      bottomCircle[current].setBounds(bottomCircle[t.path[index - 1]].getX() +
                                      w - t.r,
                                      this.getHeight() - 20 - t.r,
                                      t.r * 2, t.r * 2);
      this.add(bottomCircle[current]);
      this.validate();
    }

  }

  /**恢复遍历前的状态*/
  public void cleanBottonCircle() {
    for (int i = 0; i < t.number; i++) {
      if (bottomCircle[i].getParent() == this) {
        this.remove(bottomCircle[i]);
      }
      circles[i].setfill(false);
    }

    this.validate();
  }

  /**填充当前遍历的节点*/
  public void fillCurrent() {
    circles[current].setfill(true);
    this.validate();
  }

  /**取消当前遍历的节点的前一个节点的填充色*/
  public void unFillPreCurrent(int in) {
    if (in >= 0) {
      circles[t.path[in]].setfill(false);
    }
    this.validate();
  }

  /**设置表格*/
  public void setTable(Table ta) {
    t = ta;
  }

  /**设置当前节点*/
  public void setCurrent(int c) {
    current = t.path[c];
    index = c;
  }

  /**初始化画板*/
  public void InitDrawLablePanel() {
    t = new Table();
    current = -1;
    index = -1;
    circles = new CircleNode[100];
    bottomCircle = new CircleNode[100];
    this.setLayout(null);

  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -