我使用Jung绘制图形,输入采用.net格式(使用txt2pajek构建)。我想看到边缘和输出的权重。 请帮帮我怎么做? 感谢
代码和.net文件格式为:
package pGraph;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.commons.collections15.FactoryUtils;
import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.functors.MapTransformer;
import com.sun.xml.internal.ws.api.server.Container;
import pGraph.JungExample2.MyRenderer;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.io.PajekNetReader;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
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.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
/**
* A class that shows the minimal work necessary to load and visualize a graph.
*/
public class D1
{
public static void main(String[] args) throws IOException
{
JFrame jf = new JFrame();
PajekNetReader pnr = new PajekNetReader(FactoryUtils.instantiateFactory(Object.class));
Graph g = new DirectedSparseGraph();
VisualizationViewer vv = new VisualizationViewer(new FRLayout(g));
vv.getRenderContext().setVertexLabelTransformer(pnr.getVertexLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
pnr.load("F:\\1c\\qq.net", g);
DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
vv.setGraphMouse(gm);
System.out.println(g.toString());
final DefaultModalGraphMouse<String, Number> graphMouse = new DefaultModalGraphMouse<String, Number>();
graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
vv.setGraphMouse(graphMouse);
final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
jf.getContentPane().add(vv) ;
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
}
*顶点5
1“t”
2“b”
3“c”
4“d”
5“a”
*弧
1 2 0.2
3 4 0.9
5 4 0.86
答案 0 :(得分:0)
PajekNetReader.getEdgeWeightTransformer()
会为Pajek
文件提供边缘权重。
然后,您需要提供一个简单的Transformer,它可以将您的Transformer<E, Number>
转换为Transformer<E, String>
,并将其传递给vv.getRenderContext().setEdgeLabelTransformer()
。 (使用toStringLabeller
并没有多大意义,因为边缘对象本身并不是你想要的权重。)
http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/io/PajekNetReader.html