I would like to plot lines on a simple x,y graph to display in a JApplet using JGraphT. The examples I found were not very helpful. Could someone please point me to a few simple JGraphT examples?
            Asked
            
        
        
            Active
            
        
            Viewed 2.3k times
        
    8
            
            
        
        Freelancer
        
- 836
 - 1
 - 14
 - 47
 
        Paul Lombardi
        
- 131
 - 2
 - 2
 - 9
 
2 Answers
6
            here an example I hope will help jgrapht
        josemm1790
        
- 831
 - 2
 - 11
 - 19
 
- 
                    that link is out of date..! – moosehead42 Jun 28 '21 at 15:03
 
4
            
            
        package simple;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import org.jgraph.JGraph;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import org.jgrapht.*;
import org.jgrapht.ext.*;
import org.jgrapht.graph.*;
// resolve ambiguity
import org.jgrapht.graph.DefaultEdge;
import com.compilervision.common.CFNode;
import com.compilervision.common.llvmir.instructions.CFG;
import com.compilervision.common.Edge;
/**
 * A demo applet that shows how to use JGraph to visualize JGraphT graphs.
 *
 * @author Barak Naveh
 * @since Aug 3, 2003
 */
public class Test
    extends JApplet
{
    //~ Static fields/initializers ---------------------------------------------
    private static final long serialVersionUID = 3256444702936019250L;
    private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF");
    private static final Dimension DEFAULT_SIZE = new Dimension(530, 320);
    //~ Instance fields --------------------------------------------------------
    //
    private JGraphModelAdapter<CFNode, Edge> jgAdapter;
    //~ Methods ----------------------------------------------------------------
    /**
     * An alternative starting point for this demo, to also allow running this
     * applet as an application.
     *
     * @param args ignored.
     */
    public static void main(String [] args)
    {
        Test applet = new Test();
        applet.init();
        JFrame frame = new JFrame();
        frame.getContentPane().add(applet);
        frame.setTitle("JGraphT Adapter to JGraph Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    /**
     * {@inheritDoc}
     */
    public void init()
    { 
        // create a JGraphT graphq
        ListenableGraph<CFNode, Edge> g =
            new ListenableDirectedMultigraph<CFNode, Edge>(
                    Edge.class);
        // create a visualization using JGraph, via an adapter
        jgAdapter = new JGraphModelAdapter<CFNode,Edge>(g);
        JGraph jgraph = new JGraph(jgAdapter);
        adjustDisplaySettings(jgraph);
        getContentPane().add(jgraph);
        resize(DEFAULT_SIZE);
        String v1 = "v1";
        String v2 = "v2";
       String v3 = "v3";
        String v4 = "v4";
        // add some sample data (graph manipulated via JGraphT)
        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);
        // position vertices nicely within JGraph component
        //GraphSimpleLayout()
        positionVertexAt(v1, 130, 140);
        positionVertexAt(v2, 60, 200);
        positionVertexAt(v3, 310, 230);
        positionVertexAt(v4, 380, 70);
        // that's all there is to it!...
    }
    private void adjustDisplaySettings(JGraph jg)
    {
        jg.setPreferredSize(DEFAULT_SIZE);
        Color c = DEFAULT_BG_COLOR;
        String colorStr = null;
        try {
            colorStr = getParameter("bgcolor");
        } catch (Exception e) {
        }
        if (colorStr != null) {
            c = Color.decode(colorStr);
        }
        jg.setBackground(c);
    }
    @SuppressWarnings("unchecked") // FIXME hb 28-nov-05: See FIXME below
    private void positionVertexAt(Object vertex, int x, int y)
    {
        DefaultGraphCell cell = jgAdapter.getVertexCell(vertex);
        AttributeMap attr = cell.getAttributes();
        Rectangle2D bounds = GraphConstants.getBounds(attr);
        Rectangle2D newBounds = new Rectangle2D.Double(x,y,bounds.getWidth(),bounds.getHeight());
        GraphConstants.setBounds(attr, newBounds);
        // TODO: Clean up generics once JGraph goes generic
        org.jgraph.graph.AttributeMap cellAttr = new AttributeMap();
        cellAttr.put(cell, attr);
        jgAdapter.edit(cellAttr, null, null, null);
    }
    //~ Inner Classes ----------------------------------------------------------
    /**
     * a listenable directed multigraph that allows loops and parallel edges.
     */
    private static class ListenableDirectedMultigraph<V, E>
        extends DefaultListenableGraph<V, E>
        implements DirectedGraph<V, E>
    {
        private static final long serialVersionUID = 1L;
        ListenableDirectedMultigraph(Class<E> edgeClass)
        {
            super(new DirectedMultigraph<V, E>(edgeClass));
        }
    }
}
// End JGraphAdapterDemo.java
        Yogesh Gadakh
        
- 59
 - 8