6

I got the following code:

import java.awt.*;
import javax.swing.*;

public class GraphicPanel extends JPanel {

    public void paintComponent(Graphics g){

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.setFont(new Font("Arial",Font.BOLD,24));
        g2d.drawString("A",300,250);
        g2d.rotate(45 * Math.PI/180);
        g2d.drawString("B",300,250);

    }

}

It should rotate B and put it in the same place A is, but for some reason, B apears at a totally different location.

Pretty sure I understand why that is. It's because the entire coordination system is manipulated when scaling, rotating etc (am I correct?)

Anyway, I'd like to know how to rotate or scale an object, and still have it appear where it was before. For example in a game that keeps updating and displaying itself every 10 milliseconds, where a sprite is being rotated, but still stays where it was 10 millisceonds before.

EDIT: Tried to search Google beforehand, guess the wording for this question is kind of tricky because I found nothing helpful.

Thanks a lot :)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3150201
  • 1,901
  • 5
  • 26
  • 29
  • 1
    Supply a rotation anchor point... – MadProgrammer Dec 31 '13 at 21:02
  • Is it possible to do that without making an AffineTransform object? I don't see a reason to make one, the simple method I used should work fine, right? – user3150201 Dec 31 '13 at 21:06
  • Yes, supply the `rotate` method with an anchor point...personally, I prefer using `AffineTransform`, but that's just me... – MadProgrammer Dec 31 '13 at 21:10
  • For [example](http://stackoverflow.com/questions/20256183/bufferedimage-rotated-change-resulting-background/20256348#20256348) and [example](http://stackoverflow.com/questions/11911316/java-2d-rotation-in-direction-mouse-point/11911470#11911470) – MadProgrammer Dec 31 '13 at 21:28

2 Answers2

5

When you call the rotate method on a Graphics2D object, you're rotating the entire canvas by that angle around the origin. So, drawing a shape at (300, 250) after the rotation will draw it at whatever that point maps to if you started at (300, 250) and then rotated it 45 degrees around (0, 0).

You should use the other form of rotate that takes the angle as well as the x- and y-coordinates of the rotation pivot point, and pass your point (300, 250) into it. (Although, if you want to rotate around the center of the character or string, you'll need to adjust that point a bit.)

Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
  • So basically, I call rotate this way: g2d.rotate(30 * Math.PI/180, 300, 250); Where 300 and 250 are the coordinates of the pivot point. But if I want it more accurate, then I need to measure the dimensions of the object I'm rotating, come up with the center point of it, and set that as the pivot point. So let's say I got an image the size fo 10x10, and it's location on the screen is 250,250 (location of it's top-left point), then I would set the pivot point to be 260,260. Wrote all of this to make sure I understand correctly. Do I? – user3150201 Dec 31 '13 at 21:13
  • Almost — if you have an image that is 10x10 and it is drawn at 250x250, then you would need the pivot point to be 255x255 if you wanted to pivot around the center of the image. – Tony Allevato Dec 31 '13 at 21:15
  • Great, thanks a lot. Now I'm one step closer to creating the game I'm thinking of :) Cheers – user3150201 Dec 31 '13 at 21:22
1

The rotation is done around the origin, e.g. 0,0 of the coordinate system. If you want to rotate around a different point you need to translate the origin, rotate, and translate back, e.g.:

    g2d.translate(300, 250);
    g2d.rotate(45 * Math.PI/180);
    g2d.translate(-300, -250);
    g2d.drawString("B",300,250);

Of course you have to take the size of the object you want to rotate in account. This rotates around the point where drawString would paint, which is the left baseline of the string. Probably you would want to rotate around the center; then you have to measure the object before and add those values to the translation.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50