Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Rotating an object while also moving it along an axis

Vic Didio
Greenhorn
Posts: 3
I have an Area object that I want to rotate while also translating down the y axis. I have it mostly figured out. By using a compound transformation I can rotate it while moving the star along the y axis. Here is the problem, while the object is also rotating and translating, it also seems to be rotating around an origin that I cannot make out. Essentially, it is rotating itself and around an origin along the y axis; kinda like the moon revolving around the Earth if the Earth was moving in a straight line. Any suggestion on how to solve this?

Campbell Ritchie
Marshal
Posts: 55698
163
• 1
Don't use paint. Use paintComponent. And it doesn't have public accesss.

You would do well to copy the Graphics object, and to call the superclass paintComponent method firstThat way any transforms are consumed by a particular graphics object, and there is no problem about undoing them. Rotate translate and scale don't tend to cause undoing problems; if you have such problems it is with shear. But after using shear everything else seems out of line. So confine your transforms to copy graphics objects.

You don't need to create a separate affine transform object. The graphics 2D object can do that for itself. You can read about the methods of graphics 2D here.If you find the details of rotate, it says radians relative to the previous origin, so the centre of rotation is the origin of the Cartesian coordinate system you are using. You have the option of moving the origin, or of using the overloaded version of rotate() to move the origin to your star, then moves it back whence it came. Note that rotation by a positive argument is anticlockwise. You can make it clockwise by scaling the graphics 2D by two arguments one positive and one negative.

I don't know anything about your star object, and what its location is. Also, where are you adding your Affine Transform object to the graphics object?

And welcome to the Ranch

And welcome to the Ranch

Vic Didio
Greenhorn
Posts: 3
Thanks! I made the changes suggested. Here is the code of my star object:
As for the Affine Transform object, the code that posted before was all the code that had to do with Affine Transform. Also I would think that needing the origin of the object was only really necessary for rotation. For all intents and purposes it is rotating around its origin...

Vic Didio
Greenhorn
Posts: 3
The fix was easy. The polygon and ellipse had to be declared with int arrays for the points. By having all the other values be doubles, they weren't playing nicely with the variables that had to be ints. So by making xloc and yloc int from double, it partially solved the problem. After I did this the objects would be moving the right way but way too fast so I added a timer to better increment the yloc value and slow down the animation.

Campbell Ritchie
Marshal
Posts: 55698
163
Well done

 It is sorta covered in the JavaRanch Style Guide.