Help with Image Rotating?
I'm a novice please be gentle ;D
I need to rotate an image about its center, by default it rotates around the top left corner.
The image is 30x30 so ideally, I would like it to rotate about its 15x15 point.
Right now I'm manually putting in an angle (in radians) but I would like to eventually use the inverse tangent of the mouse's x and y positions on the screen to find the angle, so the image is always facing the mouse pointer.
Code:
public class RotateImage extends JPanel{
// Declare an Image object for us to use.
Image image;
int mousex = (MouseInfo.getPointerInfo().getLocation().x);
int mousey = (MouseInfo.getPointerInfo().getLocation().y);
double pos = (mousex/mousey);
double angle = 1.57;
double arctan = Math.atan(Math.toRadians(angle));
// Create a constructor method
public RotateImage(){
super();
// Load an image to play with.
image = Toolkit.getDefaultToolkit().getImage("Player2.png");
}
public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g; // Create a Java2D version of g.
g2d.translate(400, 400); // Translate the center of our coordinates.
g2d.rotate(angle); // Rotate the image by 1 radian.
g2d.drawImage(image, 0, 0, 200, 200, this);
}
public static void main(String arg[]){
JFrame frame = new JFrame("RotateImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.setLocationRelativeTo(null);
RotateImage panel = new RotateImage();
frame.setContentPane(panel);
frame.setVisible(true);
}
}
-Thanks!