jgraph

JGraph一個簡單的開始,JGraph是一個開源的,兼容Swing的基於MVC體系結構圖形組件。

基本介紹

  • 軟體名稱:jgraph
  • 特點:基於Swing的擴展、簡單、高效
  • 費用:500美元
  • 軟體類型:開源軟體
特點,簡介,

特點

1) 基於Swing的擴展;(鑒於現在流行的SWT,這是一個缺點,不過SWT中加入Swing也是很方便的事)
2) 簡單、高效的設計;
3) 時間效率高;
4) 100 %純Java;
5) 強大的布局算法支持(雖然付費,大概500百美元,但其功能異常強大,適合像我這種不懂圖論的java程式設計師)

簡介

JGraph不包含實際的數據,它提供了數據的視;JGraph對象畫圖的機制是:
將圖元定義為一個一個的cell,每個cell可以是一個頂點(vertex)、邊(edge)或者節點(port)中的一種。頂點可以有鄰接的頂點,他們通過邊相聯繫,邊聯接的兩個端點稱為目標和源,每個目標或者源是一個節點。節點是頂點的孩子。每個cell都可以有自己的孩子。
每個cell的外觀由相應的屬性定義,屬性序列是指一系列的鍵-值對,他們以Map形式組織
例如:
Map map = new Hashtable();
// 設定一個矩形的vertex
Rectangle2D rect = new Rectangle2D.Double(20, 20, 40, 20);
//GraphConstants為設定整個graph圖像屬性的類
GraphConstants.setBounds(map, rect);
// 設定各種vertex屬性,這裡設定其顏色為例
GraphConstants.setBorderColor(map, Color.black);
來看看一個簡單的程式吧:
/* author scorpion 創建日期 2006-10-10*/
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
public class Hello {
public static void main(String[] args) {
// 定義一個存放cell的集合類
ArrayList<DefaultGraphCell> cells = new ArrayList<DefaultGraphCell>();
// model用於控制整個模型顯示屬性等,view用於控制你的圖形顯示屬性,這裡都用默認即可
GraphModel model = new DefaultGraphModel();
GraphLayoutCache view = new GraphLayoutCache(model,
new DefaultCellViewFactory());
// JGraph對象
JGraph graph = new JGraph(model, view);
// 建立你的第一個vertex對象
cells.add(new DefaultGraphCell(new String("Hello")));
GraphConstants.setBounds(cells.get(0).getAttributes(),
new Rectangle2D.Double(20, 20, 40, 20));
// 設定顏色和透明屬性
GraphConstants.setGradientColor(cells.get(0).getAttributes(),
Color.orange);
GraphConstants.setOpaque(cells.get(0).getAttributes(), true);
// 為這個vertex加入一個port
DefaultPort port0 = new DefaultPort();
cells.get(0).add(port0);
// 同理加入第二個vertex
cells.add(new DefaultGraphCell(new String("World")));
GraphConstants.setBounds(cells.get(1).getAttributes(),
new Rectangle2D.Double(200, 200, 40, 20));
GraphConstants
.setGradientColor(cells.get(1).getAttributes(), Color.red);
GraphConstants.setOpaque(cells.get(1).getAttributes(), true);
DefaultPort port1 = new DefaultPort();
cells.get(1).add(port1);
// 同理加入第三個vertex
cells.add(new DefaultGraphCell(new String("Scorpio")));
GraphConstants.setBounds(cells.get(2).getAttributes(),
new Rectangle2D.Double(20, 300, 50, 20));
GraphConstants.setGradientColor(cells.get(2).getAttributes(),
Color.blue);
GraphConstants.setOpaque(cells.get(2).getAttributes(), true);
DefaultPort port2 = new DefaultPort();
cells.get(2).add(port2);
// 加入一條邊,將hello和world的兩個port連線起來
DefaultEdge edge = new DefaultEdge();
edge.setSource(cells.get(0).getChildAt(0));
edge.setTarget(cells.get(1).getChildAt(0));
// 將edge加入cell集合類
cells.add(edge);
// 同理
DefaultEdge edge1 = new DefaultEdge();
edge1.setSource(cells.get(0).getChildAt(0));
edge1.setTarget(cells.get(2).getChildAt(0));
// 將edge加入cell集合類
cells.add(edge1);
// 同理
DefaultEdge edge2 = new DefaultEdge();
edge2.setSource(cells.get(2).getChildAt(0));
edge2.setTarget(cells.get(1).getChildAt(0));
// 將edge加入cell集合類
cells.add(edge2);
// 為edge設定一個箭頭
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
// 將以上定義的cells對象加入graph對象
Iterator it = cells.iterator();
while (it.hasNext()) {
graph.getGraphLayoutCache().insert(it.next());
}
// 一些graph對象的簡單調整
// graph.setMoveable(false);//可否移動整個圖形
// graph.setDisconnectable(false);//不能移動邊的指向,但是可以移動圖形
// graph.setDisconnectOnMove(false);//可否移動整個邊,但是在邊的源點終點改變後失效
// { graph.setGridEnabled(true); graph.setGridVisible(true); } // 顯示格線
// graph.setMoveBelowZero(true); //是否允許cell越出左上角.通常設定為false,除非有特殊用處
graph.setAntiAliased(true);// 圓滑圖像線條
// graph.setSelectionEnabled(false);//能否選擇單個cell
/*
* 加入改變邊屬性代碼位置
*/
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(graph));
frame.pack();
frame.setVisible(true);
}
}
由於在graph的cell中,決定其屬性的是Attribute,這是一個map類型,所以,JGraph的edit,insert和remove方法都是使用對Attribute map的修改
在上例程式中的“加入改變屬性顏色代碼”位置加入以下修改代碼,則可以改變其邊的顯示屬性
//改變邊顏色粗細
Map nested = new Hashtable();
Map attributeMap1 = new Hashtable();
GraphConstants.setLineColor(attributeMap1, Color.blue);
GraphConstants.setLineWidth(attributeMap1, 5);
nested.put(cells.get(4), attributeMap1);
//改變邊類型
Map attributeMap2 = new Hashtable();
GraphConstants.setLineColor(attributeMap2, Color.red);
GraphConstants
.setDashPattern(attributeMap2, new float[] { 5, 5, 5, 5 });
nested.put(cells.get(5), attributeMap2);
//將改動加入graph
graph.getGraphLayoutCache().edit(nested);// insert也可以使用類似的方法

相關詞條

熱門詞條

聯絡我們