我们将如何去创造,从点移到点精灵的动画? [英] How would we go about creating an animation of a sprite that moves from point to point?

查看:224
本文介绍了我们将如何去创造,从点移到点精灵的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的drawImage(的paintComponent)等呈现的一切。我不知道我怎么会去具有A点到B精灵移动和实际显示的运动点(动画)。

I'm using drawImage (paintComponent) etc to render everything. I'm wondering how I would go about having a sprite move from point A to B and actually show the movement to that point (animation).

谢谢!

推荐答案

这完全取决于你所使用,在最基层的框架,您将需要某种触发它允许你定期更新的位置以某种三角洲精灵

This depends entirely on the framework you are using, at the very basic level, you will need some kind of trigger which allows you to periodically update the position of the sprite with some kind of delta

在Swing中,最简单的方法是使用一个 javax.swing.Timer中的,它允许你开火定期的事件,但在事件中被触发线程调度,减少了可能的污染线和肮脏的油漆

In Swing, the simplest approach would be to use a javax.swing.Timer which allows you to fire an event at regular intervals, but which are triggered within the Event Dispatching Thread, reducing possible thread contamination and dirty paints

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SimpleSprite {

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

    public SimpleSprite() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new AnimationPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class AnimationPane extends JPanel {

        private BufferedImage dalek;
        private Point dalekPoint;
        private int deltaX = 1;

        public AnimationPane() {
            dalekPoint = new Point();
            try {
                dalek = ImageIO.read(getClass().getResource("/dalek.png"));
                dalekPoint.y = (200 - dalek.getHeight()) / 2;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (dalekPoint.x + dalek.getWidth() > getWidth()) {
                        dalekPoint.x = getWidth() - dalek.getWidth();
                        deltaX *= -1;
                    } else if (dalekPoint.x < 0) {
                        dalekPoint.x = 0;
                        deltaX *= -1;
                    } else {
                        dalekPoint.x += deltaX;
                    }
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(dalek, dalekPoint.x, dalekPoint.y, this);
            g2d.dispose();
        }
    }

}

看看如何使用Swing计时器了解更多详情

这篇关于我们将如何去创造,从点移到点精灵的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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