2016-07-04 5 views
0

私はツリーを描きたいが、再描画関数はpaintComponent関数を呼び出さない。 すべてのノードを配列に保存します。| JAVA |ツリーを描画すると、JPanelのrepaint()は機能しません

私のメインクラス:

public class Main { 

public static void main(String[] args) { 
    GUI g = new GUI(); 
    g.setVisible(true); 
} 
} 

私のGUIクラス:このような

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class GUI extends JFrame { 

private JPanel cp; 
private Node[] tree; 
private displayTree dtree = new displayTree(); 

public GUI() { 
    this.setTitle("Tree"); 
    this.setSize(900, 700); 
    this.setLocation(300, 100); 
    this.setResizable(false); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    cp = (JPanel) this.getContentPane(); 
    cp.setLayout(null); 
    cp.setBackground(Color.WHITE); 

    Node a = new Node("+"); 
    Node b = new Node("3"); 
    Node c = new Node("x"); 

    a.left = b; 
    a.right = c; 

    tree = new Node[3]; 
    tree[0] = a; 
    tree[1] = b; 
    tree[2] = c; 

    System.out.print(tree[0].data); 
    System.out.print(tree[0].left.data); 
    System.out.print(tree[0].right.data); 

    // Nothing like that works 

    dtree.revalidate(); 
    dtree.repaint(); 
    this.repaint(); 
    System.out.print("a"); 
} 

class displayTree extends JPanel { 

    private int radius = 25; 
    private int padding = 70; 

    @Override 
    public void paintComponent(Graphics g) { 
     // Should be displayed but doesn't 
     System.out.print("It works"); 
     super.paintComponent(g); 
     displayTree(g, tree[0], getWidth()/2, 30, getWidth()/4); 
    } 

    private void displayTree(Graphics g, Knoten k, int x, int y, int tempPadding) { 
     g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius); 
     g.drawString(k.data, x - 6, y + 4); 

     if (k.left != null) { 
      displayNode(g, x - tempPadding, y + padding, x, y); 
      displayTree(g, k.left, x - tempPadding, y + padding, tempPadding/2); 
     } 

     if (k.right != null) { 
      displayNode(g, x + tempPadding, y + padding, x, y); 
      displayTree(g, k.right, x + tempPadding, y + padding, tempPadding/2); 
     } 
    } 

    private void displayNode(Graphics g, int x1, int y1, int x2, int y2) { 
     double d = Math.sqrt(padding * padding + (x2 - x1) * (x2 - x1)); 
     int x11 = (int) (x1 - radius * (x1 - x2)/d); 
     int y11 = (int) (y1 - radius * (y1 - y2)/d); 
     int x21 = (int) (x2 + radius * (x1 - x2)/d); 
     int y21 = (int) (y2 + radius * (y1 - y2)/d); 
     g.drawLine(x11, y11, x21, y21); 
    } 
} 

} 

Nodeクラスのlokes:

import javax.swing.JPanel; 

public class Node extends JPanel { 

String data; 
Knoten left; 
Knoten right; 

public Node(String data) { 
    this.data = data; 
} 
} 

あなたは私を助けることができます。 どのようにノードを描くことができますか?

答えて

0

2つのこと:

あなたは、コンテンツ・ペインにdtreeを追加していない、とあなたはあなたのケースでは役に立たないnullにコンテンツペインのレイアウトを、設定されています。

public GUI() { 
    this.setTitle("Tree"); 
    this.setSize(900, 700); 
    this.setLocation(300, 100); 
    this.setResizable(false); 
    setBackground(Color.red); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    cp = (JPanel) this.getContentPane(); 
    //REMOVE null layout. 
    //cp.setLayout(null); 
    cp.setBackground(Color.WHITE); 

    Node a = new Node("+"); 
    Node b = new Node("3"); 
    Node c = new Node("x"); 

    a.left = b; 
    a.right = c; 

    tree = new Node[3]; 
    tree[0] = a; 
    tree[1] = b; 
    tree[2] = c; 

    System.out.print(tree[0].data); 
    System.out.print(tree[0].left.data); 
    System.out.print(tree[0].right.data); 

    // No useless repaints/revalidates 
    //Add the tree to the content pane 
    cp.add(dtree); 


    System.out.print("a"); 
} 

あなたが本当にサイズのツリーパネルなければならないコンテンツペインにはnullレイアウト持つように(いや、あなたはいけない)必要がある場合:

[...] 
// No useless repaints/revalidates 

//Size the content pane, otherwise with null layout its size will be 0x0 
dtree.setSize(900,700); 
//Add the tree to the content pane 
cp.add(dtree); 
+0

を感謝あなたは岩がたくさん!しかし、私はそれを調整する方法白い背景が欲しいですか? –

+0

@Leoうれしかったよ!これが正しい答えだと思うなら、[それを受け入れる]ことを検討すべきです(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。良い一日を! – BackSlash

関連する問題