Reputation: 21
Hello! I have a problem with my program.
I use JUNG libraries in java and when I compile the program I get these two errors:
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>
I do not know what these errors could mean against incompatible data types.
How can I solve the problem?
Thank you in advance!
The "GraphView" class:
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);
}
}
The "GraphBuilding" class:
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());
}
}
The "Main" class:
public class Main {
public static void main(String[] args) {
new GraphView();
}
}
Upvotes: 1
Views: 404
Reputation:
It looks like you are using version-2.1.1 (or 2.1) but your own code is based on an older version. Jung changed collections libraries to guava in 2.1
Here is your code, modified to work with 2.1.1: (if you use jung-2.1, i noticed a bug that will give you a NPE. Just change to 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);
}
}
Upvotes: 1