I am developing one application using Jung2 which will show the connection type between two point,there is two different type of connection are available.I want to show each type of connection using different color.For that i want to add two different colored edge between two vertex from A to B it will be black and B to A it will be green.Can any one help me with an example...
Asked
Active
Viewed 1,017 times
1
-
This is not only connection between two point.Actually i am drawing a network graph using JUNG. – Anupam Maiti Mar 27 '14 at 04:58
1 Answers
4
You have to define a Transformer
that receives an edge and returns a Paint
- in this case, the Paint
can simply be a Color
. This Transformer
has to be assigned to the RenderContext
as
- ArrowFillPaintTransformer
- ArrowDrawPaintTransformer
- EdgeDrawPaintTransformer
The decision of whether the edge has to be painted in green or in black is based on the vertices in this example (The statement if (s.equals("v1") && d.equals("v0"))
means that it is the edge from "v1"
to "v0"
).
import java.awt.Color;
import java.awt.Paint;
import javax.swing.JFrame;
import org.apache.commons.collections15.Transformer;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
public class JUNGEdgeColorsTest
{
public static void main(String[] args)
{
JFrame jf = new JFrame();
final Graph<String, String> g = getGraph();
VisualizationViewer<String, String> vv =
new VisualizationViewer<String, String>(new FRLayout<String, String>(g));
Transformer<String, Paint> colorTransformer = new Transformer<String, Paint>()
{
@Override
public Paint transform(String e)
{
final String s = g.getSource(e);
final String d = g.getDest(e);
if (s.equals("v1") && d.equals("v0"))
{
return Color.GREEN;
}
return Color.BLACK;
}
};
vv.getRenderContext().setArrowFillPaintTransformer(colorTransformer);
vv.getRenderContext().setArrowDrawPaintTransformer(colorTransformer);
vv.getRenderContext().setEdgeDrawPaintTransformer(colorTransformer);
jf.getContentPane().add(vv);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static Graph<String, String> getGraph()
{
Graph<String, String> g = new DirectedSparseGraph<String, String>();
g.addVertex("v0");
g.addVertex("v1");
g.addEdge("e0", "v0", "v1");
g.addEdge("e1", "v1", "v0");
return g;
}
}

Marco13
- 53,703
- 9
- 80
- 159