📄 j6_09.java
字号:
package hello;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.util.Hashtable;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
public class j6_09 {
public static void main(String[] args) {
// Construct Model and Graph
GraphModel model = new DefaultGraphModel();
JGraph graph = new JGraph(model);
graph.setSelectNewCells(true);
// Create Nested Map (from Cells to Attributes)
Map attributes = new Hashtable();
// Create Hello Vertex
DefaultGraphCell hello = new DefaultGraphCell("Hello");
// Create Hello Vertex Attributes
Map helloAttrib = GraphConstants.createMap();
attributes.put(hello, helloAttrib);
// Set bounds
Rectangle2D helloBounds = GraphConstants.createRect(20, 20, 40, 20);
GraphConstants.setBounds(helloAttrib, helloBounds);
// Set black border
GraphConstants.setBorderColor(helloAttrib, Color.black);
// Add a Port
DefaultPort hp = new DefaultPort();
hello.add(hp);
// Create World Vertex
DefaultGraphCell world = new DefaultGraphCell("World");
// Create World Vertex Attributes
Map worldAttrib = GraphConstants.createMap();
attributes.put(world, worldAttrib);
// Set bounds
Rectangle2D worldBounds = GraphConstants.createRect(140, 140, 40, 20);
GraphConstants.setBounds(worldAttrib , worldBounds);
// Set fill color
GraphConstants.setBackground(worldAttrib, Color.orange);
GraphConstants.setOpaque(worldAttrib, true);
// Set raised border
GraphConstants.setBorder(worldAttrib,
BorderFactory.createRaisedBevelBorder());
// Add a Port
DefaultPort wp = new DefaultPort();
world.add(wp);
// Create Edge
DefaultEdge edge = new DefaultEdge();
// Create Edge Attributes
Map edgeAttrib = GraphConstants.createMap();
attributes.put(edge, edgeAttrib);
// Set Arrow
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edgeAttrib , arrow);
GraphConstants.setEndFill(edgeAttrib, true);
// Connect Edge
ConnectionSet cs = new ConnectionSet(edge, hp, wp);
Object[] cells = new Object[]{edge, hello, world};
// Insert into Model
model.insert(cells, attributes, cs, null, null);
// Show in Frame
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(graph));
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -