• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Rotate with AffineTransform and keep the orignal coordinates ?

 
M. othello
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every Body

I'am a command pattern to rotate and translate shapes on Java Swing
The translation and the rotation work well separatly, but when I do a 60 deg. rotation and then the translation, the translation follow the new rotated coordinate.
Which means if I drag the mouse, the shape moves with 60 deg gap from the mouse mouvement vector
is there any easy solution ? please help, I'am hitting a wall here

My execute method for the rotation



My execute method for the translation



Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 49804
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search my previous posts about rotation; I have had the same problem myself before now.

As far as I can remember, you might try the following:
  • Copy the Graphics object or the Graphics2D object. I don't think you use clone() but create() on it.
  • Translate it so its centre is equal to the centre of the shape you wish to rotate
  • Apply the affine transform by calling the Graphics2D#rotate() method.
  • Discard the rotated Graphics2D object.
  •  
    M. othello
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the quick unswer
    I'am gonna try that and give the feed back
     
    M. othello
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi again

    Well I wanted to implemente your solution, but the architecture of the application doesn't allow it, Let me explain ...
    The application use MVC, so the class shape (which is by the way a custom shape class and not the java api class) doesn't have direct access to the graphic object.
    The shape change it's state and call notifyObeservers() and the view update it self...
    I have no clue how to solve this issue. is there any other path I can explore

    Thanks


    This is the Shape Class

     
    M. othello
    Greenhorn
    Posts: 5
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I was suggested a simplier solution, and it worked like a charme
    Instead of changing the rotation methode, I should've change the translation metode in order to take account of the orientation of the shape.

    This is the code for the translation:


    public void execute() {
    Iterator iter = objects.iterator();
    Shape shape;
    while(iter.hasNext()){
    shape = (Shape)iter.next();
    mt.addMememto(shape);
    AffineTransform t = new AffineTransform();
    System.out.println("Translation x :"+x + ", Translation y :"+y);
    t.translate(x,y);
    t.concatenate(shape.getAffineTransform());
    shape.setAffineTransform(t);
    }
    }


    Thanks guys
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic