g.rotate question

hello,

I am working on Kev’s source code for T4XI. I wanna port it to J2ME but the Graphics does not have the rotate method only the translate. I am working on a method that draws a little red car.


                        g.translate(cars[i][0]*30,cars[i][1]*30);
                  g.rotate(cars[i][2]);
                  g.setColor(Color.black);
                  g.fillRect(-9,-7,14,5);
                  g.fillRect(-9,5,14,5);
                  g.setColor(carCols[(int)            cars[i][7]].darker());
                  g.fillRect(-7,-11,10,23);
                  g.setColor(carCols[(int) cars[i][7]]);
                  g.fillRect(-7,-5,10,15);
                  g.rotate(-cars[i][2]);
                  g.translate(-cars[i][0]*30,-cars[i][1]*30);

So, the question is, how can I substitute the Graphics.rotate method with a manual made rotate method?

I’ve tried the following formula (I’m not sure right now, since I’m not with my source code here, but is something similar to this):

x’ = xMath.cos(cars[i][2]) - yMath.sin(cars[i][2])
y’ = yMath.cos(cars[i][2]) + xMath.sin(cars[i][2])

With this code, the car make a circle movement (all the car), which is not what I wanna. I wanna the car move like T4XI game (left and right arrow rotates the car around its axis, and up and down arrows move the car back and forth).

I hope that I made myself clear

You may want to rotate around a point, e.g. the car’s centre of mass, instead of rotating about (0,0) or however it works presently.

First, define (statically?) all the points that describe the car’s shape in the car’s own coordinate system. It would be something like

(16,16)
(16,-16)
(-16,16)
(-16,-16)

Then use your rotation formula on these points. That will rotate the car about its geometrical centre (if you want to rotate it about another point, add that point’s xy coordinates to all the above coordinates before applying the rotation formula).

Last, add the point which defines the car’s actual position to all those that you have just calculated. This will translate the car from (0,0) to (wherever it should be).

but what about the fillRect method? When I rotate the car points (on car’s coordinate system) I will end with new rotated points. But when the fillRect is applied, I will get a rectangle that is not rotate, only translated. I need that fillRect rotate

True, fillRect can only fill rectangles that are parallel to the axes, and in order for it to fill anything else you must have a Graphics2D.rotate method - which, you say, does not exist.

Use fillPolygon instead on the newly calculated points.