线程中的JPanel paintComponent

时间:2014-03-24 09:35:33

标签: java multithreading swing jpanel

我在网上找到了一个很好的代码来绘制二叉树。我为它创建了二进制搜索树算法,现在如果我通过该函数的根节点,它会抽出我的树。我想通过在线程中运行它,并在插入元素后使其休眠来逐步绘制它。现在的问题是,我猜,JPanel本身在绘图完成之前不会返回,所以即使它在不同的线程上运行,它也不会被添加到我的分割窗格中(我实现了很多排序算法,并且已经被绘制,并且可以从同一个选项卡式窗格访问所有选项卡。每个选项卡式窗格都包含一个拆分窗格,每个拆分窗格包含控制按钮和一个jpanel,我在其中绘制算法,但其他窗格不从JPanel扩展!)。你能帮我找一个方法在一个完全不同的主题程序中运行它,然后能看到它是如何绘制树的吗?

请不要在这段代码上评判我,我只是快速地把它放在一起,试图让它工作,这不是我编码的方式。

public class BinaryTree extends JPanel implements Runnable {

private int radius = 20;
private int vGap = 45, hGap = 150;
private int x = 350, y = 25;
private GraphNode root;
private BinarySearchTreeSort binaryTreeSort;
private int latency = 270;
private boolean isManual = false;

//Stores the sorting algorithm's instance
public BinaryTree(BinarySearchTreeSort binaryTreeSort) {
    this.binaryTreeSort = binaryTreeSort;
}

@Override
public void run() {
//Maybe i should put here the paintComponent somehow???
    root = binaryTreeSort.binaryTreeSort();
}

@Override
public void paintComponent(final Graphics g) {
    displayTree(g, root, x, y, hGap);
}

private void displayTree(Graphics g, GraphNode root, int x, int y, int hGap) {
    // Display the root  
    while (!isStarted) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(BinaryTreeFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);

    if (root.getValue() != 0) {
        g.drawString(root.getValue() + "", x - 13, y + 4);

    }

    sleep();

    if (root.getLeftChild() != null) {
        // Draw a line to the left node  
        g.setColor(Color.black);
        leftChild(g, x - hGap, y + vGap, x, y);
        //Draw the left subtree   
        displayTree(g, root.getLeftChild(), x - hGap, y + vGap, hGap / 2);
    }

    if (root.getRightChild() != null) {
        // Draw a line to the right node  
        g.setColor(Color.black);
        rightChild(g, x + hGap, y + vGap, x, y);
        // Draw the right subtree recursively  
        displayTree(g, root.getRightChild(), x + hGap, y + vGap, hGap / 2);
    }

}

/* Draw left child */
private void leftChild(Graphics g, int x1, int y1, int x2, int y2) {
    double d = Math.sqrt(vGap * vGap + (x2 - x1) * (x2 - x1));
    int x11 = (int) (x1 + radius * (x2 - x1) / d);
    int y11 = (int) (y1 - radius * vGap / d);
    int x21 = (int) (x2 - radius * (x2 - x1) / d);
    int y21 = (int) (y2 + radius * vGap / d);
    g.drawLine(x11, y11, x21, y21);

    //g.drawString("0", ((x21 + x11) - 20) / 2, (y21 + y11) / 2);
}

/*Draw right child */
private void rightChild(Graphics g, int x1, int y1, int x2, int y2) {
    double d = Math.sqrt(vGap * vGap + (x2 - x1) * (x2 - x1));
    int x11 = (int) (x1 - radius * (x1 - x2) / d);
    int y11 = (int) (y1 - radius * vGap / d);
    int x21 = (int) (x2 + radius * (x1 - x2) / d);
    int y21 = (int) (y2 + radius * vGap / d);
    g.drawLine(x11, y11, x21, y21);

    //g.drawString("1", (x21 + x11) / 2, (y21 + y11) / 2);
}

public void sleep() {
    if (!isManual) {
        try {
            Thread.sleep(latency);
        } catch (InterruptedException ex) {
            Logger.getLogger(BinaryTree.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

0 个答案:

没有答案