开发者

How to animate Rectangle on a Path2D object in Graphics2D context

开发者 https://www.devze.com 2023-03-31 14:51 出处:网络
I have just started learning basics a开发者_JAVA百科bout Graphics2D class, So far I am able to draw different objects and implements ActionListener to actually move them on screen by onKeyPress. So fa

I have just started learning basics a开发者_JAVA百科bout 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.

Start Point :

How to animate Rectangle on a Path2D object in Graphics2D context

End Point :

How to animate Rectangle on a Path2D object in Graphics2D context

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

My research so far,

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.

Thanks


Great !

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. :)

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 :

you must rewrite your path

y = f (x ) 

as a function of time :

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

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) ...

Regards, Stephane


First, make the ability to move from point a to point b. This is done with simple algebra.

Second, make the ability to take a path and translate it into points. Then when you're going to do curves, you're really just moving from point to point to point along that curve.

This is the most elementary way to do it and works for most instances.


What your talking about is simple 2D graphics and sprites. If that is all you need then for Java take a look at Java 2D Sprites If your leaning more towards or will eventually go with camera perspectives and wanting to view the animation from diferent angles the go with Java 3D from the OpenSource Java 3D.org. Either way what you want is a simple translating of the object along a line, pretty simple in either 2D or 3D.


You can try going trough the code of my open source college project - LANSim. It's code is available in Code menu. It does similar to what you are trying to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消