简体   繁体   English

如何使用 JUNG 为绘图边缘设置动画

[英]How to animate drawing edges with JUNG

I'm new to JUNG I have a FRLayout that represents a network topology with key nodes or vertices that are color as red and the other vertices blue The edges from the starting node to the end node are blue I want to be to demonstrate an animation of the path to the end node.我是 JUNG 的新手 我有一个 FRLayout,它表示一个网络拓扑,其中关键节点或顶点的颜色为红色,其他顶点为蓝色 从起始节点到结束节点的边是蓝色的 我想演示一个动画到结束节点的路径。 How can animate drawing the edges from a start_node to end_node with a specified time interval?如何以指定的时间间隔动画绘制从 start_node 到 end_node 的边缘? Can you provide or reference an example?你能提供或参考一个例子吗?

You could attach a keyframe to your Edge data.您可以将关键帧附加到 Edge 数据。 Then, every time you draw (using a transformer) you can use the keyframe to adjust the gradient of the edge:然后,每次绘制(使用变压器)时,您都可以使用关键帧来调整边缘的渐变:

RenderContext<V, E> context = vv.getRenderContext();
context.setEdgeDrawPaintTransformer(new KeyframeGradientTransformer());

public class KeyframeGradientTransformer() implements Transformer<E, Paint> {
        @Override
        public Paint transform(Edge edge) {
            // TODO: Here you would determine the gradient information
            // based on the edge.getKeyframe().
            Paint gradient = new GradientPaint(...);
            return gradient;
        }
}

EDIT:编辑:

I wrote up a quick example:我写了一个简单的例子:

在此处输入图片说明

This animates from one vertex to another (along one edge).这从一个顶点到另一个(沿着一条边)动画。 If you want to animate going through multiple vertices, that will require more logic.如果您想通过多个顶点设置动画,则需要更多的逻辑。 However, this looks pretty cool and should give you a start.然而,这看起来很酷,应该给你一个开始。 If you (or anyone else) need more comments, just let me know and I can try and make it more clear.如果您(或其他任何人)需要更多评论,请告诉我,我可以尝试使其更清楚。

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.generators.random.EppsteinPowerLawGenerator;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.Pair;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class Test {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setPreferredSize(new Dimension(1024, 768));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel content = new JPanel();

                // Set up the graph and the display.
                int numV = 70;
                int numE = 50;
                EppsteinPowerLawGenerator<String, String> gen = new EppsteinPowerLawGenerator<String, String>(
                        new GraphFactory(), new CountFactory(),
                        new CountFactory(), numV, numE, 10);
                Graph<String, String> graph = gen.create();
                Layout<String, String> layout = new SpringLayout<String, String>(
                        graph);
                VisualizationViewer<String, String> vv = new VisualizationViewer<String, String>(
                        layout);
                vv.getRenderContext().setEdgeStrokeTransformer(
                        new Transformer<String, Stroke>() {

                            @Override
                            public Stroke transform(String edge) {
                                return new BasicStroke(1.5f);
                            }
                        });

                content.add(vv);

                frame.setContentPane(content);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                // Animate the edges!
                AnimationTimerTask at = new AnimationTimerTask(vv);
                Timer timer = new Timer();
                timer.scheduleAtFixedRate(at, 10, 30);
            }

        });
    }

    static class AnimationTimerTask extends TimerTask {

        private final double width = 0.1; // Size of the colored line.
        private final double stepsize = 0.01;
        private double keyframe = 0 + width; // Between 0.0 and 1.0
        private VisualizationViewer<String, String> vv = null;

        public AnimationTimerTask(VisualizationViewer<String, String> vv) {
            this.vv = vv;
        }

        @Override
        public void run() {
            vv.getRenderContext().setEdgeDrawPaintTransformer(
                    new Transformer<String, Paint>() {

                        @Override
                        public Paint transform(String edge) {
                            // Find both points of the edge.
                            Pair<String> vs = vv.getGraphLayout().getGraph()
                                    .getEndpoints(edge);
                            Point2D p1 = vv.getGraphLayout().transform(
                                    vs.getFirst());
                            Point2D p2 = vv.getGraphLayout().transform(
                                    vs.getSecond());

                            // This code won't handle self-edges.
                            if (p1.equals(p2)) {
                                return Color.red;
                            }

                            Color[] colors = { Color.gray, Color.red,
                                    Color.gray };
                            float start = (float) Math.max(0.0, keyframe
                                    - width);
                            float end = (float) Math.min(1.0, keyframe + width);
                            float[] fractions = { start, (float) keyframe, end };
                            return new LinearGradientPaint(p1, p2, fractions,
                                    colors);
                        }

                    });
            vv.repaint();
            keyframe += stepsize;
            keyframe %= 1.0;
        }
    }

    static class GraphFactory implements Factory<Graph<String, String>> {

        @Override
        public Graph<String, String> create() {
            return new SparseMultigraph<String, String>();
        }
    }

    static class CountFactory implements Factory<String> {

        private int count = 0;

        @Override
        public String create() {
            return String.valueOf(count++);
        }
    }
}

Also, I've taken a bit of heat for this before so: this requires the JUNG library.另外,我之前为此付出了一些努力:这需要 JUNG 库。 If you don't have it, you can't run the SSCCEE.如果没有它,则无法运行 SSCCEE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM