TheMiscGamer
TheMiscGamer

Reputation: 23

How to rotate an image on command using Codename One's Transform class?

I am currently doing a school project where we are creating an Asteroids game using Codename One. My current functionality works well, except for when it comes to rotating the image of the ship. Using the Transform class has been ineffective; the image does not rotate, no matter how the Transform is applied or the image is drawn.

Here is a sample portion of the code used to turn:

public void turnRight() //Rotates the ship 5 degrees clockwise
{
    if (direction==355)
        direction = 0;
    else
        direction+=5;
    Transform tmpTransform = Transform.makeIdentity();
    theImage.getGraphics().getTransform(tmpTransform);
    tmpTransform.rotate((float)Math.toRadians(5), x, y);
    theImage.getGraphics().setTransform(tmpTransform);
    theImage.getGraphics().drawImage(shipPic, 0, 0);
}

Where:

In addition, I have tried using the draw(Graphics g, Point p) method and passing theImage.getGraphics(), as well as passing shipPic.getGraphics() I am at a loss, and Codename One's documentation on the subject is unhelpful.

Could I get some assistance, please?

Upvotes: 2

Views: 276

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

You need to use one graphics object so something like this:

Graphics g = theImage.getGraphics();

Would be more correct. You also must test for transform support when rendering onto an image as low level graphics isn't always portable to all OS's in all surfaces. A good example is iOS where rendering onto an image uses a completely different low level implementation than the display rendering.

Normally I would render directly to the display as that is hardware accelerated on modern devices and images are often implemented in software.

About the documentation, did you read the graphics section in the developer guide?

It should contain explanations of everything and if something is missing there is search. If you still can't find something and figure it out by yourself notice you can also edit the docs and help us improve them.

Upvotes: 1

Related Questions