In order to make the edge with the arrow, I set up some line of codes in the GraphView.java example.
However, the arrow didn't show up. Is there any way to show the arrow?
The following lines are what I did for the edge with arrow.
EdgeRenderer er = new EdgeRenderer(Constants.EDGE_TYPE_LINE, Constants.EDGE_ARROW_FORWARD);
drf.setDefaultEdgeRenderer(er);
m_vis.setRendererFactory(drf);
draw.add(new ColorAction(edges, VisualItem.FILLCOLOR, ColorLib.gray(200)));
draw.add(new ColorAction(edges, VisualItem.STROKECOLOR, ColorLib.gray(200)));
The below source code is the whole example code.
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(new BorderLayout());
// create a new, empty visualization for our data
m_vis = new Visualization();
// --------------------------------------------------------------------
// set up the renderers
LabelRenderer tr = new LabelRenderer();
tr.setRoundedCorner(8, 8);
DefaultRendererFactory drf = new DefaultRendererFactory(tr);
// set up the edge with arrow
EdgeRenderer er = new EdgeRenderer(Constants.EDGE_TYPE_LINE, Constants.EDGE_ARROW_FORWARD);
drf.setDefaultEdgeRenderer(er);
m_vis.setRendererFactory(drf);
// --------------------------------------------------------------------
// register the data with a visualization
// adds graph to visualization and sets renderer label field
setGraph(g, label);
// fix selected focus nodes
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(false);
}
m_vis.run("draw");
}
});
// --------------------------------------------------------------------
// create actions to process the visual data
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));
// add colors for edge and arrow.
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());
// finally, we register our ActionList with the Visualization.
// we can later execute our Actions by invoking a method on our
// Visualization, using the name we've chosen below.
m_vis.putAction("draw", draw);
m_vis.putAction("layout", animate);
m_vis.runAfter("draw", "layout");
// --------------------------------------------------------------------
// set up a display to show the visualization
Display display = new Display(m_vis);
display.setSize(700,700);
display.pan(350, 350);
display.setForeground(Color.GRAY);
display.setBackground(Color.WHITE);
// main display controls
display.addControlListener(new FocusControl(1));
display.addControlListener(new DragControl());
display.addControlListener(new PanControl());
display.addControlListener(new ZoomControl());
display.addControlListener(new WheelZoomControl());
display.addControlListener(new ZoomToFitControl());
display.addControlListener(new NeighborHighlightControl());
display.setForeground(Color.GRAY);
display.setBackground(Color.WHITE);
// --------------------------------------------------------------------
// launch the visualization
// create a panel for editing force values
ForceSimulator fsim = ((ForceDirectedLayout)animate.get(0)).getForceSimulator();
JForcePanel fpanel = new JForcePanel(fsim);
final JValueSlider slider = new JValueSlider("Distance", 0, hops, hops);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
filter.setDistance(slider.getValue().intValue());
m_vis.run("draw");
}
});
slider.setBackground(Color.WHITE);
slider.setPreferredSize(new Dimension(300,30));
slider.setMaximumSize(new Dimension(300,30));
Box cf = new Box(BoxLayout.Y_AXIS);
cf.add(slider);
cf.setBorder(BorderFactory.createTitledBorder("Connectivity Filter"));
fpanel.add(cf);
//fpanel.add(opanel);
fpanel.add(Box.createVerticalGlue());
// create a new JSplitPane to present the interface
JSplitPane split = new JSplitPane();
split.setLeftComponent(display);
split.setRightComponent(fpanel);
split.setOneTouchExpandable(true);
split.setContinuousLayout(false);
split.setDividerLocation(700);
// now we run our action list
m_vis.run("draw");
add(split);
}
public void setGraph(Graph g, String label) {
// update labeling
DefaultRendererFactory drf = (DefaultRendererFactory)
m_vis.getRendererFactory();
((LabelRenderer)drf.getDefaultRenderer()).setTextField(label);
// update graph
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);
}
// ------------------------------------------------------------------------
// Main and demo methods
public static void main(String[] args) {
UILib.setPlatformLookAndFeel();
// create graphview
String datafile = null;
String label = "label";
if ( args.length > 1 ) {
datafile = args[0];
label = args[1];
}
JFrame frame = demo(datafile, label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static JFrame demo() {
return demo((String)null, "label");
}
public static JFrame demo(String datafile, String label) {
Graph g = null;
if ( datafile == null ) {
g = GraphLib.getGrid(15,15);
label = "label";
} else {
try {
g = new GraphMLReader().readGraph(datafile);
} catch ( Exception e ) {
e.printStackTrace();
System.exit(1);
}
}
return demo(g, label);
}
public static JFrame demo(Graph g, String label) {
final GraphView view = new GraphView(g, label);
// set up menu
JMenu dataMenu = new JMenu("Data");
dataMenu.add(new OpenGraphAction(view));
dataMenu.add(new GraphMenuAction("Grid","ctrl 1",view) {
protected Graph getGraph() {
return GraphLib.getGrid(15,15);
}
});
dataMenu.add(new GraphMenuAction("Clique","ctrl 2",view) {
protected Graph getGraph() {
return GraphLib.getClique(10);
}
});
dataMenu.add(new GraphMenuAction("Honeycomb","ctrl 3",view) {
protected Graph getGraph() {
return GraphLib.getHoneycomb(5);
}
});
dataMenu.add(new GraphMenuAction("Balanced Tree","ctrl 4",view) {
protected Graph getGraph() {
return GraphLib.getBalancedTree(3,5);
}
});
dataMenu.add(new GraphMenuAction("Diamond Tree","ctrl 5",view) {
protected Graph getGraph() {
return GraphLib.getDiamondTree(3,3,3);
}
});
JMenuBar menubar = new JMenuBar();
menubar.add(dataMenu);
// launch window
JFrame frame = new JFrame("p r e f u s e | g r a p h v i e w");
frame.setJMenuBar(menubar);
frame.setContentPane(view);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
view.m_vis.run("layout");
}
public void windowDeactivated(WindowEvent e) {
view.m_vis.cancel("layout");
}
});
return frame;
}
// ------------------------------------------------------------------------
/**
* Swing menu action that loads a graph into the graph viewer.
*/
public abstract static class GraphMenuAction extends AbstractAction {
private GraphView m_view;
public GraphMenuAction(String name, String accel, GraphView view) {
m_view = view;
this.putValue(AbstractAction.NAME, name);
this.putValue(AbstractAction.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(accel));
}
public void actionPerformed(ActionEvent e) {
m_view.setGraph(getGraph(), "label");
}
protected abstract Graph getGraph();
}
public static class OpenGraphAction extends AbstractAction {
private GraphView m_view;
public OpenGraphAction(GraphView view) {
m_view = view;
this.putValue(AbstractAction.NAME, "Open File...");
this.putValue(AbstractAction.ACCELERATOR_KEY,
KeyStroke.getKeyStroke("ctrl O"));
}
public void actionPerformed(ActionEvent e) {
Graph g = IOLib.getGraphFile(m_view);
if ( g == null ) return;
String label = getLabel(m_view, g);
if ( label != null ) {
m_view.setGraph(g, label);
}
}
public static String getLabel(Component c, Graph g) {
// get the column names
Table t = g.getNodeTable();
int cc = t.getColumnCount();
String[] names = new String[cc];
for ( int i=0; i<cc; ++i )
names[i] = t.getColumnName(i);
// where to store the result
final String[] label = new String[1];
// -- build the dialog -----
// we need to get the enclosing frame first
while ( c != null && !(c instanceof JFrame) ) {
c = c.getParent();
}
final JDialog dialog = new JDialog(
(JFrame)c, "Choose Label Field", true);
// create the ok/cancel buttons
final JButton ok = new JButton("OK");
ok.setEnabled(false);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVis