我熟悉工具包,只是尝试将一些简单的图形导出到png。我有点关注我在此处找到的幻灯片共享教程:http://www.slideshare.net/gephi/gephi-toolkit-tutorialtoolkit
查看结果时,我得到的只是一张空白图片。有人知道我哪里出错吗?我使用下面的代码:
//Init a project - and therefore a workspace
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
//Get a graph model - it exists because we have a workspace
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
//Create three nodes
Node n0 = graphModel.factory().newNode("n0");
n0.setLabel("Node 0");
Node n1 = graphModel.factory().newNode("n1");
n1.setLabel("Node 1");
Node n2 = graphModel.factory().newNode("n2");
n2.setLabel("Node 2");
//Create three edges
Edge e1 = graphModel.factory().newEdge(n1, n2, 1, true);
Edge e2 = graphModel.factory().newEdge(n0, n2, 2, true);
Edge e3 = graphModel.factory().newEdge(n2, n0, 2, true); //This is e2's mutual edge
//Append as a Directed Graph
DirectedGraph directedGraph = graphModel.getDirectedGraph();
directedGraph.addNode(n0);
directedGraph.addNode(n1);
directedGraph.addNode(n2);
directedGraph.addEdge(e1);
directedGraph.addEdge(e2);
directedGraph.addEdge(e3);
//Count nodes and edges
System.out.println("Nodes: " + directedGraph.getNodeCount() + " Edges: " + directedGraph.getEdgeCount());
//Get a UndirectedGraph now and count edges
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
//Layout for 1 minute
AutoLayout autoLayout = new AutoLayout(10, TimeUnit.SECONDS);
autoLayout.setGraphModel(graphModel);
YifanHuLayout firstLayout = new YifanHuLayout(null, new StepDisplacement(1f));
ForceAtlasLayout secondLayout = new ForceAtlasLayout(null);
AutoLayout.DynamicProperty adjustBySizeProperty = AutoLayout.createDynamicProperty("forceAtlas.adjustSizes.name", Boolean.TRUE, 0.1f);//True after 10% of layout time
AutoLayout.DynamicProperty repulsionProperty = AutoLayout.createDynamicProperty("forceAtlas.repulsionStrength.name", new Double(500.), 0f);//500 for the complete period
autoLayout.addLayout(firstLayout, 0.5f);
autoLayout.addLayout(secondLayout, 0.5f, new AutoLayout.DynamicProperty[]{adjustBySizeProperty,
repulsionProperty});
autoLayout.execute();
//Preview
PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel();
model.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
model.getProperties().putValue(PreviewProperty.EDGE_COLOR, new EdgeColor(Color.GRAY));
model.getProperties().putValue(PreviewProperty.EDGE_THICKNESS, new Float(0.1f));
model.getProperties().putValue(PreviewProperty.NODE_LABEL_FONT, model.getProperties().
getFontValue(PreviewProperty.NODE_LABEL_FONT).deriveFont(8));
//Export full graph, save it to test.png and show it in an image frame
ExportController ec = Lookup.getDefault().lookup(ExportController.class);
ec.exportFile(new File("test.png"));
PNGExporter exporter = (PNGExporter) ec.getExporter("png");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ec.exportStream(baos, exporter);
byte[] png = baos.toByteArray();
Image image = ImageIO.read(new ByteArrayInputStream(png));
ImageFrame frame = new ImageFrame(image);
frame.setVisible(true);