How to rotate an object in Swing graphics

There is really only one way to rotate something -- using a rotation matrix. However, most graphics packages do the programming for you already.
In this page, we'll see how to rotate a 2D object.

Math rotation matrix

Imagine a point located at (x,y). If you wanted to rotate that point around the origin, the coordinates of the new point would be located at (x',y').

The standard math formulas for doing this are:

x' = xcosθ - ysinθ
y' = xsinθ + ycosθ

This will rotate a point by q degrees (or radians) counter clockwise.
If you wanted to rotate the point around something other than the origin, you need to first translate the whole system so that the point of rotation is at the origin. Then perform the rotation. And finally, undo the translation.
Finally, when we do computer graphics, the y-axis is inverted, so everything rotates in the opposite direction from what we expect.

CODE:

	 /* Purpose: to rotate any point by an angle (radians) about a centre point.
	 * 			The rotation is in the same direction as angles in math (anticlockwise).
	 * Called from: this.rotate()
	 * Calls: none (but creates PointD object to return)
	 * @param angle in radians
	 * @param x x value of initial point
	 * @param y y value of initial point
	 * @param centrex, centrey rotate initial point about this point
	 *
	 * @return PointD object containing new point in double precision
	 */
	PointD rotatePoint(double angle, double x, double y, double centrex, double centrey) {
		double newx = (x-centrex) * Math.cos(angle) + (y-centrey) * Math.sin(angle);
		double newy = -(x-centrex) * Math.sin(angle) + (y-centrey) * Math.cos(angle);
		PointD pd = new PointD(); 
		pd.x = newx+centrex;
		pd.y = newy+centrey;
		return pd;
	}

	/** This is a small class that enables the user to have a point with x,y fields that are double
	 *  instead of "int" that java.awt.Point forces you to have.
	 */
	private class PointD {
		double x, y;
	}

Anyone going on in programming at university, should figure out how to do use the rotation equations. See Wolfram Alpha

Graphics2D rotate() in Swing only

Using Swing, we can just rotate the graphics object (ie the screen upon which we draw), and then draw the point where it is. This ends up being the same as if we've rotated the point.

	/* Rotating with a positive angle theta rotates points on the positive x axis toward the positive y axis,
	* exactly the same as in math, but here +y is down so the rotation goes in the opposite direction. */
	g2.rotate(angle, line.cx, line.cy);                       
	line.paint(g);
	g2.rotate(-angle, line.cx, line.cy);

Gaphics2D AffineTransform in Swing only

I'm not sure what the difference is between this and the previous method.

	AffineTransform transform = new AffineTransform();
	transform.rotate(angle, line.cx, line.cy);
	AffineTransform old = g2.getTransform();
	g2.transform(transform);
	line.paint(g);                        
	g2.setTransform(old);

NOTE: in most cases, if you're rotating different things in different amounts, you have to set the graphics object back to its original orientation before you draw more stuff on it.

Sample programs

Method 1: using rotation matrix

TimerRotate.java and Line.java and

Method 2: rotating graphics object

TimerRotate2.java and Line.java.