如何在 JPanel 上绘制 SimpleWeightedGraph? [英] How to draw a SimpleWeightedGraph on a JPanel?

查看:48
本文介绍了如何在 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 演示了 JGraphTJGraphX 的链接.

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天全站免登陆