Okay, that’s quite easy to change the position’s anchor to the center or any other arbitrary number. To center the image, just subtract half of the image’s width from the x-position and half of the height from the y-position. Here’s an example:
public void paint(Graphics g) {
g.drawImage(img, x-(img.getWidth()/2), y-(img.getHeight()/2), null);
}
Now to rotate an image, it gets a little more complex. You first need to create a completely empty BufferedImage, with the same width/height of the orignal image and the same transparency (ex. TYPE_INT_ARGB). Then you obtain the Graphics2D object of the empty image by calling createGraphics() on it. You then create an AffineTransform object (located in the java.awt.geom package). Then you call rotate(double, double, double) on your AffineTransform object. The first parameter is your angle to rotate upon, in radians (convert degrees to radians with Math.toRadians(int)). The second parameter is your x-anchor to rotate on. The last parameter is your y-anchor to rotate on. The next step is to call setTransform(AffineTransform) on the Graphics2D object you obtained. The last step is to draw the orignal image onto the empty image at 0,0 (using the Graphics2D again).
Here’s the code to rotate:
public BufferedImage rotate(Image source, int angle) {
int w = source.getWidth(null);
int h = source.getHeight(null);
BufferedImage image = new BufferedImage(w, h, source.getTransparency());
Graphics2D g = image.createGraphics();
AffineTransform geom = new AffineTransform();
geom.rotate(Math.toRadians(angle), w/2, h/2);
g.setTransform(geom);
g.drawImage(source, 0, 0, null);
g.dispose();
return image;
}