1

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.setColor(Color.white);
    g.clearRect(0, 0, this.getWidth(), this.getHeight());
    player.draw(g);
    enemy.draw(g);
    mouseSelection.draw(g);
    wallBoard.draw(g);
    //draw the existing walls
    for(Wall w : walls)
        w.draw(g); 

    //draw the potential wall
    potentialWall.draw(g);

    //draw the lineWalls
    for(Wall w : lineWalls)
        w.draw(g);
}

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
    g.setColor(Color.black);
    Graphics2D g2d = (Graphics2D)g;
    g2d.drawOval(getX(), getY(), 20, 20);
    sword.draw(g2d);
    g2d.rotate(rotation);
    g2d.rotate(0);
}

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?

ceptno
  • 687
  • 2
  • 6
  • 28
  • You could take a look at [this](http://stackoverflow.com/questions/11911316/java-2d-rotation-in-direction-mouse-point/11911470#11911470) 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 00:36
  • Rotating a circle isn't go to achieve much ;), however, you could use the [`Shape`](http://docs.oracle.com/javase/tutorial/2d/geometry/index.html) API, which will allow you to "transform" the shape. Take a look at [this](http://stackoverflow.com/questions/13528953/detecting-multiple-keypresses-in-java/13537419#13537419), 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 01: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 Answers1

2

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.

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • 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
  • @Brandon you want to try to do it with matrix rotation? That sounds quite silly. I highly recommend using the libraries that were made for this exact kind of thing :) Creating temporary graphics is something that happens all the time in graphics engines. It's just easier to work with smaller pieces and add them to bigger pieces as you go. – corsiKa Nov 23 '12 at 21:39
  • 1
    Like g.drawOval(), drawRect() etc. – ceptno Nov 24 '12 at 00:28