如何在Graphics2D上下文中对Path2D对象上的矩形进行动画处理 [英] How to animate Rectangle on a Path2D object in Graphics2D context

查看:126
本文介绍了如何在Graphics2D上下文中对Path2D对象上的矩形进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习有关Graphics2D类的基础知识,到目前为止,我已经能够绘制不同的对象并实现ActionListener,以通过onKeyPress实际在屏幕上移动它们.到目前为止,一切都很好,而我想到的是做一些更复杂的事情.我想给对象一个路径,并仅在该特定路径上为其设置动画.

I have just started learning basics about Graphics2D class, So far I am able to draw different objects and implements ActionListener to actually move them on screen by onKeyPress. So far so good, While I thought of doing something more complicated. I want to give a path to my object and animate it on that particular path only.

类似的事情,我将在天空上绘制一条线,并且飞机应该坚持该绘制的线,并使它自己在该特定线上飞行.现在有可能吗?

Something like, I will draw a line on sky and a plane should stick with that drawn line and keep it self to fly on that particular line. Now is it possible?

我不需要任何代码,但是很少有不同的方法或想法可以让我开始着手进行此工作.下面是对我的想法的可视化阐述.

I don't need any sort of code, But few different method or ideas will let me get started working on this. A visualise elaboration of my idea is as below.

起点:

终点:

现在,如上图所示,我的黄色框(在未来的平面中)应该在进行动画设置时保留给定的路径(路径灰线)

Now as shown above, my yellow box(in future plane) should stick with given path while animating(path grey line)

我到目前为止的研究

My research so far,

我在Java中搜索了诸如path之类的流行词,并找到了Path2DGeneralPath类,有人知道我是否可以使用它来解决这个问题.

I have searched my buzz words such as path in java, and found Path2D and GeneralPath classes, Does anyone know if I can use that to solve this.

谢谢

推荐答案

太棒了!

这让我想起了我在IT领域迈出的第一步.我多么喜欢所有这些简单的数学知识,但是却使事情在屏幕上移动. :)

It reminds me of my first steps in IT. How much I enjoyed all this simple math stuff but that make things move on screen. :)

您实际上需要的是线性插值.还有其他几种插值方式,某些api为该概念提供了很好的封装,但这是主要思想,您经常会需要这些东西:

What you need is actually a linear interpolation . There are other sorts of interpolation and some api offer a nice encapsulation for the concept but here is the main idea, and you will quite often need this stuff :

您必须重写路径

y = f (x ) 

作为时间的函数:

在时间0,该项目将位于开始位置,在时间1,该项目将到达结束.然后根据需要增加时间(t)(例如,每毫秒0.001).

at time 0 the item will be on start position, at time 1 it will reach the end. And then you increment time (t) as you wish (0.001 every ms for instance).

这是一条简单线性路径的公式:

So here is the formula for a simple linear path :

x = xstart + (xend-xstart) * t
y = ystart + (yend-ystart) * t

当t变化时,您的对象将沿着路径线性移动,线性地在所有路径上保持恒定的速度.例如,您可以想象某种重力吸引,它可以通过二次加速度(t ^ 2而不是t)来建模...

when t varies, you object will just move linearly along the path, linearly has speed will be constant on all the path. You could imagine some kind of gravtity attraction at end for instance, this would be modeled by a quadratic acceleration (t^2 instead of t) ...

关于, 斯蒂芬(Stephane)

Regards, Stephane

这篇关于如何在Graphics2D上下文中对Path2D对象上的矩形进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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