Affine Transformation queri

Hello, I'm a computer science student, and i have to write a small program , that draws a sun, rotating around it self, a planet rotating around the sun, and a moon rotating around the planet.

I know I have to use the setRotate, and translate methods in the AffineTransform class, but I can't seem to manage to make the correct translation so that the moon will rotate around the planet, as the planet rotates around the sun.

Any kind of help would be deeply appriciated.

[506 byte] By [pslayer666] at [2007-9-26 12:37:14]
# 1

Looks to me that you will need at least two images:

1. A sun

2. A planet with a moon.

In your paintComponent() method, you would

1. draw the sun in the center of its panel

2. draw the planet/moon at various positions around the sun.

Before drawing the sun, set a simple AffineTransform to center the image.

Then change the AffineTransform to one that will translate the planet/sun image to the desired position.

You could use a swing.Timer to call repaint( )

tedhill at 2007-7-2 10:04:36 > top of Java-index,Security,Cryptography...
# 2

You can also use some simple trig to recalculate positions. To do this all you need is the radius between the sun and the planet. Let's say it's 5km, start the planet rotating @ 0 degrees from the x axis. Then just increase the increment of degrees by 1 each time (the change in degrees is not really importants, it will just make the motion more or less choppy)

@ 0 degrees, cos(0) x 5000 = 5000

sin(0) x 5000 = 0

the cosine calculation maps to the x axis, and the sine maps to the y axis

the only sticky wicket is when you change quadrants, sign flipping is required (in any basic trig textbook)

Don't know if this is any faster than an affine transformation, but it gives me a feeling of total control:)

If you really want to have some fun, use the Force of gravitational attractions (which = GMm/r^2) with innitial velocities and watch as the planets rotate like the real thing! (cause it's cool;p)

JDunlop at 2007-7-2 10:04:36 > top of Java-index,Security,Cryptography...