0

I am coding a simple shooter game whereby i want to rotate my character to the direction of the mouse and "fire". I have done all the code for this except rotating the actual image. Here is some of my code so far: (THIS IS ALL UNDER THE PAINT COMPONENT METHOD)

   xCent = x + 50;
    yCent = y + 50; // x and y center of image ( x and y change depending on Keyboard Input)       
    a.setToRotation(theta, xCent,yCent); // a = new AffineTransform()    Here is my calculation of theta (under the MouseMotionListener):  theta = Math.atan2(e.getY() - yCent,e.getX() - xCent);
    a.setToTranslation(x,y);
    a.setToRotation(theta, xCent,yCent);
    g2.drawImage(charac,a, null);

How do i go about now "setting" the x and y coordinates of the image for the Graphics2D object to draw it??

1
  • Sorry, ignore the last a.setToRotation(theta, xCent,yCent); May 30, 2013 at 12:53

1 Answer 1

0

Have a look at this:

http://www.javalobby.org/java/forums/t19387.html

public void paint(Graphics g) {`

    AffineTransform transformer = new AffineTransform();
    transformer.translate(5,5);
    transformer.scale(2,2);
    Graphics2D g2d = (Graphics2D)g;
    g2d.setTransform(transformer);
    // draw to g2d.
}

In your case off course you would do the rotation and then translation rather than translate and scale.

1
  • I have tried this, but the translate(x,y) seems to override the rotate(theta) May 31, 2013 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.