Start JUNG application with an Empty plot or visualisation

Doktor_DOOM

Mitglied
Hi,
i will start my JUNG application without a plotted Graph. If i type in a node, for example "A0" and the node exist then should JUNG plot the graph. I tried the following function.

{panel.remove((Component)vv);}

But this removed the complete visualization component. My code the follwing. I extended an JUNG example.

Java:
/*
 * Copyright (c) 2003, The JUNG Authors
 * All rights reserved.
 *
 * This software is open-source under the BSD license; see either "license.txt"
 * or https://github.com/tomnelson/jungrapht-visualization/blob/master/LICENSE for a description.
 *
 */
package org.jungrapht.samples.tree;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultGraphType;
import org.jgrapht.graph.builder.GraphTypeBuilder;
import org.jungrapht.samples.util.GraphLayoutSelector;

import org.jungrapht.samples.util.GraphControlHelpers;
import org.jungrapht.samples.util.GraphHandler;
import org.jungrapht.visualization.VisualizationScrollPane;
import org.jungrapht.visualization.VisualizationViewer;
import org.jungrapht.visualization.control.DefaultGraphMouse;
import org.jungrapht.visualization.layout.algorithms.StaticLayoutAlgorithm;
import org.jungrapht.visualization.renderers.Renderer;
import org.jungrapht.visualization.selection.SelectedState;
import org.jungrapht.visualization.spatial.rtree.RStarLeafSplitter;
import org.jungrapht.visualization.spatial.rtree.RStarSplitter;
import org.jungrapht.visualization.spatial.rtree.RTree;
import org.jungrapht.visualization.spatial.rtree.SplitterContext;
import org.jungrapht.visualization.util.LayoutPaintable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.Predicate;



/**
 * Demonstrates the visualization of a Forest using several layout algorithms. An examiner lens
 * performing a hyperbolic transformation of the view is also included.
 *
 * @author Tom Nelson
 */
public class WebSearchInterface extends JPanel implements ActionListener {

    SplitterContext<Object> splitterContext =
            SplitterContext.of(new RStarLeafSplitter<>(), new RStarSplitter<>());

  final DefaultGraphMouse<String, Integer> graphMouse = new DefaultGraphMouse<>();

    VisualizationScrollPane panel;

    @Override
    public void actionPerformed(ActionEvent e) {
      //if(e.getSource() == GraphLayoutSelector.InputtextF)
      //{
        VisualizationViewer<String, Integer> vv;
        //GraphLayoutSelector.InputtextF.getText();
        if(panel != null)remove(panel);  // panel global setzen zum löschen

        vv =
                VisualizationViewer.builder(graph)
                        .layoutAlgorithm(new StaticLayoutAlgorithm<>())
                        .layoutSize(layoutSize)
                        .viewSize(viewSize)
                        .graphMouse(graphMouse)
                        .build();

        vv.setVertexToolTipFunction(Object::toString);

        vv.addPreRenderPaintable(
                new LayoutPaintable.LayoutBounds(
                        vv.getVisualizationModel(), vv.getRenderContext().getMultiLayerTransformer()));

        vv.getSelectedVertexState()
                .addItemListener(new SelectedState.StateChangeListener<>(this::selected, this::deselected));


        vv.getRenderContext().setVertexLabelFunction(Object::toString);
        vv.getRenderContext().setVertexLabelPosition(Renderer.VertexLabel.Position.CNTR);


        Box controls = Box.createHorizontalBox();
        controls.add(
                GraphControlHelpers.getCenteredContainer(
                        "Layout Controls", GraphLayoutSelector.builder(vv).initialSelection(1).build()));
        controls.add(GraphControlHelpers.getCenteredContainer("Zoom",GraphControlHelpers.getZoom(vv)));
        add(controls, BorderLayout.SOUTH);

        //panel = new panel
        panel = new VisualizationScrollPane(vv);
       // panel.a
        add(panel);


      //}
  }

  static Predicate<Integer> edgePredicate = e -> e < 100;


  private static final Logger log = LoggerFactory.getLogger(WebSearchInterface.class);

  Graph<String, Integer> graph;



  /** the visual component and renderer for the graph */

  Dimension layoutSize = new Dimension(700, 700);
  Dimension viewSize = new Dimension(700, 700);

  public WebSearchInterface() {
    setLayout(new BorderLayout());
    // create a simple graph for the demo
    VisualizationViewer<String, Integer> vv;
    Graph<String, Integer> g =
            GraphTypeBuilder.<String, Integer>forGraphType(DefaultGraphType.directedMultigraph())
                    .buildGraph();

    graph = GraphHandler.createForest();


    GraphLayoutSelector.InputtextF.addActionListener(this); //   addDocumentListener(new DocumentListener() {})       addActionListener(this) == "A0")
      actionPerformed(null);

  }



  private void selected(Object o) {
    log.info("selected was {}", o);
  }

  private void deselected(Object o) {
    log.info("deselected: {}", o);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    content.add(new WebSearchInterface());
    frame.pack();
    frame.setVisible(true);
  }
}
 
The AddRemoveVertexDemo starts with an empty graph and adds vertices and edges, then it removes them. It may help.
If you post a complete program (including your GraphLayoutSelector) I may be able to help get it working.
 

Neue Themen


Zurück
Oben