0

I created graph with static Layout in Jung. I have a method for random movement on 2D which moves the vertex for some number of steps. I appreciate the power of JUNG in most of the operations in my project but I wanted to animate the movement of the vertex when random walk method is called. I need help with this regard. below is my graph code.

    final int NUM_WALKS = 41;
    int count = 0;
    while (count < NUM_WALKS) {
        sa.randomwalk();

        Graph<Integer, String> gr = wsn.generateRandomGraph();
        Transformer<Integer, Point2D> locationTransformer = new Transformer<Integer, Point2D>() {
            @Override
            public Point2D transform(Integer vertex) {
                int value = (vertex.intValue());// * 40) + 20;
                Map<Integer, Integer> MapX = new HashMap<Integer, Integer>();
                MapX = WirelesSensorNetwork.MapX_Object();
                Map<Integer, Integer> MapY = new HashMap<Integer, Integer>();
                MapY = WirelesSensorNetwork.MapY_Object();
                return new Point2D.Float(MapX.get(value), MapY.get(value));
            }
        };
        Dimension preferredSize = new Dimension(700, 600);
        StaticLayout<Integer, String> layout = new StaticLayout<Integer, String>(gr, locationTransformer);


        layout.setSize(new Dimension(300, 250));

        VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(layout,
                preferredSize);
        vv.setBackground(Color.WHITE);

        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
        vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<Integer, String>());

        JFrame frame = new JFrame("Wireless Sensor Network ");
        frame.getContentPane().setBackground(Color.WHITE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv);
        frame.pack();
        frame.setVisible(true);

    count++;
    }
sirbakura
  • 11
  • 2
  • What problem are you trying to solve by doing this random walking? Are the nodes connected to anything? – Joshua O'Madadhain Feb 08 '19 at 00:56
  • I have an algorithm which would compute bottleneck ratio after some time-steps of the random walk. Node are connected to theirs neighbors but did not carry any weight just like edge. The graph model mobile wireless sensor network. When I run my simulation, the nodes really move as I can observe from the traces of the move but can I visualize the move on Jung Graph – sirbakura Feb 08 '19 at 07:09

1 Answers1

0

There's a lot I don't know about your program, so I can't actually test this. You may find it useful to start with something like this:

    // set up variables (you must be declaring 'sa' somewhere up here....)
    final int NUM_WALKS = 41;
    int count = 0;
    // do first randomwalk
    sa.randomwalk();

    // create the graph, layout, and visualization
    Graph<Integer, String> gr = wsn.generateRandomGraph();
    // get locations for first layout
    Transformer<Integer, Point2D> locationTransformer = new Transformer<Integer, Point2D>() {
        @Override
        public Point2D transform(Integer vertex) {
            int value = (vertex.intValue());// * 40) + 20;
            Map<Integer, Integer> MapX = new HashMap<Integer, Integer>();
            MapX = WirelesSensorNetwork.MapX_Object();
            Map<Integer, Integer> MapY = new HashMap<Integer, Integer>();
            MapY = WirelesSensorNetwork.MapY_Object();
            return new Point2D.Float(MapX.get(value), MapY.get(value));
        }
    };
    // create the first StaticLayout
    StaticLayout<Integer, String> layout = new StaticLayout<Integer, String>(gr, locationTransformer);
    layout.setSize(new Dimension(300, 250));

    // create the visualization
    Dimension preferredSize = new Dimension(700, 600);
    VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(layout,
            preferredSize);
    vv.setBackground(Color.WHITE);

    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
    vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<Integer, String>());

    JFrame frame = new JFrame("Wireless Sensor Network ");
    frame.getContentPane().setBackground(Color.WHITE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(vv);
    frame.pack();
    frame.setVisible(true);
    // increment the count, since we have run the randomwalk one time
    count++;

    // do the rest of the randomwalks
    // you might need to do something to slow down this loop so that it does not
    // go too fast for the visualization to keep up with.
    while (count < NUM_WALKS) {
        sa.randomwalk();

        // make a new location transformer after each walk
        Transformer<Integer, Point2D> nextLocationTransformer = new Transformer<Integer, Point2D>() {
            @Override
            public Point2D transform(Integer vertex) {
                int value = (vertex.intValue());// * 40) + 20;
                Map<Integer, Integer> MapX = new HashMap<Integer, Integer>();
                MapX = WirelesSensorNetwork.MapX_Object();
                Map<Integer, Integer> MapY = new HashMap<Integer, Integer>();
                MapY = WirelesSensorNetwork.MapY_Object();
                return new Point2D.Float(MapX.get(value), MapY.get(value));
            }
        };
        // make a new layout with the new location transformer
        StaticLayout<Integer, String> newLayout = new StaticLayout<Integer, String>(gr, nextLocationTransformer);
        newLayout.setSize(new Dimension(300, 250));

        // animate the existing visualization to move from the previous layout to the new one
        LayoutTransition<Integer, String> lt =
                new LayoutTransition<Integer, String>(vv, vv.getGraphLayout(),
                        newLayout);
        Animator animator = new Animator(lt);
        animator.start();

        count++;
    }
  • Thanks it woks for me. I use thread try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } on the While loop and it works fine – sirbakura Feb 08 '19 at 17:55