public static void main(String argv[]) { // Some numbers ComplexNumber a, b, c; ArrayList<ComplexNumber> nodes = new ArrayList<ComplexNumber>(); nodes.add(a = new ComplexNumber(20, 20)); nodes.add(b = new ComplexNumber(100, 100)); nodes.add(c = new ComplexNumber(50, 50)); // Some relations (whatever they are) // When you don't have a first class entity for a relation use the placeholder ArrayList<RelationPlaceholder<ComplexNumber>> edges = new ArrayList<RelationPlaceholder<ComplexNumber>>(); edges.add(new RelationPlaceholder<ComplexNumber>(b, a)); edges.add(new RelationPlaceholder<ComplexNumber>(c, a)); // Example 1 Figure f = new Figure().observer(new SwingObserver()); f.nodesUsing( nodes, new RectangleNodePainter(true) .width( new AbstractNumericalCommand("Real") { public double execute() { return ((ComplexNumber) receiver).x; } }) .height( new AbstractNumericalCommand("Imag") { public double execute() { return ((ComplexNumber) receiver).y; } })); f.layout(new FlowLayout()); renderFigure(f); // Example 2 f = new Figure().observer(new SwingObserver()); f.nodesUsing( nodes, new EllipseNodePainter(10, 10, true) .x( new AbstractNumericalCommand("Real") { public double execute() { return ((ComplexNumber) receiver).x; } }) .y( new AbstractNumericalCommand("Imag") { public double execute() { return ((ComplexNumber) receiver).y; } }) .color( new LinearNormalizerColor( nodes, new AbstractNumericalCommand("Module") { public double execute() { return ((ComplexNumber) receiver).x * ((ComplexNumber) receiver).x + ((ComplexNumber) receiver).y * ((ComplexNumber) receiver).y; } })) .name( new AbstractStringCommand("toString") { public String execute() { return receiver.toString(); } })); f.layout(new ScatterPlotLayout()); renderFigure(f); // Example 3 f = new Figure().observer(new SwingObserver()); f.nodesUsing( nodes, new RectangleNodePainter(10, 10, true) .name( new AbstractStringCommand("toString") { public String execute() { return receiver.toString(); } })); f.edgesUsing( edges, new LineEdgePainter( new AbstractEntityCommand() { public Object execute() { return ((RelationPlaceholder) receiver).getSource(); } }, new AbstractEntityCommand() { public Object execute() { return ((RelationPlaceholder) receiver).getDestination(); } })); f.layout(new TreeLayout(50, 50)); renderFigure(f); // Example 4 f = new Figure().observer(new SwingObserver()); f.nodesUsingForEach( nodes, new RectangleNodePainter(10, 10, true) .name( new AbstractStringCommand("toString") { public String execute() { return receiver.toString(); } }), new AbstractFigureDescriptionCommand() { public Figure describe() { Figure in = new Figure().observer(observer); ArrayList list = new ArrayList(); // If a.equels(b) == true then a and b cannot have two distinct visual representattions! list.add(new Integer(((ComplexNumber) receiver).x)); list.add(new Integer(((ComplexNumber) receiver).y * 2)); in.nodesUsing( list, new RectangleNodePainter(20, 20, true) .name( new AbstractStringCommand("Name") { public String execute() { return receiver.toString(); } })); in.layout(new FlowLayout()); return in; } }); f.edgesUsing( edges, new LineEdgePainter( new AbstractEntityCommand() { public Object execute() { return ((RelationPlaceholder) receiver).getSource(); } }, new AbstractEntityCommand() { public Object execute() { return ((RelationPlaceholder) receiver).getDestination(); } })); f.layout(new TreeLayout(50, 50)); renderFigure(f); }
private static void renderFigure(Figure<?> f) { ViewRenderer r = new ViewRenderer(); f.renderOn(r); r.open(); }