有没有办法在Java中为整个Jframe设置动画以使其移动? [英] Is there a way to animate an entire Jframe in Java so that it moves?

查看:243
本文介绍了有没有办法在Java中为整个Jframe设置动画以使其移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,让Jframe能够自行自由移动。

I would like to create a program where the Jframe is able to move freely on it's own. Kind of like a translation / transition.

例如,


  1. 单击程序开始。

  1. Click on program to begin.

Jframe在位置(0,0)生成。

Jframe spawns at location (0,0).

自动向右移动(动画)100个像素,以使新坐标为(100,0)。

Automatically move (animate) 100 pixels to the right so that the new coordinates are (100,0).

我知道有setLocation(x,y)方法可以在程序运行后设置初始位置,但是有没有办法在程序启动后移动整个Jframe?

I know there's the setLocation(x,y) method that sets the initial position once the program runs but is there a way to move the entire Jframe after the program starts?

推荐答案

基本概念如下:

public class MoveMe01 {

    public static void main(String[] args) {
        new MoveMe01();
    }

    public MoveMe01() {

        EventQueue.invokeLater(
                new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JLabel("Use the Force Luke"));
                frame.pack();
                frame.setLocation(0, 0);
                frame.setVisible(true);

                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Point location = frame.getLocation();
                        Point to = new Point(location);
                        if (to.x < 100) {
                            to.x += 4;
                            if (to.x > 100) {
                                to.x = 100;
                            }
                        }
                        if (to.y < 100) {
                            to.y += 4;
                            if (to.y > 100) {
                                to.y = 100;
                            }
                        }

                        frame.setLocation(to);

                        if (to.equals(location)) {
                            ((Timer)e.getSource()).stop();
                        }
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();

            }
        });
    }
}

这是一个非常笔直的线性动画。您最好研究可用于Swing的众多动画引擎之一,这将使您能够根据当前帧更改动画速度(例如,执行慢入和慢出操作)

This is a pretty straight, linear animation. You'd be better of investigating one of the many animation engines available for Swing, this will provide you with the ability to change the speed of the animation based on the current frame (doing things like slow-in and slow-out for example)

我来看看

  • Timing Framework
  • Trident
  • Universla Tween Engine

已更新为变量时间解决方案

这基本上是一个如何制作可变时间动画的示例。也就是说,除了固定运动之外,您还可以调整时间并允许动画根据动画的运行时间来计算运动要求。

This is basically an example of how you might do a variable time animation. That is, rather then having a fixed movement, you can adjust the time and allow the animation to calculate the movement requirements based on the run time of the animation...

public class MoveMe01 {

    public static void main(String[] args) {
        new MoveMe01();
    }

    // How long the animation should run for in milliseconds
    private int runTime = 500;
    // The start time of the animation...
    private long startTime = -1;

    public MoveMe01() {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JLabel("Use the Force Luke"));
                frame.pack();
                frame.setLocation(0, 0);
                frame.setVisible(true);

                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        if (startTime < 0) {
                            // Start time of the animation...
                            startTime = System.currentTimeMillis();
                        }
                        // The current time
                        long now = System.currentTimeMillis();
                        // The difference in time
                        long dif = now - startTime;
                        // If we've moved beyond the run time, stop the animation
                        if (dif > runTime) {
                            dif = runTime;
                            ((Timer)e.getSource()).stop();
                        }
                        // The percentage of time we've been playing...
                        double progress = (double)dif / (double)runTime;

                        Point location = frame.getLocation();
                        Point to = new Point(location);

                        // Calculate the position as perctange over time...
                        to.x = (int)Math.round(100 * progress);
                        to.y = (int)Math.round(100 * progress);
                        // nb - if the start position wasn't 0x0, then you would need to
                        // add these to the x/y position above...

                        System.out.println(to);

                        frame.setLocation(to);
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();

            }
        });
    }
}

这篇关于有没有办法在Java中为整个Jframe设置动画以使其移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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