rotating a single object without rotating the entire screen with Graphics2D

Hey I’m trying to program a 2d game, and in the game you can throw balls at other players. i want the ball to rotate though as it flys through the air, the problem is that when i do g2d.rotate(Math.toRadians(rotate)); the ball, the player, the background, the everything thats drawn is rotated. can anyone offer some suggestions on how to rotate one singular object? Thanks!

Going through my old threads … surprised no one got back to you on this. Use push/popTransform to save and reset the state of your modelview matrix (that is, the current rotation/translation/shearing/scaling states)


g2d.pushTransform();
... do your rotates/scales/etc
g2d.popTransform();

You can stack transforms up to 16 deep, though I really don’t recommend going that crazy with it.

popTransform() ??
is it a class that he (i) should create or is it included in the Java2D ? cause i don’t have it

Make your own methods, something like so:


public class TransformStack {
  private AffineTransform[] transforms;
  private Graphics2D gr;
  private int depth;
  public TransformStack(int depthMax, Graphics2D graphics) {
    gr = graphics;
    transforms = new AffineTransform[depthMax];
    while (--depthMax >= 0) {
      transforms[depthMax] = new AffineTransform();
    }
  }
  public void push() {
    transforms[depth++].setTransform(gr.getTransform());
  }
  public void pop() {
    gr.setTransform(transforms[--depth]);
  }
}

I don’t think matrix stack is neccessary for simple things…
He can do it like this.


AffineTransform t = g2.getTransform();

for(Something object : yourObjects) {
    g2.translate(object.x, object.y);
    g2.rotate(object.rotation);
    g2.drawWhatever(object.whatever);
    g2.setTransform(t);
}

That works nicely as well! (that’s what I do myself with Java2D) He was asking about those push/pop methods so I thought I would implement them =P

For some bizarre reason I read Graphics2D as … Slick2D. :emo:

I think I’ll go back to lurking now. :stuck_out_tongue:

Gosh you’re always starting shit!

…for j2d I use this for its simplicity.


public void render(Graphics g)
{
    Graphics2D g2d = g.create(); //cast this to a g2d object
    //do all normal rotation/blending/drawing stuff here
    g2d.dispose();
}


May not be super fast but then neither is j2d.