Computer Science Canada

Maybe someone knows

Author:  FakeOut [ Wed Apr 30, 2008 5:08 pm ]
Post subject:  Maybe someone knows

Hi I'm new here and I've got a very quick question.
Is there any way of rotating an image in java? For example, I have a picture of a car and I want to prompt the user to enter a number between 1 and 359. The program then rotates the image by that many degrees. I just want to know because it will take way too long for me to add the rotated images one by one.

Thanks in advance.

Author:  HeavenAgain [ Wed Apr 30, 2008 5:26 pm ]
Post subject:  RE:Maybe someone knows

hello, and welcome to compsci!

after looking thru the api things that might be able to help you out,
Graphics2D -> drawRenderableImage() and rotate()

Author:  McKenzie [ Wed Apr 30, 2008 9:04 pm ]
Post subject:  Re: Maybe someone knows

HeavenAgain might be right, but when I've done it I've used AffineTransform. AffineTransform basically rotates the whole drawing context which is why I save the current settings first then reset them after.

code:
public void draw (Graphics g)
    {
                Graphics2D g2D = (Graphics2D)g;
       
                AffineTransform saveXform = g2D.getTransform();
                AffineTransform at = new AffineTransform();
                at.rotate(direction,posX+25,posY+25);
                g2D.transform(at);
                g2D.drawImage(shipPic,(int)posX,(int)posY,panel);
                g2D.setTransform(saveXform);
            } // end of draw()


: