📄 drawpanel.java
字号:
package treeandbtreedemo;
/**
* <p>Title: 画板,节点不可移动</p>
* <p>Description: 节点不可移动的画板。即直接在画板上画图</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 2.0(2005.7.5)加入图形界面
* @version 2.1(2005.7.5)初步加入动画和线程,产生节点的遍历动画效果
* @version 2.2(2005.7.6)在基础架构中添加了二叉树类,把树和二叉树两种结构分开来做了
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class DrawPanel
extends JPanel {
Table t; //表格数据
int current = -1; //控制遍历时所要画的节点
//int r; //圆圈半径为10
int index = -1;
public DrawPanel() {
t = new Table();
current = -1;
index = -1;
//t.radius= 10;
}
public DrawPanel(Table t) {
this.t = t;
current = -1;
index = -1;
//r = 10;
}
/**重载paintComponent(Graphics g)方法画图*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (t.isEmpty() == true) {
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
else {
paintTree(g); //画树
if (index != -1 && index < t.number) { //当遍历时画点
paintTree(g);
paintNode(g, t.tables[current], Color.yellow);
paintBottomNode(g, index, Color.yellow);
}
}
// g.dispose();
}
public void paintTree(Graphics g) {
//画线
for (int i = 1; t.tables[i] != null; i++) {
if (i > 0) {
paintLine(g, t.tables[t.tables[i].parents], t.tables[i]);
}
}
//画点
for (int i = 0; t.tables[i] != null; i++) {
paintNode(g, t.tables[i], Color.white);
}
//repaint();
}
/**画点*/
public void paintNode(Graphics g, TableNode nodes, Color color) {
//locateName(name)
g.setColor(Color.RED);
g.drawOval(nodes.getX() + this.getWidth() / 2 - t.r,
nodes.getY() + this.getHeight() / 8 - t.r,
t.r * 2, t.r * 2); //圆圈直径为20
g.setColor(color);
g.fillOval(nodes.getX() + this.getWidth() / 2 - t.r + 1,
nodes.getY() + this.getHeight() / 8 - t.r + 1,
t.r * 2 - 1, t.r * 2 - 1);
g.setColor(Color.BLACK);
g.drawString(nodes.name.toString(),
nodes.getX() + this.getWidth() / 2 - t.r + 7,
nodes.getY() + this.getHeight() / 8 - t.r + 14);
}
/**画线*/
public void paintLine(Graphics g, TableNode nodes1, TableNode nodes2) {
g.setColor(Color.BLACK);
g.drawLine(nodes1.getX() + this.getWidth() / 2,
nodes1.getY() + this.getHeight() / 8,
nodes2.getX() + this.getWidth() / 2,
nodes2.getY() + this.getHeight() / 8);
}
/**绘制画板底部的所遍历过的节点*/
public void paintBottomNode(Graphics g, int index, Color color) {
int w = 40;
for (int i = 0; i <= index; i++, w += 40) {
g.setColor(Color.RED);
g.drawOval(w - t.r,
this.getHeight() - 20 - t.r,
t.r * 2, t.r * 2); //圆圈直径为20
g.setColor(color);
g.fillOval(w - t.r + 1,
this.getHeight() - 20 - t.r + 1,
t.r * 2 - 1, t.r * 2 - 1);
g.setColor(Color.BLACK);
g.drawString(t.tables[t.path[i]].name.toString(),
w - t.r + 7,
this.getHeight() - 20 - t.r + 14);
}
}
/**设置表格*/
public void setTable(Table ta) {
t = ta;
}
/**设置当前节点*/
public void setCurrent(int c) {
current = t.path[c];
index = c;
}
/**初始化画板*/
public void InitDrawPanel() {
t = new Table();
current = -1;
index = -1;
t.r = 10;
//this.repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -