java JFrame 中的 JGraph
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6722035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JGraph in a JFrame
提问by Nuwan
I want to draw some graphs including vertices and edges in my application. I found that JGraph is a good library for plotting graphs. I went through some online sources about it but couldn't find any relevant articles about how to embed a JGraph in a Swing application. (Showing a JGraph in a JFrame
etc). Can Any one help me with that?
我想在我的应用程序中绘制一些图形,包括顶点和边。我发现 JGraph 是一个很好的绘图库。我浏览了一些关于它的在线资源,但找不到任何关于如何在 Swing 应用程序中嵌入 JGraph 的相关文章。(在JFrame
等中显示 JGraph )。任何人都可以帮助我吗?
回答by Costis Aivalis
This code worked for me:
这段代码对我有用:
// Insert the cells via the cache, so they get selected
graph.getGraphLayoutCache().insert(cells);
// Show in Frame
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(graph));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
If you want the whole sample i can upload it too.
如果你想要整个样本,我也可以上传。
I think the code must be a modified sample from the site. Here you go:
我认为代码必须是来自该站点的修改后的示例。干得好:
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
public class Foo {
public static void main(String[] args) {
// Construct Model and Graph
GraphModel model = new DefaultGraphModel();
JGraph graph = new JGraph(model);
// Control-drag should clone selection
graph.setCloneable(true);
// Enable edit without final RETURN keystroke
graph.setInvokesStopCellEditing(true);
// When over a cell, jump to its default port (we only have one, anyway)
graph.setJumpToDefaultPort(true);
// Insert all three cells in one call, so we need an array to store them
DefaultGraphCell[] cells = new DefaultGraphCell[3];
// Create Hello Vertex
cells[0] = createVertex("Hello", 20, 20, 40, 20, null, false );
// Create World Vertex
cells[1] = createVertex("World", 140, 140, 40, 20,
Color.ORANGE, true);
// Create Edge
DefaultEdge edge = new DefaultEdge("foo");
// Fetch the ports from the new vertices, and connect them with the edge
edge.setSource(cells[0].getChildAt(0));
edge.setTarget(cells[0].getChildAt(0));
cells[2] = edge;
// Create Edge
DefaultEdge edge1 = new DefaultEdge();
// Fetch the ports from the new vertices, and connect them with the edge
// cells[0].addPort();
// cells[1].addPort();
// edge1.setSource(cells[1]);
// edge1.setTarget(cells[0]);
// cells[3] = edge1;
// Set Arrow Style for edge
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
// Insert the cells via the cache, so they get selected
graph.getGraphLayoutCache().insert(cells);
// Show in Frame
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(graph));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static DefaultGraphCell createVertex(String name, double x,
double y, double w, double h, Color bg, boolean raised) {
// Create vertex with the given name
DefaultGraphCell cell = new DefaultGraphCell(name);
// Set bounds
GraphConstants.setBounds(cell.getAttributes(),
new Rectangle2D.Double(x, y, w, h));
// Set fill color
if (bg != null) {
GraphConstants.setGradientColor(cell.getAttributes(), bg);
GraphConstants.setOpaque(cell.getAttributes(), true);
}
// Set raised border
if (raised) {
GraphConstants.setBorder(cell.getAttributes(),
BorderFactory.createRaisedBevelBorder());
} else // Set black border
{
GraphConstants.setBorderColor(cell.getAttributes(),
Color.black);
}
// Add a Floating Port
cell.addPort();
return cell;
}
}
This is a very simple example. I have done a few complicated graphs of database data, but ended up working with Jung2which fits my needs better.
这是一个非常简单的例子。我已经完成了一些复杂的数据库数据图表,但最终使用了更适合我需求的Jung2。
回答by trashgod
JGraph
includes the packages com.mxgraph.examples.swing
and com.mxgraph.examples.swing.editor
in the examples folder:
JGraph
包括包com.mxgraph.examples.swing
和com.mxgraph.examples.swing.editor
示例文件夹中:
$ ls examples/com/mxgraph/examples/swing ClickHandler.java GraphEditor.java SchemaEditor.java editor CustomCanvas.java HelloWorld.java UserObject.java images FixedPoints.java Port.java Validation.java resources $ ls examples/com/mxgraph/examples/swing/editor BasicGraphEditor.java EditorMenuBar.java JTableRenderer.java DefaultFileFilter.java EditorPalette.java SchemaEditorMenuBar.java EditorAboutFrame.java EditorPopupMenu.java SchemaEditorToolBar.java EditorActions.java EditorRuler.java SchemaGraphComponent.java EditorKeyboardHandler.java EditorToolBar.java ShadowBorder.java