木を描きたいのですが、repaint 関数が paintComponent 関数を呼び出しません。すべてのノードを配列に保存します。
私の主なクラス:
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 クラスは次のようになります。
import javax.swing.JPanel;
public class Node extends JPanel {
String data;
Knoten left;
Knoten right;
public Node(String data) {
this.data = data;
}
}
手伝って頂けますか。ノードを描画するにはどうすればよいですか?