1

Jframeが自力で自由に動くプログラムを作りたいです。翻訳/トランジションのようなものです。

例えば、

  1. プログラムをクリックして開始します。

  2. Jframeは場所(0,0)でスポーンします。

  3. 新しい座標が(100,0)になるように、100ピクセルを自動的に右に移動(アニメート)します。

プログラムの実行後に初期位置を設定するsetLocation(x、y)メソッドがあることは知っていますが、プログラムの開始後にJframe全体を移動する方法はありますか?

4

1 に答える 1

2

基本的な考え方はこんな感じ…

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で使用できる多くのアニメーションエンジンの1つを調査する方がよいでしょう。これにより、現在のフレームに基づいてアニメーションの速度を変更できるようになります(たとえば、スローインやスローアウトなど)。

見てみます

「可変時間」ソリューションで更新

これは基本的に、可変時間アニメーションを実行する方法の例です。つまり、動きを固定するのではなく、時間を調整して、アニメーションがアニメーションの実行時間に基づいて動きの要件を計算できるようにすることができます...

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();

            }
        });
    }
}
于 2013-02-19T05:57:03.037 に答える