prefuse 是一个关系绘制数据关系的开源项目,运用它可以绘制数据关系的图形,网上资源很少,根据demo研究了一下绘制了一个简单的图形 请多指教 ,
首先 prefuse 画图需要的关键几个类 , Graph , Visuation, Display,其中Graph用来构建图形关系,visulation用来把自己设计的效果颜色等附加到图形上, Display就用来展示图形界面。
代码贴上
public class GraphView extends JPanel {
private static final String graph = "graph";
private static final String nodes = "graph.nodes";
private static final String edges = "graph.edges";
private Visualization m_vis;
public GraphView(Graph g, String label) {
super();
// 创造新视图
m_vis = new Visualization();
//设置渲染器
LabelRenderer tr = new LabelRenderer();
tr.setRoundedCorner(8, 8);
m_vis.setRendererFactory(new DefaultRendererFactory(tr));
setGraph(g, label);
// 定义风格
int hops = 30;
final GraphDistanceFilter filter = new GraphDistanceFilter(graph, hops);
ColorAction fill = new ColorAction(nodes, VisualItem.FILLCOLOR, ColorLib.rgb(200,200,255));
fill.add(VisualItem.FIXED, ColorLib.rgb(255,100,100));
fill.add(VisualItem.HIGHLIGHT, ColorLib.rgb(255,200,125));
ActionList draw = new ActionList();
draw.add(filter);
draw.add(fill);
draw.add(new ColorAction(nodes, VisualItem.STROKECOLOR, 0));
draw.add(new ColorAction(nodes, VisualItem.TEXTCOLOR, ColorLib.rgb(0,0,0)));
draw.add(new ColorAction(edges, VisualItem.FILLCOLOR, ColorLib.gray(200)));
draw.add(new ColorAction(edges, VisualItem.STROKECOLOR, ColorLib.gray(200)));
ActionList animate = new ActionList(Activity.INFINITY);
animate.add(new ForceDirectedLayout(graph));
animate.add(fill);
animate.add(new RepaintAction());
//添加显示风格
m_vis.putAction("draw", draw);
m_vis.putAction("layout", animate);
m_vis.runAfter("draw", "layout");
// --------------------------------------------------------------------
//设置显示大小
Display display = new Display(m_vis);
display.setSize(700,700);
display.pan(400, 400);
// 增加监听器
display.addControlListener(new FocusControl(){
public void itemEntered(VisualItem item, MouseEvent e) {
JPanel tipJpanel=new JPanel();
tipJpanel.setSize(300, 300);
ToolTipControl toolTip=new ToolTipControl("column_name");
toolTip.itemEntered(item,e);
}
public void itemPressed(VisualItem item, MouseEvent e){
}
});
display.addControlListener(new DragControl());
display.addControlListener(new PanControl());
display.addControlListener(new ZoomControl());
display.addControlListener(new WheelZoomControl());
display.addControlListener(new ZoomToFitControl());
display.addControlListener(new NeighborHighlightControl());
//定义节点被选中时的效果
TupleSet focusGroup = m_vis.getGroup(Visualization.FOCUS_ITEMS);
focusGroup.addTupleSetListener(new TupleSetListener() {
public void tupleSetChanged(TupleSet ts, Tuple[] add, Tuple[] rem)
{
for ( int i=0; i<rem.length; ++i )
((VisualItem)rem[i]).setFixed(false);
for ( int i=0; i<add.length; ++i ) {
((VisualItem)add[i]).setFixed(false);
((VisualItem)add[i]).setFixed(true);
}
if ( ts.getTupleCount() == 0 ) {
ts.addTuple(rem[0]);
((VisualItem)rem[0]).setFixed(true);
}
m_vis.run("draw");
}
});
this.add(display);
m_vis.run("draw");
}
public void setGraph(Graph g, String label) {
//更新渲染器
DefaultRendererFactory drf = (DefaultRendererFactory) m_vis.getRendererFactory();
((LabelRenderer)drf.getDefaultRenderer()).setTextField(label);
// 更新图
m_vis.removeGroup(graph);
VisualGraph vg = m_vis.addGraph(graph, g);
m_vis.setValue(edges, null, VisualItem.INTERACTIVE, Boolean.FALSE);
VisualItem f = (VisualItem)vg.getNode(0);
m_vis.getGroup(Visualization.FOCUS_ITEMS).setTuple(f);
f.setFixed(false);
}
// ------------------------------------------------------------------------
public static void main(String[] args) {
UILib.setPlatformLookAndFeel();
Table t1=new Table();
t1.addColumn("Person", String.class);
t1.addColumn("Titel", String.class);
t1.addColumn("FirstName", String.class);
t1.addColumn("SecondName", String.class);
t1.addColumn("Gender", String.class);
t1.addColumn("Birthday", String.class);
t1.addColumn("Department", String.class);
t1.addColumn("Function", String.class);
t1.addColumn("Boss", String.class);
t1.addColumn("Telephon", String.class);
t1.addColumn("Handy", String.class);
t1.addColumn("Tele_Home", String.class);
t1.addRow();
t1.setString(0, 0, "Tom Hans");
t1.setString(0, 1, "Dr.");
t1.setString(0, 2, "Tom");
t1.setString(0, 3, "Hans");
t1.setString(0, 4, "male");
t1.setString(0, 5, "1978-05-09");
t1.setString(0, 6, "Marketing");
t1.setString(0, 8, "Anny Tiger");
t1.setString(0, 9, "0194537653");
t1.setString(0, 10, "0191345667828");
//***Start: Graph data init
Graph graph=new Graph(true);//表示是否为有向图 false无向 true有向
graph.addColumn("data", String.class);
graph.addColumn("column_name", String.class);
graph.addColumn("id", int.class);
graph.addColumn("toShow", String.class);
graph.addColumn("size", int.class);
for(int i=0;i<t1.getColumnCount();i++)
{
Node n=graph.addNode();
n.setString("data", String.valueOf(t1.get(0, i)));
n.setString("column_name", t1.getColumnName(i));
n.setInt("id", i);
n.setString("toShow", n.get(1)+":\n"+n.get(0));
}
for(int i=1;i<t1.getColumnCount();i++){
graph.addEdge(0, i);
}
String label="toShow";
JFrame frame = demo(graph, label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static JFrame demo(Graph g, String label) {
final GraphView view = new GraphView(g, label);
// launch window
JFrame frame = new JFrame("p r e f u s e | g r a p h v i e w");
frame.setContentPane(view);
frame.pack();
frame.setVisible(true);
return frame;
}
}