Graphstream视图未加载 [英] Graphstream view not loading

查看:94
本文介绍了Graphstream视图未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我有以下模式:

Well, i have the following schemas:

  • 扩展JFrame的Java类,实例化另一个 包含我的返回空白屏幕的图形.

  • A Java class that extends JFrame instantiating the other class that contains my graph which returns a blank screen.

第二个是普通类,里面有main方法,调用 包含我的图并返回正常图的同一个类.

The second a normal class with the method main inside of it, calling the same class that contains my graph which returns a normal graph.

现在,为什么作为JFrame的类返回一个空白图形?

Now, why the class that is a JFrame returns a blank graph?

我的图类代码.

public class grafoComparacao implements ViewerListener {
    private List<Comparados> integralizacoesComparadas;
    private Viewer viewer;
    private Graph graph;
    private View view;
    protected boolean loop = true;

    public grafoComparacao(List<Comparados> listaComparados) throws HeadlessException, InterruptedException {
        System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        this.integralizacoesComparadas = listaComparados;
        graph = new MultiGraph("clicks");
        graph.addAttribute("ui.stylesheet", GraphsStreamStylesheet.stylesheet);

        adicionaNodes(graph);

        viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        viewer.enableAutoLayout();
        view = viewer.addDefaultView(false);

        JFrame frame = new JFrame("Comparação de catálogos");
        frame.add((Component) view);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

        ViewerPipe fromViewer = viewer.newViewerPipe();
        fromViewer.addViewerListener(this);
        fromViewer.addSink(graph);

        frame.setVisible(true);

        while(loop) {
            fromViewer.pump();
        }        
    }

    //this actually works too.
//    public static void main(String[] args) throws HeadlessException, InterruptedException{
//        Control.Controller ctrl = new Controller();
//        JFileChooser escolherAlunos = new JFileChooser();
//        escolherAlunos.setMultiSelectionEnabled(true);
//        escolherAlunos.showOpenDialog(null);
//        File[] integralzacoes = escolherAlunos.getSelectedFiles();
//        List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
//        try {
//            grafoComparacao comparacao = new grafoComparacao(integralizacoesComparadas);
//        } catch (HeadlessException ex) {
//            Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (InterruptedException ex) {
//            Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        new grafoComparacao(integralizacoesComparadas);
//    }

    @Override
    public void viewClosed(String string) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void buttonPushed(String string) {

    }

    @Override
    public void buttonReleased(String string) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void adicionaNodes(Graph graph) {
       //function that populate my graph
    }

}

大多数人都会说,为什么在这里创建JPANEL?我没有找到如何设置其默认大小的方法,但是即使从内部删除jpanel,它也无法正常工作,在没有任何异常或其他情况的情况下,显示空白的查看器.

Most will say, why did you create a JPANEL here? I didnt find how to set the default size of it, but even removing the jpanel from inside, it didnt work, shows a blank viewer without any exception or other thing.

在这里我叫全班做我必须做的事情.

Here's where i call the class doing stuff i have to do.

  JFileChooser escolherAlunos = new JFileChooser();
    escolherAlunos.setMultiSelectionEnabled(true);
    escolherAlunos.showOpenDialog(null);
    File[] integralzacoes = escolherAlunos.getSelectedFiles();
    List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
    try {      
        grafoComparacao comparacao = new grafoComparacao(integralizacoesComparadas); // heres the graph call.
    } catch (HeadlessException ex) {
        Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
    }

在创建我的SwingWorker之后,图形被渲染,但是用户交互消失了.

After the creation of my SwingWorker, the graph was rendered, but the user interaction dissapeared.

摇摆工人,在这里我只是收集一些信息,以使图类有处理信息的可能性.

Swing worker, here i just gather some information to give the graph class the possibility to proccess information.

public class VisualizacaoWorker extends SwingWorker<Void, Void>{
    private List<Comparados> listao;
    public VisualizacaoWorker(List<Comparados> lista){
        this.listao = lista;
    }

    @Override
    protected Void doInBackground() throws Exception {
        GrafoComparacao gf = new GrafoComparacao(this.listao);
        return null;
    }

}

在这里,叫它.

JFileChooser escolherAlunos = new JFileChooser();
escolherAlunos.setMultiSelectionEnabled(true);
escolherAlunos.showOpenDialog(null);
File[] integralzacoes = escolherAlunos.getSelectedFiles();
List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
VisualizacaoWorker visualizacaoWorker = new VisualizacaoWorker(integralizacoesComparadas);
visualizacaoWorker.execute();

推荐答案

首先,类名称应以大写字母开头. "grafoComparacao"应为GrafoComparacao.

First of all class names should start with an upper case character. "grafoComparacao" should be GrafoComparacao.

现在,为什么作为JFrame的类返回一个空白图形?

Now, why the class that is a JFrame returns a blank graph?

我想您的GrafoComparacao类是在某些侦听器代码中创建的,这意味着该代码是在负责绘制GUI的事件分发线程上执行的.

I would guess that your GrafoComparacao class is created in some listener code which means the code is executing on the Event Dispatch Thread which is responsible for painting the GUI.

while(loop) 
{
    fromViewer.pump();
} 

这看起来像阻塞了在EDT上执行的代码,这意味着您正在使用无限循环,这意味着代码永远不会完成执行,因此GUI永远不会响应事件或重新绘制自身.

That looks like blocking code that is executing on the EDT, and means you are using an infinite loop which means the code never finishes executing so the GUI can never respond to events or repaint itself.

该代码应在单独的线程(可能是SwingWorker)上执行,因此EDT可以自由地响应事件并重新绘制自身.

That code should execute on a separate Thread (maybe a SwingWorker) so the EDT is free to respond to events and repaint itself.

Swing中的并发有关EDT和SwingWorker的更多信息.

Read the section from the Swing tutorial on Concurrency in Swing for more information on the EDT and on a SwingWorker.

这篇关于Graphstream视图未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆