Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void pintarGraf(UndirectedSparseGraph<String, Double> g){
- SimpleGraphView graphView = new SimpleGraphView(g);
- graphView.mostraVista();
- }
- public void convertirGrafPerVisualitzar(){
- UndirectedSparseGraph<String, Double> g = new UndirectedSparseGraph<String,Double>();
- graf = ponderacions.getGraf();
- for(String s : graf.consultarNodes()) {
- g.addVertex(s);
- }
- for(String s : graf.consultarNodes()){
- Set<Map.Entry<String, Double>> a = graf.consultarArestesNode(s);
- Double i = 0.0;
- for(Entry<String, Double> s2 : a){
- g.addEdge(i, s,s2.getKey());
- ++i;
- }
- }
- ictrlPresentacio.pintarGraf(g);
- }
- package asdf;
- import edu.uci.ics.jung.algorithms.layout.CircleLayout;
- import edu.uci.ics.jung.algorithms.layout.FRLayout;
- import edu.uci.ics.jung.algorithms.layout.Layout;
- import edu.uci.ics.jung.algorithms.layout.TreeLayout;
- import edu.uci.ics.jung.graph.Graph;
- import edu.uci.ics.jung.graph.SparseMultigraph;
- import edu.uci.ics.jung.graph.UndirectedSparseGraph;
- import edu.uci.ics.jung.visualization.BasicVisualizationServer;
- import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
- import edu.uci.ics.jung.visualization.picking.PickedInfo;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.GridLayout;
- import java.awt.Paint;
- import java.awt.Toolkit;
- import javax.swing.JFrame;
- import org.apache.commons.collections15.Transformer;
- public class SimpleGraphView {
- Graph<String, Double> g;
- /** Creates a new instance of SimpleGraphView */
- public SimpleGraphView(UndirectedSparseGraph<String, Double> gr) {
- g = gr;
- }
- private static class VertexLabelTransformer implements Transformer<Integer,String>{
- private final PickedInfo<Integer> pi;
- public VertexLabelTransformer( PickedInfo<Integer> pi ){
- this.pi = pi;
- }
- @Override
- public String transform(Integer t) {
- return t.toString();
- }
- }
- public void mostraVista() {
- /*
- Transformer<String,Paint> vertexColor = new Transformer<String,Paint>() {
- @Override
- public Paint transform(String i) {
- // TODO Auto-generated method stub
- if(i.equals("1")) return Color.GREEN;
- else if (i.equals("2"))return Color.RED;
- else return Color.CYAN;
- }
- };
- */
- Layout<String, Double> layout = new FRLayout(g);
- Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
- d.setSize(d.getWidth(), d.getHeight()-60);
- layout.setSize(d);
- BasicVisualizationServer<String,Double> vv = new BasicVisualizationServer<String, Double>(layout);
- vv.setPreferredSize(new Dimension(d));
- //vv.getRenderContext().setVertexFillPaintTransformer(vertexColor);
- vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
- JFrame frame = new JFrame("Simple Graph View");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().add(vv);
- frame.pack();
- frame.setVisible(true);
- frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement