[英]|JAVA| Draw a Tree, JPanel repaint() does not work
I want to draw a tree but the repaint function does not call the paintComponent function. 我想绘制一棵树,但repaint函数不调用paintComponent函数。 I save every node in an array.
我将每个节点保存在一个数组中。
My main class: 我的主班:
public class Main {
public static void main(String[] args) {
GUI g = new GUI();
g.setVisible(true);
}
}
My GUI class: 我的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);
}
}
}
The Node class lokes like this: Node类如下所示:
import javax.swing.JPanel;
public class Node extends JPanel {
String data;
Knoten left;
Knoten right;
public Node(String data) {
this.data = data;
}
}
Can you help me. 你能帮助我吗。 How can i draw the Nodes ?
如何绘制节点?
Two things: 两件事情:
You aren't adding dtree
to the content pane, and you are setting the content pane's layout to null, which is useless in your case. 您没有将
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");
}
If you really need (no, you shouldn't) to have a null layout on the content pane you must size the tree panel: 如果您确实需要(否,您不应该)在内容窗格上具有空布局,则必须调整树面板的大小:
[...]
// 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.