gpt4 book ai didi

Java JUNG - 不兼容的类型

转载 作者:行者123 更新时间:2023-11-30 05:53:09 26 4
gpt4 key购买 nike


您好!我的程序有问题。
我在 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 中将集合库更改为 Guava

这是您的代码,经过修改以适用于 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);
}
}

关于Java JUNG - 不兼容的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com