如何在MVC-gui中使用JUNG2?

我正在玩JUNG2并希望实现一个允许我显示和更改图表的小GUI。 遵循JUNG库中的示例工作正常,但他们没有单独的模型,视图和控制器。 所以我开始用干净的分离来构建GUI。

假设我的第一个版本的GUI,只是显示一个初始图形。 视图是模型的观察者,只要图形发生变化(在图形的初始化步骤中发生一次),就会收到通知。 但是,图形不会显示在屏幕的中央(就像在非MVC示例中那样),但我可以在左上角看到它的一小部分。

现在,这引出了一个普遍的问题:如何告诉jung-visualization组件,模型发生了变化? 以后:我如何使用即用型组件,如MVC架构中的Jung-Mouse? JUNG似乎混合了模型,视图和控制器,我不确定,如何以及在何处正确使用它们。

编辑:荣格教程显示如何使用鼠标管理更改,但它没有显示,如何根据模型中的更改更改视图(通过其他选项,例如按钮“添加节点”或其他)

这是我到目前为止的第一次尝试:

风景

public class MOCView implements GraphChangeObserver { private final ControllerInterface controller; private final MOCModelInterface model; private Layout layout; private BasicVisualizationServer visualization; private JFrame frame; public MOCView(final ControllerInterface controller, final MOCModelInterface model) { this.controller = controller; this.model = model; model.registerObserver(this); } public void createView() { this.layout = new CircleLayout(this.model.getGraph()); this.layout.setSize(new Dimension(300, 300)); this.visualization = new BasicVisualizationServer( this.layout); this.visualization.setPreferredSize(new Dimension(350, 350)); this.frame = new JFrame("MOC View"); this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.frame.getContentPane().add(this.visualization); this.frame.pack(); this.frame.setVisible(true); } @Override public void updateGraph() { this.visualization.repaint(); } } 

该模型

 public class MOCModel implements MOCModelInterface { private final Graph graph = new DirectedSparseGraph(); private final ArrayList graphChangeObservers = new ArrayList(); @Override public void initialize() { this.generateInitialGraph(); } @Override public Graph getGraph() { return this.graph; } @Override public void registerObserver(final GraphChangeObserver o) { this.graphChangeObservers.add(o); } @Override public void removeObserver(final GraphChangeObserver o) { this.graphChangeObservers.remove(o); } private void generateInitialGraph() { final Node nodeA = new Node("Node A"); this.graph.addVertex(nodeA); final Node nodeB = new Node("Node B"); this.graph.addVertex(nodeB); final Node nodeC = new Node("Node C"); this.graph.addVertex(nodeC); final Node nodeD = new Node("Node D"); this.graph.addVertex(nodeD); final Node nodeE = new Node("Node E"); this.graph.addVertex(nodeE); this.graph.addEdge(new Edge("Edge 1"), nodeA, nodeB); this.graph.addEdge(new Edge("Edge 2"), nodeA, nodeC); this.graph.addEdge(new Edge("Edge 3"), nodeB, nodeC); this.graph.addEdge(new Edge("Edge 4"), nodeC, nodeD); this.graph.addEdge(new Edge("Edge 5"), nodeD, nodeE); this.graph.addEdge(new Edge("Edge 6"), nodeA, nodeE); this.graph.addEdge(new Edge("Edge 7"), nodeE, nodeA); this.graph.addEdge(new Edge("Edge 8"), nodeD, nodeB); notifyGraphChangeObservers(); } private void notifyGraphChangeObservers() { for (final GraphChangeObserver gco : this.graphChangeObservers) { gco.updateGraph(); } } } 

控制器

 public class MOCController implements ControllerInterface { private final MOCModelInterface model; private final MOCView view; public MOCController(final MOCModelInterface model) { this.model = model; this.view = new MOCView(this, model); this.view.createView(); this.model.initialize(); } } 

主类

 public class MOCStart { /** * @param args */ public static void main(final String[] args) { final MOCModelInterface model = new MOCModel(); new MOCController(model); } } 

了解JUNG2如何工作的最好方法是使用Maven svn checkout http://jung.googlecode.com/svn/trunk/ jung-read-only通过m2e Maven Eclipse插件从SVN存储库查看其样本

从这里,检查包edu.uci.ics.jung.samples以获取涉及图形鼠标的各种JUNG2代码示例:例如GraphEditorDemo.java

注意 :应用下面的SVN补丁来纠正一些错误

 ### Eclipse Workspace Patch 1.0 #P jung-io Index: src/test/java/edu/uci/ics/jung/io/TestGraphMLReader.java =================================================================== --- src/test/java/edu/uci/ics/jung/io/TestGraphMLReader.java (revision 31) +++ src/test/java/edu/uci/ics/jung/io/TestGraphMLReader.java (working copy) @@ -125,7 +125,7 @@ public void testAttributes() throws IOException { Graph graph = new UndirectedSparseGraph(); - gmlreader.load("src/test/resources/attributes.graphml", graph); + gmlreader.load("src/test/resources/edu/uci/ics/jung/io/graphml/attributes.graphml", graph); Assert.assertEquals(graph.getVertexCount(), 6); Assert.assertEquals(graph.getEdgeCount(), 7); @@ -193,7 +193,7 @@ GraphMLReader, Number, Number> hyperreader = new GraphMLReader, Number, Number>( vertexFactory, edgeFactory); - hyperreader.load("src/test/resources/hyper.graphml", graph); + hyperreader.load("src/test/resources/edu/uci/ics/jung/io/graphml/hyper.graphml", graph); Assert.assertEquals(graph.getVertexCount(), 7); Assert.assertEquals(graph.getEdgeCount(), 4); ### Eclipse Workspace Patch 1.0 #P jung2 Index: jung-visualization/src/main/java/edu/uci/ics/jung/visualization/control/EditingPopupGraphMousePlugin.java =================================================================== --- jung-visualization/src/main/java/edu/uci/ics/jung/visualization/control/EditingPopupGraphMousePlugin.java (revision 31) +++ jung-visualization/src/main/java/edu/uci/ics/jung/visualization/control/EditingPopupGraphMousePlugin.java (working copy) @@ -54,6 +54,8 @@ final PickedState pickedVertexState = vv.getPickedVertexState(); final PickedState pickedEdgeState = vv.getPickedEdgeState(); + popup.removeAll(); + if(vertex != null) { Set picked = pickedVertexState.getPicked(); if(picked.size() > 0) {