2016-05-27 3 views
0

Im пытается рисовать двоичное дерево с использованием awt и swing. Я могу показать узлы дерева, но я не могу нарисовать линии от родительского узла до ребенка. Im, используя следующие классы: Node (представляет узел), treeGUI, DrawTree и Main. Это вывод без строк.Рисование линий в графическом представлении дерева

Árvore binária

Мое намерение состоит, чтобы показать строки из родительского узла к Чайлдс. Я судимый использовать drawLine metohd из класса Graphics и это выход:

Arvore

Метод drawTree определяет положение значений узла на экране и сохраняет в ArrayLists позиции узла. Метод drawLine рисует линии. Я думаю, что строки такие, потому что значения хранятся в ArrayList в этом конкретном порядке. Я пытался различными способами правильно рисовать линии, но все неудачно. Как я могу нарисовать линии от родителя до ребенка?

public class TreeGUI extends JFrame { 

    private JPanel contentPane; 
    public Node node; 
    public DrawTree drawer; 

    /** 
    * Create the frame. 
    */ 
    public TreeGUI(Node node) { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 500, 500); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     drawer = new DrawTree(node); 
     contentPane.add(drawer); 
     setContentPane(contentPane); 
     this.node = node; 
     setVisible(true); 
    } 

} 

class DrawTree extends JPanel{ 

    public Node node; 
    public static ArrayList listX = new ArrayList(); 
    public static ArrayList listY = new ArrayList(); 


    public DrawTree(Node node){ 
     this.node = node; 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     // TODO Auto-generated method stub 
     g.setFont(new Font("Tahoma", Font.BOLD, 20)); 
     DrawTree(g, 0, getWidth(), 0, getHeight()/node.getheight(node), node); 
     listX.clear(); 
     listY.clear(); 
    } 

    public void DrawTree(Graphics g, int StartWidth, int EndWidth, int StartHeight, int Level, Node node) { 
     String data = String.valueOf(node.getValue()); 
     g.setFont(new Font("Tahoma", Font.BOLD, 20)); 
     FontMetrics fm = g.getFontMetrics(); 
     int dataWidth = fm.stringWidth(data); 

     g.drawString(data, (StartWidth + EndWidth)/2 - dataWidth/2, StartHeight + Level/2); 
     listX.add((StartWidth + EndWidth)/2 - dataWidth/2); 
     listY.add(StartHeight + Level/2); 
     drawLine(g, node); 

     if (node.getLeft() != null) { 
      DrawTree(g, StartWidth, (StartWidth + EndWidth)/2, StartHeight + Level, Level, node.getLeft()); 
     } 
     if (node.getRight() != null) 
      DrawTree(g, (StartWidth + EndWidth)/2, EndWidth, StartHeight + Level, Level, node.getRight()); 
    } 

public void drawLine(Graphics g, Node node){ 
     for (int i=1; i < listY.size(); i++) 
      g.drawLine((int)listX.get(i-1), (int)listY.get(i-1), (int)listX.get(i), (int)listY.get(i)); 
    } 

}

Metodo главный

public static void main(String[] args) { 
    Node raiz = null; 
    raiz = raiz.insert(raiz, 35); 
    raiz.insert(raiz, 25); 
    raiz.insert(raiz, 75); 
    raiz.insert(raiz, 30); 
    raiz.insert(raiz, 20); 
    raiz.insert(raiz, 12); 
    raiz.insert(raiz, 6); 
    raiz.insert(raiz, 23); 
    raiz.insert(raiz, 90); 
    TreeGUI gui = new TreeGUI(raiz); 
} 
+0

Это: 'Node raiz = null; raiz = raiz.insert (raiz, 35); 'должен вызывать исключение NullPointerException. Пожалуйста, опубликуйте реальный действующий код. –

+0

, если вставка не является статической, и это неправильно. –

+0

Почему статическая неправа? –

ответ

5

Вы могли бы иметь функцию DrawTree вернуть позицию, которую она напечатала свой текст. Затем родитель вычертите линию из текущей позиции в позицию, возвращаемую функцией DrawTree ребенка. Это позволит вам избавиться от списков.

public Point DrawTree(Graphics g, int StartWidth, int EndWidth, int StartHeight, int Level, Node node) 
{ 
    String data = String.valueOf(node.getValue()); 
    g.setFont(new Font("Tahoma", Font.BOLD, 20)); 
    FontMetrics fm = g.getFontMetrics(); 
    int dataWidth = fm.stringWidth(data); 

    // Calculate position to draw text string 
    Point textPos = new Point((StartWidth + EndWidth)/2 - dataWidth/2, StartHeight + Level/2); 
    g.drawString(data, textPos.x, textPos.y); 

    if (node.getLeft() != null) { 
     Point child1 = DrawTree(g, StartWidth, (StartWidth + EndWidth)/2, StartHeight + Level, Level, node.getLeft()); 
     // Draw line from this node to child node 
     drawLine(g, textPos, child1); 
    } 
    if (node.getRight() != null) { 
     Point child2 = DrawTree(g, (StartWidth + EndWidth)/2, EndWidth, StartHeight + Level, Level, node.getRight()); 
     // Draw line from this node to child node 
     drawLine(g, textPos, child2); 
    } 
    // Return position for parent to use 
    return textPos; 
} 

public void drawLine(Graphics g, Point p1, Point p2) 
{ 
    g.drawLine(p1.x, p1.y, p2.x, p2.y); 
} 
Смежные вопросы