简体   繁体   English

Java Swing中的动画行

[英]Animated lines in Java Swing

I am currently writing code for creating a monitoring tool which monitors the Amazon Web Services and Amazon Cloud, and visualize my currently running cloud infrastructure. 我目前正在编写代码,以创建用于监视Amazon Web Services和Amazon Cloud并可视化当前正在运行的云基础架构的监视工具。

For example , if the CPU Utilization or the Network I/O increases some threshold value than auto scaling is called and a new instance of EC2 is added to the system, this code is running perfectly but now I am visualizing this thing on the GUI of the tool which shows this activity. 例如,如果CPU利用率或网络I / O增加了一些阈值,而不是调用了自动缩放,并且将新的EC2实例添加到系统中,则此代码可以正常运行,但是现在我可以在显示此活动的工具。 I am using Java Swing to create the monitoring tool. 我正在使用Java Swing创建监视工具。

The main help I needed is as following: 我需要的主要帮助如下:

I am visualizing client on the top of the frame and EC2 instances below the client and just to show logical connectivity between client and server I am drawing lines between client and EC2 instances. 我在框架顶部可视化客户端,在客户端下方可视化EC2实例,只是为了显示客户端和服务器之间的逻辑连接,我正在绘制客户端和EC2实例之间的界线。

Now I need these line to be animated - maybe a stroked line moving slowly just to show that there is some traffic between client and EC2 instances also I want that the speed of the animation or stroke movement in the line increases as the traffic increases.(may be a variable to set speed of animation). 现在,我需要对这些线进行动画处理-可能是一条描边线移动缓慢,只是为了表明客户端和EC2实例之间存在流量,我还希望动画或笔画移动的速度随着流量的增加而增加。(可以是设置动画速度的变量)。

I need help on how I could implement such animated lines in Java Swings. 我需要有关如何在Java Swings中实现此类动画行的帮助。 Any help is highly appreciated. 非常感谢您的帮助。

Here is a small example of moving dashed line fully painted and animated from zero: 这是一个从零开始完全绘制和动画的虚线移动的小示例:

private static int speed = 5;

public static void main ( String[] args )
{
    JFrame frame = new JFrame ();
    frame.setLayout ( new BorderLayout () );

    frame.add ( new JComponent ()
    {
        private int diff = 0;

        {
            final Timer timer = new Timer ( 1000 / ( 10 * speed ), null );
            timer.addActionListener ( new ActionListener ()
            {
                public void actionPerformed ( ActionEvent e )
                {
                    if ( diff < 20 )
                    {
                        diff++;
                    }
                    else
                    {
                        diff = 0;
                    }
                    repaint ();
                    timer.setDelay ( 1000 / ( 10 * speed ) );
                }
            } );
            timer.start ();
        }

        protected void paintComponent ( Graphics g )
        {
            Graphics2D g2d = ( Graphics2D ) g;
            g2d.setRenderingHint ( RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON );
            g2d.setStroke (
                    new BasicStroke ( 5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1f,
                            new float[]{ 10f, 10f }, diff ) );
            g2d.setPaint ( Color.BLACK );
            g2d.drawLine ( 0, getHeight () / 2, getWidth (), getHeight () / 2 );
        }
    } );
    frame.add ( new JSlider ( JSlider.HORIZONTAL, 1, 10, speed )
    {
        {
            addChangeListener ( new ChangeListener ()
            {
                public void stateChanged ( ChangeEvent e )
                {
                    speed = getValue ();
                }
            } );
        }
    }, BorderLayout.SOUTH );
    frame.setSize ( 500, 500 );
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

Basically for animation you will need to paint most of the things you want to animate. 基本上,对于动画,您将需要绘制要动画的大部分东西。 In your case if it is two objects connected with a dashed line - you could use container that paints that connection and just simple Swing components placed on it. 在您的情况下,如果是两个用虚线连接的对象-您可以使用绘制该连接的容器以及放置在其上的简单Swing组件。 So it will detect their bounds and draw connecting lines... 因此它将检测其边界并绘制连接线...

Animate a BasicStroke . BasicStroke动画处理。 See this answer for example code. 有关示例代码,请参见此答案

Use JPanel and override its paintComponent() method.This method will be responsible for animation. 使用JPanel并覆盖其paintComponent()方法。该方法将负责动画处理。

public void paintComponent(Graphics g)
 {
 super.paintComponent(g);
 g.setColor(Color.red); 
 g.drawLine(x1,y1,x2, y2); 
}

Increment the x2 again when required.In your case, need to do increment x2 when new connections are handled by the server. 根据需要再次增加x2。在您的情况下,当服务器处理新连接时,需要增加x2。 Hope this will help you. 希望这会帮助你。

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

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