Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes How to Rotate with AffineTransform and keep the orignal coordinates ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to Rotate with AffineTransform and keep the orignal coordinates ?" Watch "How to Rotate with AffineTransform and keep the orignal coordinates ?" New topic
Author

How to Rotate with AffineTransform and keep the orignal coordinates ?

M. othello
Greenhorn

Joined: Feb 02, 2011
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

    Joined: Feb 02, 2011
    Posts: 5
    Thanks for the quick unswer
    I'am gonna try that and give the feed back
    M. othello
    Greenhorn

    Joined: Feb 02, 2011
    Posts: 5
    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

    Joined: Feb 02, 2011
    Posts: 5
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to Rotate with AffineTransform and keep the orignal coordinates ?
     
    Similar Threads
    return problems
    refresh canvas, designer problem?
    Collision detection with rotating shapes
    Moving and resizing a rectangle around
    What kind posible exception thrown