Advertisement
Guest User

Untitled

a guest
May 30th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. public void pintarGraf(UndirectedSparseGraph<String, Double> g){
  2. SimpleGraphView graphView = new SimpleGraphView(g);
  3. graphView.mostraVista();
  4. }
  5.  
  6.  
  7.  
  8.  
  9. public void convertirGrafPerVisualitzar(){
  10. UndirectedSparseGraph<String, Double> g = new UndirectedSparseGraph<String,Double>();
  11. graf = ponderacions.getGraf();
  12. for(String s : graf.consultarNodes()) {
  13. g.addVertex(s);
  14. }
  15. for(String s : graf.consultarNodes()){
  16. Set<Map.Entry<String, Double>> a = graf.consultarArestesNode(s);
  17. Double i = 0.0;
  18. for(Entry<String, Double> s2 : a){
  19. g.addEdge(i, s,s2.getKey());
  20. ++i;
  21. }
  22. }
  23. ictrlPresentacio.pintarGraf(g);
  24. }
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. package asdf;
  32.  
  33. import edu.uci.ics.jung.algorithms.layout.CircleLayout;
  34. import edu.uci.ics.jung.algorithms.layout.FRLayout;
  35. import edu.uci.ics.jung.algorithms.layout.Layout;
  36. import edu.uci.ics.jung.algorithms.layout.TreeLayout;
  37. import edu.uci.ics.jung.graph.Graph;
  38. import edu.uci.ics.jung.graph.SparseMultigraph;
  39. import edu.uci.ics.jung.graph.UndirectedSparseGraph;
  40. import edu.uci.ics.jung.visualization.BasicVisualizationServer;
  41. import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
  42. import edu.uci.ics.jung.visualization.picking.PickedInfo;
  43.  
  44. import java.awt.Color;
  45. import java.awt.Dimension;
  46. import java.awt.Frame;
  47. import java.awt.GridLayout;
  48. import java.awt.Paint;
  49. import java.awt.Toolkit;
  50.  
  51. import javax.swing.JFrame;
  52.  
  53. import org.apache.commons.collections15.Transformer;
  54.  
  55. public class SimpleGraphView {
  56. Graph<String, Double> g;
  57. /** Creates a new instance of SimpleGraphView */
  58. public SimpleGraphView(UndirectedSparseGraph<String, Double> gr) {
  59. g = gr;
  60.  
  61. }
  62.  
  63. private static class VertexLabelTransformer implements Transformer<Integer,String>{
  64. private final PickedInfo<Integer> pi;
  65.  
  66. public VertexLabelTransformer( PickedInfo<Integer> pi ){
  67. this.pi = pi;
  68. }
  69.  
  70. @Override
  71. public String transform(Integer t) {
  72. return t.toString();
  73. }
  74. }
  75.  
  76. public void mostraVista() {
  77.  
  78. /*
  79. Transformer<String,Paint> vertexColor = new Transformer<String,Paint>() {
  80.  
  81. @Override
  82. public Paint transform(String i) {
  83. // TODO Auto-generated method stub
  84. if(i.equals("1")) return Color.GREEN;
  85. else if (i.equals("2"))return Color.RED;
  86. else return Color.CYAN;
  87.  
  88. }
  89. };
  90. */
  91.  
  92. Layout<String, Double> layout = new FRLayout(g);
  93. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  94. d.setSize(d.getWidth(), d.getHeight()-60);
  95. layout.setSize(d);
  96. BasicVisualizationServer<String,Double> vv = new BasicVisualizationServer<String, Double>(layout);
  97. vv.setPreferredSize(new Dimension(d));
  98. //vv.getRenderContext().setVertexFillPaintTransformer(vertexColor);
  99. vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
  100. JFrame frame = new JFrame("Simple Graph View");
  101. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  102.  
  103. frame.getContentPane().add(vv);
  104. frame.pack();
  105. frame.setVisible(true);
  106. frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement