i am drawing two rect in paint method. 1st rect i want to rotate it but 2nd rect i dont want to rotate it. the code i have should rotate only 1st rect but the problem is that when it rotate it missup the x, y postion of rect. this might has to do with translate but not sure. how can i fix tis?

  ///paint method
     Graphics2D g2d = (Graphics2D) g;

        AffineTransform saveAT = g2d.getTransform();

          g2d.translate(x+(width/2), y+(height/2)); 
          g2d.setTransform(saveAT); 

            g2d.rotate(Math.PI/4);

            g.drawRect(...);   //rotate this rect

            g2d.setTransform(saveAT); //rotate back?

            g.drawRect(...);  //dont rotate this rect

You are basically undoing your translation on line 7 by restoring the original transform. You are rotating the original context.

i removed line 7. still i am runing into same problem. do you think this is bc of what i put insdie of translate() method?

  ///paint method
     Graphics2D g2d = (Graphics2D) g;
        AffineTransform saveAT = g2d.getTransform();

          g2d.translate(x+(width/2), y+(height/2));   //set orgin in rect middle
          g2d.rotate(Math.PI/4);                      //rotate

            g.drawRect(...);   //rotate this rect

            g2d.setTransform(saveAT); //rotate back?

            g.drawRect(...);  //dont rotate this rect

If you translate your origin to where you want the center of the rectangle to be, are you drawing the rectangle relative to the new origin location?

If you have a rectangle centered at say (2,2) and you translate to that point and draw the rectangle with the same (x,y) values, you will now have a rectangle centered at (4,4) because you have shifted the entire coordinate system to a new location.

i am not sure about orgin. i am just trying to rotate the 1st rectangle and not change its x, y coordinate.

Since you've moved the origin to the middle of where you want the rectangle, draw the rectangle centered about the new origin.

Old rect:

g2d.drawRect(100,100,10,10);

translating to center of rect and drawing the same shape:

g2d.translate(105,105);
g2d.drawRect(-5,-5,10,10);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.