Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

custom shape rotation issue

Mihir K Parekh
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to rotate a custom shape around its center, but can not get the result as expected.

what i want is

*shape should be rotated around its center without moving itself.*

what my solution is currently doing is rotating
a whole shape around its center , by every rotation its changing its position.

I have multiple shapes so i have created a class to encapsulate a shape with its transform in following class

I am calculating angle of rotation using following method

public static double getAngle(Point2D origin, Point2D other) {

double dy = other.getY() - origin.getY();
double dx = other.getX() - origin.getX();
double angle;

if (dx == 0) {// special case
angle = dy >= 0 ? Math.PI / 2 : -Math.PI / 2;
} else {
angle = Math.atan(dy / dx);
if (dx < 0) // hemisphere correction
angle += Math.PI;
// all between 0 and 2PI
if (angle < 0) // between -PI/2 and 0
angle += 2 * Math.PI;
return angle;

in mouse press event of the canvas mouse listener

selectedShape.onMousePress(me, canvasBoundary, shoeViewer

i am just calling selected shape's onMousePress method

and in my mouse drag method of the canvas mouse listener , i am just calling the selected shape's onMouseDrag method which updates the rotation angle as you can see from the very first class

selectedShape.onMouseDrag(me, canvasBoundary, shoeViewer

and you can see the draw method of the individual shape , to draw the shape according to current transform , i am calling from paintComponent like

Iterator<Shoe> shoeIter = shoeShapeMap.values().iterator();

while (shoeIter.hasNext()) {

Shoe shoe =;
shoe.draw(g2, firstTime);


where shoeShapeMap contains all of the custom shapes currently on the canvas.

is i am doing mistake in calculating angle or determining anchor point ? my current solution rotates shape 360 degree by checking all the conditions[90 degree etc.] as you can see in the above mentioned method.

i want the shape should be rotated around its center without resizing its positions ? in the word it is difficult to explain , so please suggest me any better way to show here what i want to accomplish ?

i think i have mentioned all the things related to this issue. if you have any doubts please feel free to ask me.

Thank You
Mihir Parekh
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic