在JPanel上绘制SimpleWeightedGraph [英] Drawing a SimpleWeightedGraph on a JPanel

查看:199
本文介绍了在JPanel上绘制SimpleWeightedGraph的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SimpleWeightedGraph,我想在JFrame中的JPanel上绘制它。
不幸的是没有画出来。

I have a SimpleWeightedGraph and I want to draw it on a JPanel in a JFrame. Unfortunately nothing is drawn.

我读过这篇文章。他们使用 ListenableDirectedGraph 所以我尝试了 ListenableUndirectedGraph 但没有成功。

I read this article. They are using a ListenableDirectedGraph so I tried a ListenableUndirectedGraph with no success.

public class DisplayGraphForm extends javax.swing.JFrame {
    public DisplayGraphForm(SimpleWeightedGraph g) {
       initComponents(); // graphPanel added to JFrame with BorderLayout (Center)

       JGraphModelAdapter adapter = new JGraphModelAdapter(g);

       JGraph jgraph = new JGraph(adapter);
       graphPanel.add(jgraph);
     }
}


推荐答案

它看起来你要从你的问题中留下一些重要的细节,如果没有最小,完整和可验证的例子,很难说问题出在哪里。

It looks that you're leaving some important details out of your question, and without a Minimal, Complete, and Verifiable example it is hard to say where is the problem.

但请注意,您尝试采用的样本非常陈旧。 JGraph 已转移到 JGraphX 。请考虑以下示例,该示例使用 JGraphXAdapter JGraphT JGraphX 的链接$ C>。

However, note that the sample you're trying to adopt is very old. JGraph has moved on to JGraphX. Consider the following sample that demonstrates the link of JGraphT and JGraphX using JGraphXAdapter.

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jgrapht.ListenableGraph;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.ListenableDirectedWeightedGraph;
import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.swing.mxGraphComponent;

public class DemoWeightedGraph {

    private static void createAndShowGui() {
        JFrame frame = new JFrame("DemoGraph");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ListenableGraph<String, MyEdge> g = buildGraph();
        JGraphXAdapter<String, MyEdge> graphAdapter = 
                new JGraphXAdapter<String, MyEdge>(g);

        mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
        layout.execute(graphAdapter.getDefaultParent());

        frame.add(new mxGraphComponent(graphAdapter));

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }

    public static class MyEdge extends DefaultWeightedEdge {
        @Override
        public String toString() {
            return String.valueOf(getWeight());
        }
    }

    public static ListenableGraph<String, MyEdge> buildGraph() {
        ListenableDirectedWeightedGraph<String, MyEdge> g = 
            new ListenableDirectedWeightedGraph<String, MyEdge>(MyEdge.class);

        String x1 = "x1";
        String x2 = "x2";
        String x3 = "x3";

        g.addVertex(x1);
        g.addVertex(x2);
        g.addVertex(x3);

        MyEdge e = g.addEdge(x1, x2);
        g.setEdgeWeight(e, 1);
        e = g.addEdge(x2, x3);
        g.setEdgeWeight(e, 2);

        e = g.addEdge(x3, x1);
        g.setEdgeWeight(e, 3);

        return g;
    }
}

请注意 MyEdge 扩展 DefaultWeightedEdge 以提供显示边缘权重的自定义 toString()。一个更干净的解决方案可能是覆盖 mxGraph.convertValueToString ,检查单元格的内容并根据需要提供自定义标签。 toString 是演示的快捷方式,我注意到 DefaultWeightedEdge.getWeight()受到保护,因此扩展名为无论如何需要:)

Note that MyEdge extends DefaultWeightedEdge to provide custom toString() that displays edge weight. A cleaner solution would be probably to override mxGraph.convertValueToString, examine content of cells and provide custom labels as needed. toString is a shortcut for the demo and also I noticed that DefaultWeightedEdge.getWeight() is protected, so the extension is needed anyway :)

这篇关于在JPanel上绘制SimpleWeightedGraph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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