Exemplo n.º 1
0
 @Override
 public void stateChanged(ChangeEvent e) {
   int level = levelControl.getValue();
   if (level != current_level) {
     createTree(level);
     current_level = level;
     if (!radial.isSelected()) {
       TreeLayout<String, Number> tl2 = treeLayout;
       radialLayout = new BalloonLayout<String, Number>(graph);
       radialLayout.setSize(new Dimension(1200, 1200));
       rings = new Rings(radialLayout);
       treeLayout = new TreeLayout<String, Number>(graph);
       LayoutTransition<String, Number> lt =
           new LayoutTransition<String, Number>(vv, tl2, treeLayout);
       Animator animator = new Animator(lt);
       animator.start();
     } else {
       BalloonLayout<String, Number> r2 = radialLayout;
       radialLayout = new BalloonLayout<String, Number>(graph);
       radialLayout.setSize(new Dimension(1200, 1200));
       treeLayout = new TreeLayout<String, Number>(graph);
       vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
       vv.removePreRenderPaintable(rings);
       rings = new Rings(radialLayout);
       LayoutTransition<String, Number> lt =
           new LayoutTransition<String, Number>(vv, r2, radialLayout);
       Animator animator = new Animator(lt);
       animator.start();
       vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
       vv.addPreRenderPaintable(rings);
     }
     // vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
   }
 }
  /** create an instance of a simple graph with controls to demo the zoom features. */
  public GraphZoomScrollPaneDemo() {

    // create a simple graph for the demo
    graph = new DirectedSparseGraph();
    Vertex[] v = createVertices(10);
    createEdges(v);

    ImageIcon sandstoneIcon = null;
    String imageLocation = "/samples/images/Sandstone.jpg";
    try {
      sandstoneIcon = new ImageIcon(getClass().getResource(imageLocation));
    } catch (Exception ex) {
      System.err.println("Can't load \"" + imageLocation + "\"");
    }
    final ImageIcon icon = sandstoneIcon;
    PluggableRenderer pr = new PluggableRenderer();

    vv = new VisualizationViewer(new FRLayout(graph), pr);
    vv.setPickSupport(new ShapePickSupport());
    pr.setEdgeShapeFunction(new EdgeShape.QuadCurve());

    if (icon != null) {
      vv.addPreRenderPaintable(
          new VisualizationViewer.Paintable() {
            public void paint(Graphics g) {
              Dimension d = vv.getSize();
              g.drawImage(icon.getImage(), 0, 0, d.width, d.height, vv);
            }

            public boolean useTransform() {
              return false;
            }
          });
    }
    vv.addPostRenderPaintable(
        new VisualizationViewer.Paintable() {
          int x;
          int y;
          Font font;
          FontMetrics metrics;
          int swidth;
          int sheight;
          String str = "GraphZoomScrollPane Demo";

          public void paint(Graphics g) {
            Dimension d = vv.getSize();
            if (font == null) {
              font = new Font(g.getFont().getName(), Font.BOLD, 30);
              metrics = g.getFontMetrics(font);
              swidth = metrics.stringWidth(str);
              sheight = metrics.getMaxAscent() + metrics.getMaxDescent();
              x = (d.width - swidth) / 2;
              y = (int) (d.height - sheight * 1.5);
            }
            g.setFont(font);
            Color oldColor = g.getColor();
            g.setColor(Color.lightGray);
            g.drawString(str, x, y);
            g.setColor(oldColor);
          }

          public boolean useTransform() {
            return false;
          }
        });

    vv.addGraphMouseListener(new TestGraphMouseListener());

    // add my listener for ToolTips
    vv.setToolTipFunction(new DefaultToolTipFunction());

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphMouse graphMouse = new DefaultModalGraphMouse();
    vv.setGraphMouse(graphMouse);
    JMenuBar menu = new JMenuBar();
    menu.add(((DefaultModalGraphMouse) graphMouse).getModeMenu());
    panel.setCorner(menu);
    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
          }
        });
    JButton minus = new JButton("-");
    minus.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
          }
        });
    JButton reset = new JButton("reset");
    reset.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent e) {
            vv.getLayoutTransformer().setToIdentity();
            vv.getViewTransformer().setToIdentity();
          }
        });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(reset);
    content.add(controls, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
  }