Search in sources :

Example 1 with VertexPaintFunction

use of edu.uci.ics.jung.graph.decorators.VertexPaintFunction in project tdq-studio-se by Talend.

the class MyFirstMain method run.

public void run(final GraphBuilder graphbuilder) {
    Graph graph = graphbuilder.createMultiGraph(allData);
    PluggableRenderer pr = new PluggableRenderer();
    vv = new VisualizationViewer(new FRLayout(graph), pr);
    // vv = new VisualizationViewer(new CircleLayout(graph), pr);
    // vv = new VisualizationViewer(new SpringLayout(graph), pr);
    // vv = new VisualizationViewer(new KKLayout(graph), pr);
    vv.setBackground(Color.white);
    vv.setPickSupport(new ShapePickSupport());
    pr.setEdgeShapeFunction(new EdgeShape.Line());
    vv.addPostRenderPaintable(new VisualizationViewer.Paintable() {

        int x;

        int y;

        Font font;

        FontMetrics metrics;

        int swidth;

        int sheight;

        String str = "My first Demo";

        public void paint(Graphics g) {
            Dimension d = vv.getSize();
            if (font == null) {
                font = new Font(g.getFont().getName(), Font.BOLD, 30);
                metrics = g.getFontMetrics(font);
                swidth = metrics.stringWidth(str);
                sheight = metrics.getMaxAscent() + metrics.getMaxDescent();
                x = (d.width - swidth) / 2;
                y = (int) (d.height - sheight * 1.5);
            }
            g.setFont(font);
            Color oldColor = g.getColor();
            g.setColor(Color.DARK_GRAY);
            g.drawString(str, x, y);
            g.setColor(oldColor);
        }

        public boolean useTransform() {
            return false;
        }
    });
    pr.setVertexPaintFunction(new VertexPaintFunction() {

        public Paint getFillPaint(Vertex v) {
            final Object userDatum = v.getUserDatum(GraphBuilder.COLUMN_IDX_KEY);
            if (userDatum != null) {
                Integer colIndex = (Integer) userDatum;
                return AWTColorUtils.getColor(colIndex);
            }
            return null;
        }

        public Paint getDrawPaint(Vertex v) {
            return Color.BLACK;
        }
    });
    pr.setVertexStringer(new VertexStringer() {

        public String getLabel(ArchetypeVertex v) {
            final Object userDatum = v.getUserDatum(GraphBuilder.V_LABEL_KEY);
            if (userDatum != null) {
                return (String) userDatum;
            }
            return null;
        }
    });
    pr.setEdgeStrokeFunction(new EdgeStrokeFunction() {

        public Stroke getStroke(Edge e) {
            Integer weight = graphbuilder.getEdgeWeight().getNumber(e).intValue();
            // return new BasicStroke(10 * weight / graphbuilder.getTotalWeight());
            return new BasicStroke(10.0f / weight);
        }
    });
    pr.setEdgePaintFunction(new EdgePaintFunction() {

        public Paint getFillPaint(Edge e) {
            return null;
        }

        public Paint getDrawPaint(Edge e) {
            return getInternalPaint(e);
        }

        private Paint getInternalPaint(Edge e) {
            final Object userDatum = e.getUserDatum(GraphBuilder.E_ROWNUM_KEY);
            if (userDatum != null) {
                Integer color = (Integer) userDatum;
                return AWTColorUtils.getColor(color);
            }
            return Color.GRAY;
        }
    });
    pr.setEdgeStringer(new EdgeStringer() {

        public String getLabel(ArchetypeEdge arg0) {
            return String.valueOf(graphbuilder.getEdgeWeight().getNumber(arg0));
        // final Object userDatum = arg0.getUserDatum(GraphBuilder.E_LABEL_KEY);
        // if (userDatum != null) {
        // return (String) userDatum;
        // }
        // return null;
        }
    });
    // vv.addGraphMouseListener(new TestGraphMouseListener());
    // add my listener for ToolTips
    vv.setToolTipFunction(new DefaultToolTipFunction());
    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphMouse graphMouse = new DefaultModalGraphMouse();
    vv.setGraphMouse(graphMouse);
    JMenuBar menu = new JMenuBar();
    menu.add(((DefaultModalGraphMouse) graphMouse).getModeMenu());
    panel.setCorner(menu);
    final ScalingControl scaler = new CrossoverScalingControl();
    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });
    JButton reset = new JButton("reset");
    reset.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            vv.getLayoutTransformer().setToIdentity();
            vv.getViewTransformer().setToIdentity();
        }
    });
    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(reset);
    content.add(controls, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}
Also used : BasicStroke(java.awt.BasicStroke) Vertex(edu.uci.ics.jung.graph.Vertex) ArchetypeVertex(edu.uci.ics.jung.graph.ArchetypeVertex) JPanel(javax.swing.JPanel) ActionEvent(java.awt.event.ActionEvent) FRLayout(edu.uci.ics.jung.visualization.FRLayout) JButton(javax.swing.JButton) Font(java.awt.Font) Container(java.awt.Container) JFrame(javax.swing.JFrame) FontMetrics(java.awt.FontMetrics) ArchetypeEdge(edu.uci.ics.jung.graph.ArchetypeEdge) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) ScalingControl(edu.uci.ics.jung.visualization.control.ScalingControl) CrossoverScalingControl(edu.uci.ics.jung.visualization.control.CrossoverScalingControl) ShapePickSupport(edu.uci.ics.jung.visualization.ShapePickSupport) GraphMouse(edu.uci.ics.jung.visualization.VisualizationViewer.GraphMouse) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) EdgeStringer(edu.uci.ics.jung.graph.decorators.EdgeStringer) EdgeShape(edu.uci.ics.jung.graph.decorators.EdgeShape) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) ArchetypeVertex(edu.uci.ics.jung.graph.ArchetypeVertex) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) Color(java.awt.Color) GraphZoomScrollPane(edu.uci.ics.jung.visualization.GraphZoomScrollPane) Dimension(java.awt.Dimension) VertexPaintFunction(edu.uci.ics.jung.graph.decorators.VertexPaintFunction) Paint(java.awt.Paint) EdgePaintFunction(edu.uci.ics.jung.graph.decorators.EdgePaintFunction) Paint(java.awt.Paint) Graphics(java.awt.Graphics) Graph(edu.uci.ics.jung.graph.Graph) ActionListener(java.awt.event.ActionListener) VertexStringer(edu.uci.ics.jung.graph.decorators.VertexStringer) PluggableRenderer(edu.uci.ics.jung.visualization.PluggableRenderer) EdgeStrokeFunction(edu.uci.ics.jung.graph.decorators.EdgeStrokeFunction) DefaultToolTipFunction(edu.uci.ics.jung.graph.decorators.DefaultToolTipFunction) CrossoverScalingControl(edu.uci.ics.jung.visualization.control.CrossoverScalingControl) Edge(edu.uci.ics.jung.graph.Edge) ArchetypeEdge(edu.uci.ics.jung.graph.ArchetypeEdge) JMenuBar(javax.swing.JMenuBar)

Aggregations

ArchetypeEdge (edu.uci.ics.jung.graph.ArchetypeEdge)1 ArchetypeVertex (edu.uci.ics.jung.graph.ArchetypeVertex)1 Edge (edu.uci.ics.jung.graph.Edge)1 Graph (edu.uci.ics.jung.graph.Graph)1 Vertex (edu.uci.ics.jung.graph.Vertex)1 DefaultToolTipFunction (edu.uci.ics.jung.graph.decorators.DefaultToolTipFunction)1 EdgePaintFunction (edu.uci.ics.jung.graph.decorators.EdgePaintFunction)1 EdgeShape (edu.uci.ics.jung.graph.decorators.EdgeShape)1 EdgeStringer (edu.uci.ics.jung.graph.decorators.EdgeStringer)1 EdgeStrokeFunction (edu.uci.ics.jung.graph.decorators.EdgeStrokeFunction)1 VertexPaintFunction (edu.uci.ics.jung.graph.decorators.VertexPaintFunction)1 VertexStringer (edu.uci.ics.jung.graph.decorators.VertexStringer)1 FRLayout (edu.uci.ics.jung.visualization.FRLayout)1 GraphZoomScrollPane (edu.uci.ics.jung.visualization.GraphZoomScrollPane)1 PluggableRenderer (edu.uci.ics.jung.visualization.PluggableRenderer)1 ShapePickSupport (edu.uci.ics.jung.visualization.ShapePickSupport)1 VisualizationViewer (edu.uci.ics.jung.visualization.VisualizationViewer)1 GraphMouse (edu.uci.ics.jung.visualization.VisualizationViewer.GraphMouse)1 CrossoverScalingControl (edu.uci.ics.jung.visualization.control.CrossoverScalingControl)1 DefaultModalGraphMouse (edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse)1