I've been trying to get a n-ary tree structure graphed so when I enter some node it shows up but it doesn't.
It only draws the root then it "deletes" everything.
Here's my code:
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);
}
}
}
and I'm adding the nodes trought a button, here's the code:
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();
}
So my problem resides in whenever I call repaint() after the first time, all that was drawn (only the root) gets "erased" from the panel.