对荣格上的顶点进行随机运动的动画

时间:2019-02-07 14:51:29

标签: jung jung2

我在Jung中使用静态布局创建了图形。我有一种在2D上随机移动的方法,该方法可以将顶点移动一些步骤。我很欣赏JUNG在项目中大多数操作中的强大功能,但是当调用随机游走方法时,我想为顶点的移动设置动画。我在这方面需要帮助。下面是我的图形代码。

    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++;
    }

1 个答案:

答案 0 :(得分:0)

我对您的程序不了解很多,因此我无法进行实际测试。 您可能会发现从这样的事情开始很有用:

    // 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++;
    }