Java - repaint() 方法被调用一次,然后它不显示任何内容
Java - repaint() method is called once then it doesn't show anything
我一直在尝试绘制一个 n 元树结构图,所以当我输入某个节点时它出现了,但没有出现。
它只绘制根,然后它 "deletes" 一切。
这是我的代码:
public LinkedList<Node> nodes;
public Tree tree;
public TreeViewer() {
initComponents();
this.nodes = new LinkedList<>();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
if (this.tree != null) {
this.checkTree(this.tree.getRoot(), g, 200, 20, 20, 20);
}else{
System.out.println("empty");
}
}
public void checkTree(Node current, Graphics g, int x, int y, int height, int width) {
current.setX(x);
current.setY(y);
current.setHeight(height);
current.setWidth(width);
if (!this.nodes.contains(current)) {
g.drawString(current.name, x, y);
g.setColor(Color.black);
this.nodes.add(current);
if (current.getSon() != null && current.getBrother() == null) {
this.checkTree(current.getSon(), g, x, y + 40, height, width);
} else if (current.getBrother() != null && current.getSon() == null) {
this.checkTree(current.getBrother(), g, x - 30, y, height, width);
} else if (current.getBrother() != null && current.getSon() != null) {
this.checkTree(current.getSon(), g, x, y + 40, height, width);
this.checkTree(current.getBrother(), g, x - 30, y, height, width);
}
}
}
我通过按钮添加节点,代码如下:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(this.modificator.getTree() == null){
Node current = new Node(JOptionPane.showInputDialog("Type root ID:"));
this.modificator.setTree( new Tree(current));
}else{
Node current = new Node(JOptionPane.showInputDialog("Type ID:"));
this.modificator.getTree().addSon(this.modificator.getTree().getRoot(), this.modificator.getTree().getRoot(), current);
}
this.modificator.repaint();
}
所以我的问题在于,每当我第一次调用 repaint() 时,所有绘制的内容(仅根)都会从面板中获取 "erased"。
在 checkTree
里面你有这个:
if (!this.nodes.contains(current))
大概这是为了处理图表中的循环,但我没有看到任何清除 this.nodes
的地方。这意味着在第二次调用 paintComponent
时,您会立即退出 checkTree
,因为根目录已经添加到 this.nodes
.
如果您在 paintComponent
末尾清除 this.nodes
可能会成功。
我一直在尝试绘制一个 n 元树结构图,所以当我输入某个节点时它出现了,但没有出现。
它只绘制根,然后它 "deletes" 一切。
这是我的代码:
public LinkedList<Node> nodes;
public Tree tree;
public TreeViewer() {
initComponents();
this.nodes = new LinkedList<>();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
if (this.tree != null) {
this.checkTree(this.tree.getRoot(), g, 200, 20, 20, 20);
}else{
System.out.println("empty");
}
}
public void checkTree(Node current, Graphics g, int x, int y, int height, int width) {
current.setX(x);
current.setY(y);
current.setHeight(height);
current.setWidth(width);
if (!this.nodes.contains(current)) {
g.drawString(current.name, x, y);
g.setColor(Color.black);
this.nodes.add(current);
if (current.getSon() != null && current.getBrother() == null) {
this.checkTree(current.getSon(), g, x, y + 40, height, width);
} else if (current.getBrother() != null && current.getSon() == null) {
this.checkTree(current.getBrother(), g, x - 30, y, height, width);
} else if (current.getBrother() != null && current.getSon() != null) {
this.checkTree(current.getSon(), g, x, y + 40, height, width);
this.checkTree(current.getBrother(), g, x - 30, y, height, width);
}
}
}
我通过按钮添加节点,代码如下:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(this.modificator.getTree() == null){
Node current = new Node(JOptionPane.showInputDialog("Type root ID:"));
this.modificator.setTree( new Tree(current));
}else{
Node current = new Node(JOptionPane.showInputDialog("Type ID:"));
this.modificator.getTree().addSon(this.modificator.getTree().getRoot(), this.modificator.getTree().getRoot(), current);
}
this.modificator.repaint();
}
所以我的问题在于,每当我第一次调用 repaint() 时,所有绘制的内容(仅根)都会从面板中获取 "erased"。
在 checkTree
里面你有这个:
if (!this.nodes.contains(current))
大概这是为了处理图表中的循环,但我没有看到任何清除 this.nodes
的地方。这意味着在第二次调用 paintComponent
时,您会立即退出 checkTree
,因为根目录已经添加到 this.nodes
.
如果您在 paintComponent
末尾清除 this.nodes
可能会成功。