JFlap_2.EditingGraphViewer1.java Source code

Java tutorial

Introduction

Here is the source code for JFlap_2.EditingGraphViewer1.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package JFlap_2;

import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import org.apache.commons.collections15.Factory;

public class EditingGraphViewer1 {
    Graph<Integer, String> g;
    int nodeCount, edgeCount;
    Factory<Integer> vertexFactory;
    Factory<String> edgeFactory;

    public EditingGraphViewer1() {
        g = new SparseMultigraph<Integer, String>();
        nodeCount = 0;
        edgeCount = 0;
        vertexFactory = new Factory<Integer>() {
            public Integer create() {
                return nodeCount++;
            }
        };
        edgeFactory = new Factory<String>() {
            public String create() {
                return "E" + edgeCount++;
            }
        };
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        EditingGraphViewer1 sgv = new EditingGraphViewer1();
        // Layout<V, E>, VisualizationViewer<V,E>
        Layout<Integer, String> layout = new StaticLayout(sgv.g);
        layout.setSize(new Dimension(300, 300));
        VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(layout);
        vv.setPreferredSize(new Dimension(350, 350));
        // Show vertex and edge labels
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
        EditingModalGraphMouse gm = new EditingModalGraphMouse(vv.getRenderContext(), sgv.vertexFactory,
                sgv.edgeFactory);
        vv.setGraphMouse(gm);

        JFrame frame = new JFrame("Editing Graph Viewer 1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv);
        JMenuBar menuBar = new JMenuBar();
        JMenu modeMenu = gm.getModeMenu();
        modeMenu.setText("Mouse Mode");
        modeMenu.setIcon(null);
        modeMenu.setPreferredSize(new Dimension(80, 20));

        menuBar.add(modeMenu);
        frame.setJMenuBar(menuBar);
        gm.setMode(ModalGraphMouse.Mode.EDITING);
        frame.pack();
        frame.setVisible(true);

    }

}