Jung Factory自定义对象

时间:2016-01-09 20:42:42

标签: java user-interface jung

我是jung的新手,我需要有关如何学习的帮助。我搜索了很多地方,但我还没有找到一步一步的指南。我试图创建一个gui,只有当他们选择了正确的菜单并输入了正确的名称时才会添加顶点。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JMenu;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

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 demo3 {
  Graph<State,Transition> g;
 Factory<State>stateList;
 Factory<Transition>linelist;
 ArrayList<State>states = new ArrayList<State>();
 ArrayList<Transition>lines = new ArrayList<Transition>();
 private static JFrame frame;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    initialize();
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                demo3 sgv = new demo3();
                Layout<State, Transition> layout = new StaticLayout(sgv.g);
                layout.setSize(new Dimension(300,300));
                VisualizationViewer<State,Transition> vv = new VisualizationViewer<State,Transition>(layout);
                vv.setPreferredSize(new Dimension(350,350));
                demo3 window = new demo3();
                  EditingModalGraphMouse gm = new EditingModalGraphMouse(vv.getRenderContext(), 
                    sgv.stateList, sgv.linelist); 
                    vv.setGraphMouse(gm);
                    gm.setMode(ModalGraphMouse.Mode.EDITING);

                    frame.getContentPane().add(vv);
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public demo3() {
    g = new SparseMultigraph<State, Transition>();
    stateList = new Factory<State>() {
        public State create() {
            State newState = new State();
            states.add(newState);
            return newState;
        }
    };
    linelist = new Factory<Transition>(){
        public Transition create(){
            Transition line = new Transition();
            lines.add(line);
            return line;
        }
    };
}

/**
 * Initialize the contents of the frame.
 */
private static void initialize() {;
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu mnAddStateOr = new JMenu("Add State or Transition");
    menuBar.add(mnAddStateOr);

    JPanel panel = new JPanel();
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            String stateName = JOptionPane.showInputDialog("Enter the value of the state");
            State newState = new State(Integer.parseInt(stateName));


        }
    });
    frame.getContentPane().add(panel, BorderLayout.CENTER);
}

}

我需要知道如何根据输入的州名来标记每个圆圈。学习JUNG的任何提示都会有所帮助。

0 个答案:

没有答案