Take the 2-minute tour ×

I am trying to rotate a player to follow the mouse. To do this I use a Graphics obj casted to a Graphics2D object and use the rotate method. Here is my Panel draw:

public void paint(Graphics g){
    g.clearRect(0, 0, this.getWidth(), this.getHeight());
    //draw the existing walls
    for(Wall w : walls)

    //draw the potential wall

    //draw the lineWalls
    for(Wall w : lineWalls)

All my rotation stuff is happening in player.draw(g), but I figured it would be better to have more information than less. Here is my player.draw(g)

    public void draw(Graphics g){
    //draw the player as a circle for now
    Graphics2D g2d = (Graphics2D)g;
    g2d.drawOval(getX(), getY(), 20, 20);

I have tried many combinations of the g2d.rotate and drawing the shapes. Any advice as how i can rotate the player and the sword, but not the entire world itself?

share|improve this question
You could take a look at this which rotates a image based on the position of the mouse cursor –  MadProgrammer Nov 23 '12 at 21:34
hmm, that is kinda what I'm already doing. The issue I'm having is the everything that graphics has been drawing is rotated instead of just the one circle and one rectangle i want to rotate. –  ceptno Nov 24 '12 at 0:36
Rotating a circle isn't go to achieve much ;), however, you could use the Shape API, which will allow you to "transform" the shape. Take a look at this, you'll have to scroll to the bottom of the source code, but I create a triangle shape and rotate it within its own context –  MadProgrammer Nov 24 '12 at 1:19
I have that code written currently, but for some reason it isn't rotating around the correct axis. –  ceptno Nov 24 '12 at 18:34
cool I got it working, much appreciated!!! :D:D:D –  ceptno Nov 24 '12 at 18:51

1 Answer 1

up vote 2 down vote accepted

I would try drawing your player to its own image (with its own graphics object), rotating THAT image, and then drawing that image on your main graphics.

You'll run into some potentially annoying hurdles to get across, like transparency on the temporary image, but they aren't anything that can't be gotten around with a little blood sweat and tears.

share|improve this answer
How do you rotate such an image? –  ceptno Nov 23 '12 at 21:06
Using whatever method you used to rotate your main image. I would try tempImage.createGraphics().rotate(rotation); But since I have tried it, take that with a grain of salt. –  corsiKa Nov 23 '12 at 21:12
hmm I really would like to know how to do it with primitives. Thanks though –  ceptno Nov 23 '12 at 21:16
@Brandon What do mean by primitives? –  MadProgrammer Nov 23 '12 at 21:35
Like g.drawOval(), drawRect() etc. –  ceptno Nov 24 '12 at 0:28

Your Answer


