你好!我的程序有问题。
我在Java中使用JUNG库,并且在编译程序时遇到以下两个错误:

Error:(43, 61) java: incompatible types: org.apache.commons.collections15.Transformer<java.lang.Integer,java.awt.Paint> cannot be converted to com.google.common.base.Function<? super java.lang.Integer,java.awt.Paint>

Error:(44, 56) java: incompatible types: org.apache.commons.collections15.Transformer<java.lang.String,java.awt.Stroke> cannot be converted to com.google.common.base.Function<? super java.lang.String,java.awt.Stroke>


我不知道这些错误对不兼容的数据类型可能意味着什么。
我该如何解决这个问题?
先感谢您!

“ GraphView”类:

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import org.apache.commons.collections15.Transformer;
import javax.swing.*;
import java.awt.*;


public class GraphView {

    public GraphView() {

        GraphBuilding gb = new GraphBuilding(); // This builds the graph

        // Layout<V, E>, BasicVisualizationServer<V,E>
        Layout<Integer, String> layout = new CircleLayout(gb.g);

        layout.setSize(new Dimension(300, 300));
        BasicVisualizationServer<Integer, String> vv =
                new BasicVisualizationServer<Integer, String>(layout);
        vv.setPreferredSize(new Dimension(350, 350));

        // Setup up a new vertex to paint transformer...
        Transformer<Integer, Paint> vertexPaint = new Transformer<Integer, Paint>() {
            public Paint transform(Integer i) {
                return Color.GREEN;
            }
        };

        // Set up a new stroke Transformer for the edges
        float dash[] = {10.0f};
        final Stroke edgeStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
        Transformer<String, Stroke> edgeStrokeTransformer =
                new Transformer<String, Stroke>() {
                    public Stroke transform(String s) {
                        return edgeStroke;
                    }
                };

        vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
        vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
        vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);

        JFrame frame = new JFrame("Custom Graph View");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv);
        frame.pack();
        frame.setVisible(true);
    }

}


“ GraphBuilding”类:

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseGraph;
import edu.uci.ics.jung.graph.util.EdgeType;


public class GraphBuilding {
    // Graph<V, E> where V is the type of the vertices
    // and E is the type of the edges
    public Graph<String, String> g = new SparseGraph<String, String>();

    public GraphBuilding() {

        // Add some vertices. From above we defined these to be type Integer.
        g.addVertex("A");
        g.addVertex("B");
        g.addVertex("C");

        // Add some edges. From above we defined these to be of type String
        // Note that the default is for undirected edges.
        g.addEdge("Edge-1", "A", "B", EdgeType.DIRECTED); // Note that Java 1.5 auto-boxes primitives
        g.addEdge("Edge-2", "B", "C", EdgeType.DIRECTED);

        // Let's see what we have. Note the nice output from the
        // SparseMultigraph<V,E> toString() method
        System.out.println("The graph g = " + g.toString());

    }
}


“主”类:

public class Main {

    public static void main(String[] args) {

        new GraphView();
    }
}

最佳答案

看起来您正在使用版本2.1.1(或2.1),但是您自己的代码基于旧版本。 Jung在2.1版中将集合库更改为番石榴

这是修改为可与2.1.1一起使用的代码:(如果您使用jung-2.1,我注意到一个会为您提供NPE的错误。只需更改为2.1.1)

import com.google.common.base.Function;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import javax.swing.*;
import java.awt.*;


public class GraphView {

     public GraphView() {

        GraphBuilding gb = new GraphBuilding(); // This builds the graph

        // Layout<V, E>, BasicVisualizationServer<V,E>
        Layout<String, String> layout = new CircleLayout(gb.g);

        layout.setSize(new Dimension(300, 300));
        BasicVisualizationServer<String, String> vv =
            new BasicVisualizationServer<String, String>(layout);
        vv.setPreferredSize(new Dimension(350, 350));

        // Setup up a new vertex to paint transformer...
        Function<String, Paint> vertexPaint = new Function<String, Paint>() {
                public Paint apply(String i) {
                return Color.GREEN;
            }
        };

        // Set up a new stroke Transformer for the edges
        float dash[] = {10.0f};
        final Stroke edgeStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
        Function<String, Stroke> edgeStrokeTransformer =
            new Function<String, Stroke>() {
                public Stroke apply(String s) {
                    return edgeStroke;
                }
            };

        vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
        vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
        vv.getRenderer().getVertexLabelRenderer()
             .setPosition(Renderer.VertexLabel.Position.CNTR);

        JFrame frame = new JFrame("Custom Graph View");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv);
        frame.pack();
        frame.setVisible(true);
    }
}

10-07 13:59