展开全部
画什么都行,这62616964757a686964616fe78988e69d8331333262353463个你得自己实现。我可以给你写一个画树的小程序:
---------------------------------------------------------
//TreeNode.java
package util;
import java.util.Vector;
public class TreeNode {
private Vector children;
private TreeNode parent;
private TreeNode root;
public TreeNode (TreeNode parent) {
children = new Vector();
if (parent == null)
root = this;
else {
parent.addChild(this);
}
}
public void addChild (TreeNode n) {
addChild(children.size(), n);
}
public void addChild (int index, TreeNode n) {
children.add(index, n);
n.parent = this;
}
public void removeChild (TreeNode n) {
children.remove(n);
}
public void removeChild (int index) {
children.remove(index);
}
public void setChild (int index, TreeNode n) {
children.set(index, n);
n.parent = this;
}
public TreeNode getChild (int index) {
return children.get(index);
}
public void changePosition (int index1, int index2) {
if (0 <= index1 && index1 < children.size() &&
0 <= index2 && index2 <= children.size()) {
TreeNode tmp = children.get(index1);
children.set(index1, children.get(index2));
children.set(index2, tmp);
}
}
public void setParent (TreeNode n) {
parent = n;
}
public TreeNode getRoot () {
return root;
}
public TreeNode getParent () {
return parent;
}
public Vector getChildren () {
return children;
}
}
------------------------------------------------------------
//TreePanelNode.java
package ui;
import util.TreeNode;
import java.awt.*;
public class TreePanelNode extends TreeNode {
private int x, y, width, height;
private Object value;
private TreePanelNode parent;
public TreePanelNode (TreePanelNode parent, int x, int y, int width, int height, Object value) {
super(parent);
this.x = x;
this.y = y;
this.parent = parent;
this.width = width;
this.height = height;
this.value = value;
}
public void setLocation (int x, int y) {
this.x = x;
this.y = y;
}
public void setSize (int width, int height) {
this.width = width;
this.height = height;
}
public Rectangle getBounds () {
return new Rectangle(x, y, width, height);
}
public Object getValue () {
return value;
}
public Point getLocation () {
return new Point(x, y);
}
public int getWidth () {
return width;
}
public int getHeight () {
return height;
}
public void setValue (Object value) {
this.value = value;
}
public TreePanelNode getParent () {
return parent;
}
}
--------------------------------------------------------------
//TreePanel.java
package ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public class TreePanel extends JPanel {
private Vector nodes;
private Vector selectedNodes;
private int tmpX, tmpY;
private int editingNode = -1;
private int addingParent = -2;
private int mouseX, mouseY;
public TreePanel () {
nodes = new Vector();
selectedNodes = new Vector();
setBackground(Color.WHITE);
addMouseListener(new MouseAdapter() {
public void mousePressed (MouseEvent me) {
tmpX = me.getX();
tmpY = me.getY();
if (nodes.size() == 0) {
addingParent = -1;
}
int i = 0;
for (; i
if (nodes.get(i).getBounds().contains(mouseX, mouseY)) {
if (me.isControlDown()) {
if (!selectedNodes.contains(i))
selectedNodes.add(i);
else
selectedNodes.removeElement(i);
} else if (!selectedNodes.contains(i)) {
selectedNodes = new Vector();
selectedNodes.add(i);
}
if (me.getClickCount() == 2) {
nodes.get(i).setValue("");
editingNode = i;
}
break;
}
}
if (i == nodes.size())
if (!me.isControlDown())
selectedNodes = new Vector();
repaint();
}
public void mouseReleased (MouseEvent me) {
addingParent = -2;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged (MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
int i = 0;
for (; i
if (nodes.get(selectedNodes.get(i)).getBounds().contains(mouseX, mouseY))
break;
}
if (i != selectedNodes.size()) {
if (me.isAltDown() && addingParent != -3) {
addingParent = selectedNodes.get(i);
} else {
for (i=0; i
nodes.get(selectedNodes.get(i)).setLocation(nodes.get(selectedNodes.get(i)).getLocation().x + mouseX - tmpX,
nodes.get(selectedNodes.get(i)).getLocation().y + mouseY - tmpY);
}
tmpX = mouseX;
tmpY = mouseY;
}
}
repaint();
}
public void mouseMoved (MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
}
});
addKeyListener(new KeyAdapter() {
public void keyTyped (KeyEvent ke) {
if (editingNode != -1) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
editingNode = -1;
else
nodes.get(editingNode).setValue(nodes.get(editingNode).getValue().toString()+ke.getKeyChar());
repaint();
}
}
});
}
public void paint (Graphics g) {
super.paint(g);
FontMetrics fm = g.getFontMetrics();
if (addingParent > -2) {
String str = "node" + nodes.size();
int width = fm.stringWidth(str);
int height = fm.getHeight();
TreePanelNode pnt = null;
if (addingParent != -1)
pnt = nodes.get(addingParent);
nodes.add(new TreePanelNode(pnt, mouseX-width/2-10, mouseY-height/2-20, width+20, height+40, str));
addingParent = -3;
selectedNodes = new Vector();
selectedNodes.add(nodes.size()-1);
}
for (int i=0; i
String str = nodes.get(i).getValue().toString();
int width = fm.stringWidth(str);
int height = fm.getHeight();
nodes.get(i).setSize(width + 20, height + 40);
if (selectedNodes.contains(i))
g.setColor(Color.RED);
else
g.setColor(Color.BLACK);
if (nodes.get(i).getParent() != null)
g.drawLine(nodes.get(i).getLocation().x+nodes.get(i).getWidth()/2,
nodes.get(i).getLocation().y+nodes.get(i).getHeight()/2,
nodes.get(i).getParent().getLocation().x+nodes.get(i).getParent().getWidth()/2,
nodes.get(i).getParent().getLocation().y+nodes.get(i).getParent().getHeight()/2);
g.drawString(str, nodes.get(i).getLocation().x + 10, nodes.get(i).getLocation().y + 20);
g.drawRect(nodes.get(i).getLocation().x, nodes.get(i).getLocation().y, nodes.get(i).getWidth(), nodes.get(i).getHeight());
}
grabFocus();
}
}
----------------------------------------------------------
//MainView.java
package ui;
import javax.swing.*;
public class MainView extends JFrame {
private TreePanel tp;
public MainView () {
super("TreeDemo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
tp = new TreePanel();
add(tp);
setVisible(true);
}
public static void main (String args[]) {
new MainView();
}
}
------------------------------------------------------------
好了。然后你新建一个 util 文件夹 和 一个 ui 文件夹,把第一个文件放在 util 文件夹下,把其余文件放在 ui 文件夹下,文件名都根据我的注释就行了。
例如你这两个文件夹新建在 D 盘下,在命令行中编译运行:
----------------------------------------------------------
...>D:
D:\>javac util\TreeNode.java ui\TreePanelNode.java ui\TreePanel.java ui\MainView.java
D:\>java ui.MainView
----------------------------------------------------------
于是便能看到效果。祝您好运,顺便我写了这么多就给个最佳答案吧,哈哈。
忘了告诉你,按住 Alt 键拖动 Node 可以添加子节点。
按住 Ctrl 键单击 node 可以多选。
双击一个 node 然后可以从键盘输入 新的字符串,按回车结束。
----------------------------------------------------------
如果对以上的程序还有什么问题,留下邮箱,我直接发给你。
已赞过
已踩过<
你对这个回答的评价是?
评论
收起