如何使用JUNG为绘图边设置动画 [英] How to animate drawing edges with JUNG

查看:149
本文介绍了如何使用JUNG为绘图边设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JUNG的新手
我有一个FRLayout代表一个网络拓扑,其中关键节点或顶点颜色为红色,其他顶点为蓝色
从起始节点到结束的边缘节点是蓝色的
我想要演示结束节点路径的动画。
如何使用指定的时间间隔将start_node的边缘绘制为end_node?您能提供或引用一个示例吗?

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. How can animate drawing the edges from a start_node to end_node with a specified time interval? Can you provide or reference an example?

推荐答案

您可以将关键帧附加到Edge数据。然后,每次绘制(使用变换器)时,您都可以使用关键帧调整边缘的渐变:

You could attach a keyframe to your Edge data. 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;
        }
}

编辑:

我写了一个简单的例子:

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

另外,我已经服用了之前有点热:这需要JUNG库。如果没有,则无法运行SSCCEE。

Also, I've taken a bit of heat for this before so: this requires the JUNG library. If you don't have it, you can't run the SSCCEE.

这篇关于如何使用JUNG为绘图边设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆