Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(src.getRotationInDegrees() != 0)
- {
- int pivotX = textLocationX;
- int pivotY = textLocationY;
- cardGfx.drawOval(pivotX - 1, pivotY - 1, 3, 3);
- cardGfx.rotate(src.getRotationInDegrees() * Math.PI / 180.0,
- PivotX,
- PivotY);
- }
- textLocationX -= (hAlign == Horizontal.left ? 0 : hAlign == Horizontal.middle ? (textWidth / 2) : textWidth);
- textLocationY -= (vAlign == Vertical.top ? 0 : vAlign == Vertical.middle ? (textHeight / 2) : textHeight);
- /*
- pivotX/Y is the point I'm trying to move - this is where I'm rotating the text around, and I want to rotate the text around its centre. At the minute, this is at the top if vAlign == Vertical.top, the middle if vAlign == Vertical.middle, and the bottom if vAlign == Vertical.bottom, and at the left if hAlign == Horizontal.left, you get the idea.
- I still want to move the text depending on the values of hAlign and vAlign, but I always want pivotX/Y to be in the centre of the text, regardless of the values of hAlign and vAlign.
- drawOval is just so I can visually see where the pivot is. Actually drawing the text is handled later on.
- I tried adding/subtracting half of the text's pre-rotation width/height (depending on the values of hAlign and vAlign) to/from a copy of the pivot point, then rotating that copy around the original by the same rotation as is applied to the graphics2d object, but the resulting pivot seems to only be correct where rotation == 0 or where hAlign == middle and vAlign == middle - otherwise, it seems to drift in a circle for some reason.
- This is my code where I tried this - this uses a rotatePoint method that takes (pivotPoint, rotationDegrees, pointToRotate) and mathematically rotates pointToRotate around pivotPoint by the passed rotationDegrees.
- */
- if(src.getRotationInDegrees() != 0)
- {
- int initialPivotX = textLocationX;
- int initialPivotY = textLocationY;
- double adjustedPivotX = initialPivotX + (hAlign == Horizontal.left ? (textWidth / 2) : hAlign == Horizontal.middle ? 0 : -(textWidth / 2));
- double adjustedPivotY = initialPivotY + (vAlign == Vertical.top ? (textHeight / 2) : vAlign == Vertical.middle ? 0 : -(textHeight / 2));
- Point2D initialPivot = new Point2D.Double(initialPivotX, initialPivotY);
- Point2D adjustedPivot = new Point2D.Double(adjustedPivotX, adjustedPivotY);
- adjustedPivot = rotatePoint(initialPivot, src.getRotationInDegrees(), adjustedPivot);
- cardGfx.drawOval((int)adjustedPivot.getX() - 1, (int)adjustedPivot.getY() - 1, 3, 3);
- cardGfx.rotate(src.getRotationInDegrees() * Math.PI / 180.0,
- adjustedPivot.getX(),
- adjustedPivot.getY());
- }
- textLocationX -= (hAlign == Horizontal.left ? 0 : hAlign == Horizontal.middle ? (textWidth / 2) : textWidth);
- textLocationY -= (vAlign == Vertical.top ? 0 : vAlign == Vertical.middle ? (textHeight / 2) : textHeight);
- // If it's any use, my rotatePoint method is:
- public static Point2D rotatePoint(Point2D pivotPoint, double rotationDegrees, Point2D pointToRotate)
- {
- double rads = rotationDegrees * Math.PI / 180;
- return new Point2D.Double(pivotPoint.getX() + (int)Math.round((pointToRotate.getX() - pivotPoint.getX()) * Math.cos(rads) - (pointToRotate.getY() - pivotPoint.getY()) * Math.sin(rads)),
- pivotPoint.getY() + (int)Math.round((pointToRotate.getX() - pivotPoint.getX()) * Math.sin(rads) + (pointToRotate.getY() - pivotPoint.getY()) * Math.cos(rads)));
- }
Add Comment
Please, Sign In to add comment