请,需要帮助。我试图用jung可视化一个图形,但顶点不断移动并随机改变颜色。我想要实现的是显示一些具有特定颜色的顶点(例如具有以颜色区分的簇的顶点),然后继续可视化两个感兴趣的顶点之间的路径。顶点的数量超过 500。我是 jung 的新手,为了完成这个任务,我试图修改我从这个论坛上的一个线程得到的代码。我正在使用的代码如下所示
import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.SparseGraph;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.Random;
import javax.swing.JFrame;
import org.apache.commons.collections15.Transformer;
public class graphMainHelp {
public static void main(String[] args) {
SparseGraph graph = new SparseGraph();
String[] vertices = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
for (int i = 0; i < vertices.length; i++) {
graph.addVertex((String) vertices[i]);
}
graph.addEdge(1, "J", "A");
graph.addEdge(2, "A", "C");
graph.addEdge(3, "B", "A");
graph.addEdge(4, "B", "J");
graph.addEdge(5, "I", "E");
graph.addEdge(6, "B", "F");
graph.addEdge(7, "C", "A");
graph.addEdge(8, "G", "E");
graph.addEdge(9, "G", "F");
graph.addEdge(10, "H", "B");
graph.addEdge(11, "G", "C");
graph.addEdge(12, "E", "G");
graph.addEdge(13, "I", "B");
graph.addEdge(14, "F", "J");
graph.addEdge(15, "F", "E");
if (graph.getVertexCount() != 0 || graph.getEdgeCount() != 0) {
Layout<String, String> layout = new ISOMLayout<String, String>(graph);
layout.setSize(new Dimension(700, 700));
VisualizationViewer<String, String> vv = new VisualizationViewer<String, String>(layout);
vv.getRenderContext().setVertexLabelTransformer(new Transformer<String, String>() {
@Override
public String transform(String arg0) {
return arg0;
}
});
vv.getRenderer().setVertexRenderer(new MyRenderer());
final DefaultModalGraphMouse<String, Number> graphMouse = new
DefaultModalGraphMouse<String, Number>();
graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
vv.setGraphMouse(graphMouse);
JFrame frame = new JFrame();
frame.getContentPane().add(vv);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}// close main method
}
static class MyRenderer implements Renderer.Vertex<String, String> {
@Override
public void paintVertex(RenderContext<String, String> rc, Layout<String, String> layout, String vertex) {
GraphicsDecorator graphicsContext = rc.getGraphicsContext();
Point2D center = layout.transform(vertex);
Shape shape = null;
Color color = null;
shape = new Ellipse2D.Double(center.getX() - 10, center.getY() - 10, 20, 20);
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(200);
int b = random.nextInt(250);
color = new Color(r, g, b);
graphicsContext.setPaint(color);
graphicsContext.fill(shape);
}
}//close static class
}