我想在我的应用程序中绘制包括顶点和边的一些图。我发现JGraph是绘制图表的好库。我浏览了一些关于它的在线资源,但找不到有关如何在Swing应用程序中嵌入JGraph的相关文章。 (在JFrame
等中显示JGraph)。任何人都可以帮助我吗?JFrame中的JGraph
2
A
回答
3
此代码为我工作:
// 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);
如果你想整个样品,我可以把它上传过。
我认为代码必须是来自网站的修改样本。在这里你去:
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;
}
}
这是一个非常简单的例子。我已经完成了一些数据库数据的复杂图形,但最终与Jung2合作,这更符合我的需求。
+0
可以请你上传整个样本..这对我非常有帮助! ! thanxx – Nuwan
+0
好的!只是在答案中添加了代码。 –
1
也许你在找什么是G。
2
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
相关问题
- 1. Java jframe jgraph滚动平面
- 2. 将JGraph嵌入到JFrame中与YAML
- 3. Jgraph in JavaFx
- 4. jgraph端口大小
- 5. JGraph在eclipse插件
- 6. 等价于JavaFX8的JGraph?
- 7. JGraph中的“刷新”单元格
- 8. jgraph布局不起作用
- 9. 使用Jgraph创建图表
- 10. UndirectedGraph使用JgraphT/Jgraph定义
- 11. 在jgraph中创建成本函数
- 12. JFrame内的JFrame
- 13. JFrame + JDialog的JFrame
- 14. jgraph像vaadin框架上的组件
- 15. JGraph - 压倒一切的细胞阻力
- 16. Python是否有JGraph的替代方案?
- 17. 如何启动基于JGraph的工具
- 18. Java中的JFrame
- 19. JFrame中的JPanel
- 20. JApplet中的JFrame
- 21. JGraph(T)图元素选择定制
- 22. 想用JPanel制作一个JGraph
- 23. Java jgraph applet cmd jar不会显示
- 24. Java jgraph applet可视化二分图
- 25. (JGraph/mxGraph)返回所选单元格
- 26. JGraph进入/退出/主页监听器
- 27. 将元数据添加到使用jgraph创建的xml中
- 28. JFrame中的图像
- 29. JFrame中的错误
- 30. 的Java:在JFrame中
见[JGraph支持](http://www.jgraph.com/support.html)。 –
您是否需要实际绘制图表或让它出现在JFrame中的帮助?你想知道如何获取数据并将其显示在图表上? – gsfd
或http://www.java2s.com/Code/Java/Chart/CatalogChart.htm,但教程和论坛是作者选定的defalut – mKorbel