如何从扩展线程的类在 JPanel 中启动动画(将圆从一个点移动到另一个点)?

我有一个名为 Player 的类,它扩展了线程,它的参数是一个JPanel(称为DrawPanel)和坐标 x,y;在Player构造函数中,我在面板上的 x、y 位置画了一个圆圈(我不知道它有多正确)。

Player运行函数中,我想启动一个动画,将一个小的红色圆圈从玩家坐标移动到另一个点。

就像在这个动画中一样。

我怎样才能做到这一点?

public class Player extends Thread {

 private  Graphics graphic;

 private Graphics2D g2;

 private int x;

 private int y;

 private DrawPanel panel;

    public Player(int x, int y,DrawPanel panel) 

    {

        this.x = x;

        this.y = y;

        this.panel = panel;

        graphic = panel.getGraphics();

        g2 = (Graphics2D) graphic;

        g2.fillOval( column,row, 10, 10);

    }

    public void run()

    {

     //startAnimation(this.x,this.y,destination.x,destination.y)

    }

}


神不在的星期二
浏览 107回答 2
2回答

ITMISS

我只想开始,动画并不容易,好的动画也很难。有很多理论可以让动画“看起来”很好,我不打算在这里介绍,有更好的人和资源。我要讨论的是如何在基本级别上用 Swing 制作“好的”动画。第一个问题似乎是您对 Swing 中的绘画工作原理没有很好的理解。您应该首先阅读在 Swing 中执行自定义绘画和在 Swing中绘画接下来,您似乎没有意识到 Swing 实际上不是线程安全的(并且是单线程的)。这意味着您不应该从事件调度线程的上下文之外更新 UI 或 UI 所依赖的任何状态。有关详细信息,请参阅Swing 中的并发。解决此问题的最简单方法是使用 Swing ,有关详细信息Timer,请参阅如何使用 Swing 计时器。现在,您可以简单地运行 aTimer并进行直线、线性进展,直到您的所有点都达到目标,但这并不总是最好的解决方案,因为它不能很好地扩展,并且会根据个人的不同在不同的 PC 上显示不同能力。在大多数情况下,基于持续时间的动画效果更好。这允许算法在 PC 无法跟上时“丢弃”帧。它可以更好地扩展(时间和距离)并且可以高度配置。我喜欢生成可重复使用的代码块,所以我将从一个简单的“基于持续时间的动画引擎”开始......// Self contained, duration based, animation engine...public class AnimationEngine {&nbsp; &nbsp; private Instant startTime;&nbsp; &nbsp; private Duration duration;&nbsp; &nbsp; private Timer timer;&nbsp; &nbsp; private AnimationEngineListener listener;&nbsp; &nbsp; public AnimationEngine(Duration duration) {&nbsp; &nbsp; &nbsp; &nbsp; this.duration = duration;&nbsp; &nbsp; }&nbsp; &nbsp; public void start() {&nbsp; &nbsp; &nbsp; &nbsp; if (timer != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; startTime = null;&nbsp; &nbsp; &nbsp; &nbsp; timer = new Timer(5, new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tick();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; timer.start();&nbsp; &nbsp; }&nbsp; &nbsp; public void stop() {&nbsp; &nbsp; &nbsp; &nbsp; timer.stop();&nbsp; &nbsp; &nbsp; &nbsp; timer = null;&nbsp; &nbsp; &nbsp; &nbsp; startTime = null;&nbsp; &nbsp; }&nbsp; &nbsp; public void setListener(AnimationEngineListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; this.listener = listener;&nbsp; &nbsp; }&nbsp; &nbsp; public AnimationEngineListener getListener() {&nbsp; &nbsp; &nbsp; &nbsp; return listener;&nbsp; &nbsp; }&nbsp; &nbsp; public Duration getDuration() {&nbsp; &nbsp; &nbsp; &nbsp; return duration;&nbsp; &nbsp; }&nbsp; &nbsp; public double getRawProgress() {&nbsp; &nbsp; &nbsp; &nbsp; if (startTime == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0.0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Duration duration = getDuration();&nbsp; &nbsp; &nbsp; &nbsp; Duration runningTime = Duration.between(startTime, Instant.now());&nbsp; &nbsp; &nbsp; &nbsp; double progress = (runningTime.toMillis() / (double) duration.toMillis());&nbsp; &nbsp; &nbsp; &nbsp; return Math.min(1.0, Math.max(0.0, progress));&nbsp; &nbsp; }&nbsp; &nbsp; protected void tick() {&nbsp; &nbsp; &nbsp; &nbsp; if (startTime == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTime = Instant.now();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; double rawProgress = getRawProgress();&nbsp; &nbsp; &nbsp; &nbsp; if (rawProgress >= 1.0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rawProgress = 1.0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; AnimationEngineListener listener = getListener();&nbsp; &nbsp; &nbsp; &nbsp; if (listener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener.animationEngineTicked(this, rawProgress);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // This is done so if you wish to expand the&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // animation listener to include start/stop events&nbsp; &nbsp; &nbsp; &nbsp; // this won't interfer with the tick event&nbsp; &nbsp; &nbsp; &nbsp; if (rawProgress >= 1.0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rawProgress = 1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stop();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static interface AnimationEngineListener {&nbsp; &nbsp; &nbsp; &nbsp; public void animationEngineTicked(AnimationEngine source, double progress);&nbsp; &nbsp; }}它并不过分复杂,它有一段duration时间,它会运行。它将tick以固定间隔(不少于 5 毫秒)生成tick事件,报告动画的当前进度(作为 0 到 1 之间的标准化值)。这里的想法是我们将“引擎”与使用它的那些元素分离。这使我们能够将它用于更广泛的可能性。接下来,我需要一些方法来跟踪移动物体的位置......public class Ping {&nbsp; &nbsp; private Point point;&nbsp; &nbsp; private Point from;&nbsp; &nbsp; private Point to;&nbsp; &nbsp; private Color fillColor;&nbsp; &nbsp; private Shape dot;&nbsp; &nbsp; public Ping(Point from, Point to, Color fillColor) {&nbsp; &nbsp; &nbsp; &nbsp; this.from = from;&nbsp; &nbsp; &nbsp; &nbsp; this.to = to;&nbsp; &nbsp; &nbsp; &nbsp; this.fillColor = fillColor;&nbsp; &nbsp; &nbsp; &nbsp; point = new Point(from);&nbsp; &nbsp; &nbsp; &nbsp; dot = new Ellipse2D.Double(0, 0, 6, 6);&nbsp; &nbsp; }&nbsp; &nbsp; public void paint(Container parent, Graphics2D g2d) {&nbsp; &nbsp; &nbsp; &nbsp; Graphics2D copy = (Graphics2D) g2d.create();&nbsp; &nbsp; &nbsp; &nbsp; int width = dot.getBounds().width / 2;&nbsp; &nbsp; &nbsp; &nbsp; int height = dot.getBounds().height / 2;&nbsp; &nbsp; &nbsp; &nbsp; copy.translate(point.x - width, point.y - height);&nbsp; &nbsp; &nbsp; &nbsp; copy.setColor(fillColor);&nbsp; &nbsp; &nbsp; &nbsp; copy.fill(dot);&nbsp; &nbsp; &nbsp; &nbsp; copy.dispose();&nbsp; &nbsp; }&nbsp; &nbsp; public Rectangle getBounds() {&nbsp; &nbsp; &nbsp; &nbsp; int width = dot.getBounds().width;&nbsp; &nbsp; &nbsp; &nbsp; int height = dot.getBounds().height;&nbsp; &nbsp; &nbsp; &nbsp; return new Rectangle(point, new Dimension(width, height));&nbsp; &nbsp; }&nbsp; &nbsp; public void update(double progress) {&nbsp; &nbsp; &nbsp; &nbsp; int x = update(progress, from.x, to.x);&nbsp; &nbsp; &nbsp; &nbsp; int y = update(progress, from.y, to.y);&nbsp; &nbsp; &nbsp; &nbsp; point.x = x;&nbsp; &nbsp; &nbsp; &nbsp; point.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; protected int update(double progress, int from, int to) {&nbsp; &nbsp; &nbsp; &nbsp; int distance = to - from;&nbsp; &nbsp; &nbsp; &nbsp; int value = (int) Math.round((double) distance * progress);&nbsp; &nbsp; &nbsp; &nbsp; value += from;&nbsp; &nbsp; &nbsp; &nbsp; if (from < to) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = Math.max(from, Math.min(to, value));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = Math.max(to, Math.min(from, value));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; }}这是一个简单的对象,它获取起点和终点,然后根据进度计算对象在这些点之间的位置。它可以在需要时自行绘制。现在,我们只需要一些方法把它放在一起......public class TestPane extends JPanel {&nbsp; &nbsp; private Point source;&nbsp; &nbsp; private Shape sourceShape;&nbsp; &nbsp; private List<Ping> pings;&nbsp; &nbsp; private List<Shape> destinations;&nbsp; &nbsp; private Color[] colors = new Color[]{Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW};&nbsp; &nbsp; private AnimationEngine engine;&nbsp; &nbsp; public TestPane() {&nbsp; &nbsp; &nbsp; &nbsp; source = new Point(10, 10);&nbsp; &nbsp; &nbsp; &nbsp; sourceShape = new Ellipse2D.Double(source.x - 5, source.y - 5, 10, 10);&nbsp; &nbsp; &nbsp; &nbsp; Dimension size = getPreferredSize();&nbsp; &nbsp; &nbsp; &nbsp; Random rnd = new Random();&nbsp; &nbsp; &nbsp; &nbsp; int quantity = 1 + rnd.nextInt(10);&nbsp; &nbsp; &nbsp; &nbsp; pings = new ArrayList<>(quantity);&nbsp; &nbsp; &nbsp; &nbsp; destinations = new ArrayList<>(quantity);&nbsp; &nbsp; &nbsp; &nbsp; for (int index = 0; index < quantity; index++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x = 20 + rnd.nextInt(size.width - 25);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int y = 20 + rnd.nextInt(size.height - 25);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point toPoint = new Point(x, y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create the "ping"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color color = colors[rnd.nextInt(colors.length)];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ping ping = new Ping(source, toPoint, color);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pings.add(ping);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create the destination shape...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle bounds = ping.getBounds();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Shape destination = new Ellipse2D.Double(toPoint.x - (bounds.width / 2d), toPoint.y - (bounds.height / 2d), 10, 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destinations.add(destination);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; engine = new AnimationEngine(Duration.ofSeconds(10));&nbsp; &nbsp; &nbsp; &nbsp; engine.setListener(new AnimationEngine.AnimationEngineListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void animationEngineTicked(AnimationEngine source, double progress) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Ping ping : pings) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ping.update(progress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; engine.start();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Dimension getPreferredSize() {&nbsp; &nbsp; &nbsp; &nbsp; return new Dimension(200, 200);&nbsp; &nbsp; }&nbsp; &nbsp; protected void paintComponent(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; Graphics2D g2d = (Graphics2D) g.create();&nbsp; &nbsp; &nbsp; &nbsp; // This is probably overkill, but it will make the output look nicer ;)&nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);&nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);&nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);&nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);&nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);&nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);&nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);&nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);&nbsp; &nbsp; &nbsp; &nbsp; // Lines first, these could be cached&nbsp; &nbsp; &nbsp; &nbsp; g2d.setColor(Color.LIGHT_GRAY);&nbsp; &nbsp; &nbsp; &nbsp; double fromX = sourceShape.getBounds2D().getCenterX();&nbsp; &nbsp; &nbsp; &nbsp; double fromY = sourceShape.getBounds2D().getCenterY();&nbsp; &nbsp; &nbsp; &nbsp; for (Shape destination : destinations) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double toX = destination.getBounds2D().getCenterX();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double toY = destination.getBounds2D().getCenterY();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.draw(new Line2D.Double(fromX, fromY, toX, toY));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Pings, so they appear above the line, but under the points&nbsp; &nbsp; &nbsp; &nbsp; for (Ping ping : pings) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ping.paint(this, g2d);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Destination and source&nbsp; &nbsp; &nbsp; &nbsp; g2d.setColor(Color.BLACK);&nbsp; &nbsp; &nbsp; &nbsp; for (Shape destination : destinations) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.fill(destination);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; g2d.fill(sourceShape);&nbsp; &nbsp; &nbsp; &nbsp; g2d.dispose();&nbsp; &nbsp; }}好吧,这个“看起来”很复杂,其实很简单。我们创建一个“源”点然后我们创建随机数量的“目标”然后我们创建一个动画引擎并启动它。然后动画引擎将循环遍历所有Pings 并根据当前进度值更新它们并触发新的绘制通道,然后绘制源点和目标点之间的线,绘制Pings,最后绘制源和所有目标点。简单的。如果我想让动画以不同的速度运行怎么办?啊,好吧,这要复杂得多,需要更复杂的动画引擎。一般来说,您可以建立一个“可动画化”的概念。然后,这将由一个中央“引擎”更新,该引擎不断“滴答作响”(本身不受持续时间的限制)。然后,每个“可动画化的”都需要决定如何更新或报告其状态,并允许更新其他对象。在这种情况下,我会寻找更现成的解决方案,例如......Super Simple Swing Animation Framework&nbsp;- 这是我写的一个实验API,所以我只认为它是一个学习点通用补间引擎计时框架三叉戟可运行的例子....https://i.stack.imgur.com/MFQt4.gif import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.Shape;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.geom.Ellipse2D;import java.awt.geom.Line2D;import java.time.Duration;import java.time.Instant;import java.util.ArrayList;import java.util.List;import java.util.Random;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;public class JavaApplication124 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new JavaApplication124();&nbsp; &nbsp; }&nbsp; &nbsp; public JavaApplication124() {&nbsp; &nbsp; &nbsp; &nbsp; EventQueue.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("Testing");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.add(new TestPane());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public class TestPane extends JPanel {&nbsp; &nbsp; &nbsp; &nbsp; private Point source;&nbsp; &nbsp; &nbsp; &nbsp; private Shape sourceShape;&nbsp; &nbsp; &nbsp; &nbsp; private List<Ping> pings;&nbsp; &nbsp; &nbsp; &nbsp; private List<Shape> destinations;&nbsp; &nbsp; &nbsp; &nbsp; private Color[] colors = new Color[]{Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW};&nbsp; &nbsp; &nbsp; &nbsp; private AnimationEngine engine;&nbsp; &nbsp; &nbsp; &nbsp; public TestPane() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; source = new Point(10, 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sourceShape = new Ellipse2D.Double(source.x - 5, source.y - 5, 10, 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dimension size = getPreferredSize();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Random rnd = new Random();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int quantity = 1 + rnd.nextInt(10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pings = new ArrayList<>(quantity);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destinations = new ArrayList<>(quantity);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int index = 0; index < quantity; index++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x = 20 + rnd.nextInt(size.width - 25);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int y = 20 + rnd.nextInt(size.height - 25);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point toPoint = new Point(x, y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create the "ping"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color color = colors[rnd.nextInt(colors.length)];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ping ping = new Ping(source, toPoint, color);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pings.add(ping);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create the destination shape...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle bounds = ping.getBounds();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Shape destination = new Ellipse2D.Double(toPoint.x - (bounds.width / 2d), toPoint.y - (bounds.height / 2d), 10, 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destinations.add(destination);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; engine = new AnimationEngine(Duration.ofSeconds(10));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; engine.setListener(new AnimationEngine.AnimationEngineListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void animationEngineTicked(AnimationEngine source, double progress) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Ping ping : pings) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ping.update(progress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; engine.start();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Dimension getPreferredSize() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Dimension(200, 200);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected void paintComponent(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Graphics2D g2d = (Graphics2D) g.create();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This is probably overkill, but it will make the output look nicer ;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Lines first, these could be cached&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setColor(Color.LIGHT_GRAY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double fromX = sourceShape.getBounds2D().getCenterX();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double fromY = sourceShape.getBounds2D().getCenterY();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Shape destination : destinations) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double toX = destination.getBounds2D().getCenterX();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double toY = destination.getBounds2D().getCenterY();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.draw(new Line2D.Double(fromX, fromY, toX, toY));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Pings, so they appear above the line, but under the points&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Ping ping : pings) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ping.paint(this, g2d);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Destination and source&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setColor(Color.BLACK);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Shape destination : destinations) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.fill(destination);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.fill(sourceShape);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.dispose();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Self contained, duration based, animation engine...&nbsp; &nbsp; public static class AnimationEngine {&nbsp; &nbsp; &nbsp; &nbsp; private Instant startTime;&nbsp; &nbsp; &nbsp; &nbsp; private Duration duration;&nbsp; &nbsp; &nbsp; &nbsp; private Timer timer;&nbsp; &nbsp; &nbsp; &nbsp; private AnimationEngineListener listener;&nbsp; &nbsp; &nbsp; &nbsp; public AnimationEngine(Duration duration) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.duration = duration;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void start() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (timer != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTime = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer = new Timer(5, new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tick();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.start();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void stop() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.stop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTime = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void setListener(AnimationEngineListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.listener = listener;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public AnimationEngineListener getListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return listener;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Duration getDuration() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return duration;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public double getRawProgress() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (startTime == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Duration duration = getDuration();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Duration runningTime = Duration.between(startTime, Instant.now());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double progress = (runningTime.toMillis() / (double) duration.toMillis());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Math.min(1.0, Math.max(0.0, progress));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected void tick() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (startTime == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTime = Instant.now();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double rawProgress = getRawProgress();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rawProgress >= 1.0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rawProgress = 1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AnimationEngineListener listener = getListener();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (listener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener.animationEngineTicked(this, rawProgress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This is done so if you wish to expand the&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // animation listener to include start/stop events&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this won't interfer with the tick event&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rawProgress >= 1.0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rawProgress = 1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static interface AnimationEngineListener {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void animationEngineTicked(AnimationEngine source, double progress);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Ping {&nbsp; &nbsp; &nbsp; &nbsp; private Point point;&nbsp; &nbsp; &nbsp; &nbsp; private Point from;&nbsp; &nbsp; &nbsp; &nbsp; private Point to;&nbsp; &nbsp; &nbsp; &nbsp; private Color fillColor;&nbsp; &nbsp; &nbsp; &nbsp; private Shape dot;&nbsp; &nbsp; &nbsp; &nbsp; public Ping(Point from, Point to, Color fillColor) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.from = from;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.to = to;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.fillColor = fillColor;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; point = new Point(from);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dot = new Ellipse2D.Double(0, 0, 6, 6);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void paint(Container parent, Graphics2D g2d) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Graphics2D copy = (Graphics2D) g2d.create();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = dot.getBounds().width / 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = dot.getBounds().height / 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copy.translate(point.x - width, point.y - height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copy.setColor(fillColor);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copy.fill(dot);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copy.dispose();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Rectangle getBounds() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = dot.getBounds().width;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = dot.getBounds().height;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Rectangle(point, new Dimension(width, height));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void update(double progress) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x = update(progress, from.x, to.x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int y = update(progress, from.y, to.y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; point.x = x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; point.y = y;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected int update(double progress, int from, int to) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int distance = to - from;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int value = (int) Math.round((double) distance * progress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value += from;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (from < to) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = Math.max(from, Math.min(to, value));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = Math.max(to, Math.min(from, value));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}有没有更简单的😓正如我所说,好的动画很难。需要付出很多努力和计划才能做好。我什至没有谈到地役权、链式或混合算法,所以请相信我,这实际上是一个简单、可重用的解决方案不信,看看Java Swing 中的 JButton 悬停动画

智慧大石

试试这个:public class Player extends Thread {&nbsp;private&nbsp; Graphics graphic;&nbsp;private Graphics2D g2;&nbsp;private int x;&nbsp;private int y;&nbsp;private DrawPanel panel;&nbsp;private numberOfIteration=5;&nbsp;private currentNumberOfIteration=0;&nbsp; &nbsp; public Player(int x, int y,DrawPanel panel)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; &nbsp; &nbsp; this.panel = panel;&nbsp; &nbsp; &nbsp; &nbsp; graphic = panel.getGraphics();&nbsp; &nbsp; &nbsp; &nbsp; g2 = (Graphics2D) graphic;&nbsp; &nbsp; &nbsp; &nbsp; g2.fillOval( column,row, 10, 10);&nbsp; &nbsp; }&nbsp; &nbsp; public Player(int x, int y,DrawPanel panel,int numberOfIteration)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; &nbsp; &nbsp; this.panel = panel;&nbsp; &nbsp; &nbsp; &nbsp; this.numberOfIteration=numberOfIterarion;&nbsp; &nbsp; &nbsp; &nbsp; graphic = panel.getGraphics();&nbsp; &nbsp; &nbsp; &nbsp; g2 = (Graphics2D) graphic;&nbsp; &nbsp; &nbsp; &nbsp; g2.fillOval( column,row, 10, 10);&nbsp; &nbsp; }&nbsp; &nbsp; public void run()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;//startAnimation(this.x,this.y,destination.x,destination.y)&nbsp; &nbsp; &nbsp; &nbsp; currentNumberOfIteration=(++currentNumberOfIteration)%numberOfIteration;&nbsp; &nbsp; &nbsp; &nbsp; currentX=(int)((destinationX*currentNumberOfIteration+this.x)/(currentNumberOfIteration+1));&nbsp; &nbsp; &nbsp; &nbsp; currentY=(int)((destinationY*currentNumberOfIteration+this.y)/(currentNumberOfIteration+1));&nbsp; &nbsp; &nbsp; &nbsp; g2.fillOval( currentX,currentY, 10, 10);&nbsp; &nbsp; }}我没有看到destinationXand的任何声明部分destinationY。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java