prefuse点击边后整个图消失
代码如下 出来图形后 点击边 整个图消失
package 呵呵;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.SwingConstants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.filter.GraphDistanceFilter;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.activity.Activity;
import prefuse.controls.ControlAdapter;
import prefuse.controls.DragControl;
import prefuse.controls.FocusControl;
import prefuse.controls.NeighborHighlightControl;
import prefuse.controls.PanControl;
import prefuse.controls.WheelZoomControl;
import prefuse.controls.ZoomControl;
import prefuse.controls.ZoomToFitControl;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Schema;
import prefuse.data.Table;
import prefuse.data.Tuple;
import prefuse.data.event.TupleSetListener;
import prefuse.data.io.DataIOException;
import prefuse.data.io.sql.ConnectionFactory;
import prefuse.data.io.sql.DatabaseDataSource;
import prefuse.data.tuple.TupleSet;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.LabelRenderer;
import prefuse.util.ColorLib;
import prefuse.util.FontLib;
import prefuse.util.PrefuseLib;
import prefuse.util.force.ForceSimulator;
import prefuse.util.ui.JFastLabel;
import prefuse.util.ui.JPrefuseApplet;
import prefuse.util.ui.UILib;
import prefuse.visual.EdgeItem;
import prefuse.visual.NodeItem;
import prefuse.visual.VisualGraph;
import prefuse.visual.VisualItem;
/**
* @author <a href="http://jheer.org">jeffrey heer</a>
*/
public class MysqlGraphView2 extends JPrefuseApplet {
private static final String graph = "graph";
private static final String nodes = "graph.nodes";
private static final String edges = "graph.edges";
static String label1;
private int seed = 0;
private Map<Integer, String> keyMap = new HashMap<Integer, String>();
private Map<String, Integer> relKeyMap = new HashMap<String, Integer>();
private Table node;
private Table edge;
public void init() {
UILib.setPlatformLookAndFeel();
JComponent graphview = demo("label","users");
this.getContentPane().add(graphview);
}
public void initNodeAndEdge() {
Schema nodeSchema = new Schema(3);
nodeSchema.addColumn("id", int.class);
nodeSchema.addColumn("label", String.class);
nodeSchema.addColumn("type", String.class);
nodeSchema.addColumn("ext", String.class);
node = nodeSchema.instantiate();
Schema edgeSchema = new Schema(3);
edgeSchema.addColumn("id", int.class);
edgeSchema.addColumn("sid", int.class);
edgeSchema.addColumn("tid", int.class);
edge = edgeSchema.instantiate();
}
public JComponent demo(String name,String tableName) {
label1 = name;
System.out.println("name=" + name);
Graph graph = null;
try {
// datasrc = ConnectionFactory.getDatabaseConnection(driverName,dbURL, userName, userPwd);
initNodeAndEdge();
node.addRow();
node.set(0, 0, 0);
node.set(0, 1, "aa1");
node.addRow();
node.set(1, 0, 1);
node.set(1, 1, "aa2");
edge.addRow();
edge.set(0, 0, 0);
edge.set(0, 1, 0);
edge.set(0, 2, 1);
graph = new Graph(node, edge, false, "id", "sid", "tid");
} catch (Exception e) {
e.printStackTrace();
}
return demo(graph, name);
}
public JComponent demo(Graph g, String label) {
System.out.println("label=" + label);
final Visualization vis = new Visualization();
VisualGraph vg = vis.addGraph(graph, g);
vis.setInteractive(edges, null, true);
TupleSet focusGroup = 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);
}
vis.run("draw");
}
});
LabelRenderer tr = new LabelRenderer(label);
tr.setRoundedCorner(9, 9);
vis.setRendererFactory(new DefaultRendererFactory(tr));
int hops = 10;
final GraphDistanceFilter filter = new GraphDistanceFilter(graph, hops);
ActionList draw = new ActionList();
draw.add(filter);
draw.add(new ColorAction(nodes, VisualItem.FILLCOLOR, ColorLib.rgb(200,
200, 255)));
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)));
ColorAction fill = new ColorAction(nodes, VisualItem.FILLCOLOR,
ColorLib.rgb(200, 200, 255));
fill.add("_fixed", ColorLib.rgb(255, 100, 100));
fill.add("_highlight", ColorLib.rgb(255, 200, 125));
ForceDirectedLayout fdl = new ForceDirectedLayout(graph);
ForceSimulator fsim = fdl.getForceSimulator();
fsim.getForces()[0].setParameter(0, -1.2f);// 1 Gravitational参数
ActionList animate = new ActionList(Activity.INFINITY);
animate.add(fdl);
animate.add(fill);
animate.add(new RepaintAction());
vis.putAction("draw", draw);
vis.putAction("layout", animate);
vis.runAfter("draw", "layout");
Display display = new Display(vis);
display.setSize(500,500);
display.addControlListener(new FocusControl(1));
NodeItem focus = (NodeItem) vg.getNode(0);
PrefuseLib.setX(focus, null, 680);
PrefuseLib.setY(focus, null, 380);
focusGroup.setTuple(focus);
return display;
}
} // end of class GraphView
补充:Java , Java SE