📄 networkgraph.java
字号:
/*
* Created on 10-nov-2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package graph;
import org.jgraph.*;
import org.jgraph.graph.*;
import java.util.*;
import java.awt.*;
import javax.swing.BorderFactory;
import gui.MyVertexRenderer;
import gui.MyEdgeRenderer;
/**
* @author Andy Holvoet
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class NetworkGraph extends JGraph {
public NetworkGraph()
{
VertexView.renderer = new MyVertexRenderer();
EdgeView.renderer = new MyEdgeRenderer();
}
public NetworkGraph(GraphModel model)
{
super(model, (GraphLayoutCache) null, new BasicMarqueeHandler());
VertexView.renderer = new MyVertexRenderer();
EdgeView.renderer = new MyEdgeRenderer();
}
public static void addSampleData(GraphModel model) {
ConnectionSet cs = new ConnectionSet();
Map attributes = new Hashtable();
// Styles For Implement/Extend/Aggregation
Map implementStyle = GraphConstants.createMap();
GraphConstants.setLineBegin(
implementStyle,
GraphConstants.ARROW_TECHNICAL);
GraphConstants.setBeginSize(implementStyle, 10);
GraphConstants.setDashPattern(implementStyle, new float[] { 3, 3 });
GraphConstants.setFont(implementStyle, GraphConstants.defaultFont.deriveFont(10));
Map extendStyle = GraphConstants.createMap();
GraphConstants.setLineBegin(
extendStyle,
GraphConstants.ARROW_TECHNICAL);
GraphConstants.setBeginFill(extendStyle, true);
GraphConstants.setBeginSize(extendStyle, 10);
GraphConstants.setFont(extendStyle, GraphConstants.defaultFont.deriveFont(10));
Map aggregateStyle = GraphConstants.createMap();
GraphConstants.setLineBegin(
aggregateStyle,
GraphConstants.ARROW_DIAMOND);
GraphConstants.setBeginFill(aggregateStyle, true);
GraphConstants.setBeginSize(aggregateStyle, 6);
GraphConstants.setLineEnd(aggregateStyle, GraphConstants.ARROW_SIMPLE);
GraphConstants.setEndSize(aggregateStyle, 8);
GraphConstants.setLabelPosition(aggregateStyle, new Point(500, 1200));
GraphConstants.setFont(aggregateStyle, GraphConstants.defaultFont.deriveFont(10));
//
// The Swing MVC Pattern
//
// Model Column
DefaultGraphCell gm = new DefaultGraphCell("GraphModel");
attributes.put(gm, createBounds(20, 100, Color.blue));
gm.add(new DefaultPort("GraphModel/Center"));
DefaultGraphCell dgm = new DefaultGraphCell("DefaultGraphModel");
attributes.put(dgm, createBounds(20, 180, Color.blue));
dgm.add(new DefaultPort("DefaultGraphModel/Center"));
DefaultEdge dgmImplementsGm = new DefaultEdge("implements");
cs.connect(dgmImplementsGm, gm.getChildAt(0), dgm.getChildAt(0));
attributes.put(dgmImplementsGm, implementStyle);
DefaultGraphCell modelGroup = new DefaultGraphCell("ModelGroup");
modelGroup.add(gm);
modelGroup.add(dgm);
modelGroup.add(dgmImplementsGm);
// JComponent Column
DefaultGraphCell jc = new DefaultGraphCell("JComponent");
attributes.put(jc, createBounds(150, 20, Color.green));
jc.add(new DefaultPort("JComponent/Center"));
DefaultGraphCell jg = new DefaultGraphCell("JGraph");
attributes.put(jg, createBounds(150, 100, Color.green));
jg.add(new DefaultPort("JGraph/Center"));
DefaultEdge jgExtendsJc = new DefaultEdge("extends");
cs.connect(jgExtendsJc, jc.getChildAt(0), jg.getChildAt(0));
attributes.put(jgExtendsJc, extendStyle);
// UI Column
DefaultGraphCell cu = new DefaultGraphCell("ComponentUI");
attributes.put(cu, createBounds(280, 20, Color.red));
cu.add(new DefaultPort("ComponentUI/Center"));
DefaultGraphCell gu = new DefaultGraphCell("GraphUI");
attributes.put(gu, createBounds(280, 100, Color.red));
gu.add(new DefaultPort("GraphUI/Center"));
DefaultGraphCell dgu = new DefaultGraphCell("BasicGraphUI");
attributes.put(dgu, createBounds(280, 180, Color.red));
dgu.add(new DefaultPort("BasicGraphUI/Center"));
DefaultEdge guExtendsCu = new DefaultEdge("extends");
cs.connect(guExtendsCu, cu.getChildAt(0), gu.getChildAt(0));
attributes.put(guExtendsCu, extendStyle);
DefaultEdge dguImplementsDu = new DefaultEdge("implements");
cs.connect(dguImplementsDu, gu.getChildAt(0), dgu.getChildAt(0));
attributes.put(dguImplementsDu, implementStyle);
DefaultGraphCell uiGroup = new DefaultGraphCell("UIGroup");
uiGroup.add(cu);
uiGroup.add(gu);
uiGroup.add(dgu);
uiGroup.add(dguImplementsDu);
uiGroup.add(guExtendsCu);
// Aggregations
DefaultEdge jgAggregatesGm = new DefaultEdge("model");
cs.connect(jgAggregatesGm, jg.getChildAt(0), gm.getChildAt(0));
attributes.put(jgAggregatesGm, aggregateStyle);
DefaultEdge jcAggregatesCu = new DefaultEdge("ui");
cs.connect(jcAggregatesCu, jc.getChildAt(0), cu.getChildAt(0));
attributes.put(jcAggregatesCu, aggregateStyle);
// Insert Cells into model
Object[] cells =
new Object[] {
jgAggregatesGm,
jcAggregatesCu,
modelGroup,
jc,
jg,
jgExtendsJc,
uiGroup };
model.insert(cells, attributes, cs, null, null);
}
/**
* Returns an attributeMap for the specified position and color.
*/
public static Map createBounds(int x, int y, Color c) {
Map map = GraphConstants.createMap();
GraphConstants.setBounds(map, new Rectangle(x, y, 90, 30));
GraphConstants.setBorder(map, BorderFactory.createRaisedBevelBorder());
GraphConstants.setBackground(map, c.darker());
GraphConstants.setForeground(map, Color.white);
GraphConstants.setFont(map, GraphConstants.defaultFont.deriveFont(Font.BOLD, 12));
GraphConstants.setOpaque(map, true);
return map;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -