Jung coloring vertex with value

强颜欢笑 提交于 2019-12-05 03:24:08

I can see two possible problems; you are calling setVertexFillPaintTransformer twice and BasicVisualizationServer dosn't seem to like DefaultModalGraphMouse.

Try this version:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.functors.ConstantTransformer;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.picking.PickedInfo;
import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class Design {
     public Graph<Integer, String> g;
    public Design() {     

         this.g = ProjectTools.readNet("SmallTown.net");
    }

    private static class VertexPaintTransformer implements Transformer<Integer,Paint> {

        private final PickedInfo<Integer> pi;

        VertexPaintTransformer ( PickedInfo<Integer> pi ) { 
            super();
            if (pi == null)
                throw new IllegalArgumentException("PickedInfo instance must be non-null");
            this.pi = pi;
        }

        @Override
        public Paint transform(Integer i) {
            Color p = null;
            //Edit here to set the colours as reqired by your solution
            if ( i % 2 == 0)
                p = Color.GREEN;
            else
                p =  Color.RED;
            //Remove if a selected colour is not required
            if ( pi.isPicked(i)){
                p = Color.yellow;
            }
            return p;
        }
    }

    public static <Paint> void main(String[] args) {
        Design sgv = new Design(); 
        Layout<Integer, String> layout = new FRLayout(sgv.g);
        layout.setSize(new Dimension(800,800));  
        VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(layout);

        Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() {

            @Override
            public Paint transform(Integer i) {
                if ( i % 2 == 0)
                    return (Paint) Color.GREEN;
                else
                    return  (Paint) Color.RED;
            }
        };  

        vv.setPreferredSize(new Dimension(850,850));
        vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.green));
        vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.white));
        vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2.5f)));

        vv.getRenderContext().setVertexFillPaintTransformer(new VertexPaintTransformer(vv.getPickedVertexState()));

        DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
        graphMouse.setMode(edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode.PICKING);
        vv.setGraphMouse(graphMouse);

        vv.setBackground(Color.gray);
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Integer>());
        vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);


        JFrame frame = new JFrame("Projet Algo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv); 
        frame.pack();
        frame.setVisible(true);       
    }
}

I've repalced your Transformer with a new version VertexPaintTransformer that supports selection, removed the second use of setVertexFillPaintTransformer and swithced you from a BasicVisualizationServer to VisualizationViewer so I can use a picking GraphMouse

I had to mock your ProjectTools#readNet() method but Ihope you can see the Odd, Even and Selected colours.

You can now modify VertexPaintTransformer#transform() as required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!