使用 SWING + 时没有内容JUNG 刷新图形可视化

发布于 2024-12-28 04:35:11 字数 1166 浏览 1 评论 0原文

我的线程类中有以下 java 代码:

@Override    
public void run() {

    JFrame window = new JFrame("Visualization POC");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while (true) {
        window.setVisible(false);

        Layout<Node,Edge> layout = new CircleLayout<Node, Edge>(graph);
        layout.setSize(new Dimension(300, 300));

        BasicVisualizationServer<Node, Edge> vv = new BasicVisualizationServer<Node, Edge>(layout);
        vv.setPreferredSize(new Dimension(350, 350));
        vv.getRenderContext().setVertexFillPaintTransformer(new NodeColorTransformer());
        vv.getRenderContext().setEdgeDrawPaintTransformer(new EdgeColorTransformer());

        window.getContentPane().add(vv);
        window.pack();
        window.setVisible(true);

        try {
            Thread.sleep(ONE_SECOND);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

我想用它来刷新图形可视化的状态,但我陷入了巨大的问题。当创建 JFrame 布局和设置内容的代码块位于 while 循环内时,它不会显示在输出窗口中。当我之前将其放置时,它工作正常,但这不是我想要的。我通过主类中的 SpringUtilities.invokeLater 运行此线程。

我可以看到窗口正在刷新,因为它闪烁了一段时间。 我期待任何提示。

I have following java code in my thread class:

@Override    
public void run() {

    JFrame window = new JFrame("Visualization POC");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while (true) {
        window.setVisible(false);

        Layout<Node,Edge> layout = new CircleLayout<Node, Edge>(graph);
        layout.setSize(new Dimension(300, 300));

        BasicVisualizationServer<Node, Edge> vv = new BasicVisualizationServer<Node, Edge>(layout);
        vv.setPreferredSize(new Dimension(350, 350));
        vv.getRenderContext().setVertexFillPaintTransformer(new NodeColorTransformer());
        vv.getRenderContext().setEdgeDrawPaintTransformer(new EdgeColorTransformer());

        window.getContentPane().add(vv);
        window.pack();
        window.setVisible(true);

        try {
            Thread.sleep(ONE_SECOND);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

I want to use it to refresh state of graph visualization, but I got stuck on massive problem. When the block of code creating layout and setting content of JFrame is inside while loop it is not displayed in output window. When I place it before while, it works fine but it isn't that what I want. I run this thread via SpringUtilities.invokeLater in my main class.

I can see that the window is refresh, because it is blinking for a while.
I'm looking forward for any tips.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2025-01-04 04:35:11

您从 EDT 线程调用代码。对组件的任何更新都将在 EDT 线程的末尾绘制。你的线程不会退出

我对你的代码做了一点修改,它工作得很好。请将 JLabel 替换为您的 jung 。

final JFrame window = new JFrame("Visualization POC");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

最终随机运行=新随机();

while (true) {


  SwingUtilities.invokeLater(new Runnable(){
public void run() {
    window.setVisible(false);

     JPanel panel = new JPanel();
     JLabel label = new JLabel();
      int nextInt = ran.nextInt(100);
     label.setText("Test Label"+String.valueOf(nextInt));
    System.out.println(nextInt);
     panel.add(label);

    window.getContentPane().removeAll();
    window.getContentPane().add(panel);
    window.pack();
    window.setVisible(true);


}

});

尝试 {

     Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

}

You invoke your code from EDT thread. Any update to the component will be painted at the end of the EDT thread. Your thread will not exit

I have modified your code little, it works fine. please replace JLabel with your jung .

final JFrame window = new JFrame("Visualization POC");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final Random ran = new Random();

while (true) {


  SwingUtilities.invokeLater(new Runnable(){
public void run() {
    window.setVisible(false);

     JPanel panel = new JPanel();
     JLabel label = new JLabel();
      int nextInt = ran.nextInt(100);
     label.setText("Test Label"+String.valueOf(nextInt));
    System.out.println(nextInt);
     panel.add(label);

    window.getContentPane().removeAll();
    window.getContentPane().add(panel);
    window.pack();
    window.setVisible(true);


}

});

try {

     Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文