org.jgrapht.demo.JGraphXAdapterDemo Maven / Gradle / Ivy
/*
* (C) Copyright 2013-2021, by Barak Naveh and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* See the CONTRIBUTORS.md file distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the
* GNU Lesser General Public License v2.1 or later
* which is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
*
* SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
*/
package org.jgrapht.demo;
//@example:full:begin
import com.mxgraph.layout.*;
import com.mxgraph.swing.*;
import org.jgrapht.*;
import org.jgrapht.ext.*;
import org.jgrapht.graph.*;
import javax.swing.*;
import java.awt.*;
/**
* A demo applet that shows how to use JGraphX to visualize JGraphT graphs. Applet based on
* JGraphAdapterDemo.
*
*/
public class JGraphXAdapterDemo
extends
JApplet
{
private static final long serialVersionUID = 2202072534703043194L;
private static final Dimension DEFAULT_SIZE = new Dimension(530, 320);
private JGraphXAdapter jgxAdapter;
/**
* An alternative starting point for this demo, to also allow running this applet as an
* application.
*
* @param args command line arguments
*/
public static void main(String[] args)
{
JGraphXAdapterDemo applet = new JGraphXAdapterDemo();
applet.init();
JFrame frame = new JFrame();
frame.getContentPane().add(applet);
frame.setTitle("JGraphT Adapter to JGraphX Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
@Override
public void init()
{
// create a JGraphT graph
ListenableGraph g =
new DefaultListenableGraph<>(new DefaultDirectedGraph<>(DefaultEdge.class));
// create a visualization using JGraph, via an adapter
jgxAdapter = new JGraphXAdapter<>(g);
setPreferredSize(DEFAULT_SIZE);
mxGraphComponent component = new mxGraphComponent(jgxAdapter);
component.setConnectable(false);
component.getGraph().setAllowDanglingEdges(false);
getContentPane().add(component);
resize(DEFAULT_SIZE);
String v1 = "v1";
String v2 = "v2";
String v3 = "v3";
String v4 = "v4";
// add some sample data (graph manipulated via JGraphX)
g.addVertex(v1);
g.addVertex(v2);
g.addVertex(v3);
g.addVertex(v4);
g.addEdge(v1, v2);
g.addEdge(v2, v3);
g.addEdge(v3, v1);
g.addEdge(v4, v3);
// positioning via jgraphx layouts
mxCircleLayout layout = new mxCircleLayout(jgxAdapter);
// center the circle
int radius = 100;
layout.setX0((DEFAULT_SIZE.width / 2.0) - radius);
layout.setY0((DEFAULT_SIZE.height / 2.0) - radius);
layout.setRadius(radius);
layout.setMoveCircle(true);
layout.execute(jgxAdapter.getDefaultParent());
// that's all there is to it!...
}
}
// @example:full:end