📄 drawpanel.java~2~
字号:
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 ;//表格数据 String function;//进行某种操作的标志 int current;//控制动画时所要画的节点 public DrawPanel() { t=new Table(); function=new String(); } public DrawPanel(Table t){ this.t=t; } public DrawPanel(Table t,String function){ this.t=t; this.function=function; } /**画布边框(35,15,600,420)即wide=565 high=415 *画节点的区域为(35,15,600,400) *节点半径r=20 */ public void paintComponent(Graphics g) { super.paintComponent(g); if(t.tables[0]==null) paintInit(g); else{ if (function != null && function.equals("start") == true) { paintTree(g); //repaint(); } if (function != null && function.equals("Travel") == true) { paintTree(g); fillTravelNode(g, current); //g.setColor(Color.RED); //g.drawOval(t.tables[current].getX()-r, t.tables[current].getY()-r, r*2,r*2); //圆圈直径为20 //g.setColor(Color.BLUE); //g.fillOval(t.tables[current].getX()-r + 1, t.tables[current].getY()-r + 1, r*2-1, r*2-1); //nextNode(i){ repaint(); } /*if (function != null && function.equals("postTravel") == true) { paintTree(g); fillTravelNode(g, current); repaint(); }*/ } //画边框 //g.setColor(Color.blue); //t.setTable(); //Dimension d=new Dimension(); //d=this.getSize(); //g.drawString(d.getWidth()+" "+d.getHeight(),100,100); //this.setSize(new Dimension(630,500)); //g.drawRect(0+35,0+15,600,420); /**if(t.isEmpty()==true){ //画白框 g.setColor(Color.white); g.fillRect(0 + 35, 0 + 15, 600, 420); } else{ //画白框 g.setColor(Color.white); g.fillRect(0 + 35, 0 + 15, 600, 420); //画线 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]); } }*/ } public void paintInit(Graphics g){ //super.paintComponent(g); //画白框 g.setColor(Color.white); //g.fillRect(0 + 35, 0 + 15, 600, 420); } public void paintTree(Graphics g){ //画白框 //g.setColor(Color.white); //g.fillRect(0 + 35, 0 + 15, 600, 420); //画线 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]); } //repaint(); } /**画点*/ public void paintNode(Graphics g, TableNode nodes) { //locateName(name) int r=10;//圆圈半径为10 g.setColor(Color.RED); g.drawOval(nodes.getX()+this.getWidth()/2-r, nodes.getY()+this.getHeight()/8-r, r*2,r*2); //圆圈直径为20 g.setColor(Color.white); g.fillOval(nodes.getX()+this.getWidth()/2-r + 1, nodes.getY()+this.getHeight()/8-r + 1, r*2-1, r*2-1); g.setColor(Color.BLACK); g.drawString(nodes.name.toString(), nodes.getX()+this.getWidth()/2-r + 7, nodes.getY()+this.getHeight()/8-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.getWidth()/8 , nodes2.getX()+this.getWidth()/2, nodes2.getY()+this.getWidth()/8 ); } /**设置表格*/ public void setTable(Table table){ t=table; } /**设置所要进行的操作*/ public void setFunction(String f){ function=f; } public void setCurrent(int c){ current=c; } public void fillTravelNode(Graphics g,int c){ int r=10;//圆圈半径为10 current=c; g.setColor(Color.RED); g.drawOval(t.tables[current].getX()+this.getWidth()/2-r, t.tables[current].getY()+this.getWidth()/8-r, r*2,r*2); //圆圈直径为20 g.setColor(Color.BLUE); g.fillOval(t.tables[current].getX()+this.getWidth()/2-r + 1, t.tables[current].getY()+this.getWidth()/8-r + 1, r*2-1, r*2-1); g.setColor(Color.BLACK); g.drawString(t.tables[current].name.toString(), t.tables[current].getX()+this.getWidth()/2-r + 7, t.tables[current].getY()+this.getWidth()/8-r + 14); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -