File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Rotating Squares in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Rotating Squares in java" Watch "Rotating Squares in java" New topic
Author

Rotating Squares in java

Edmond Corrola
Greenhorn

Joined: May 23, 2008
Posts: 15
I've tried rotating squares in java with no success. I've attempted to use Affine Transform, and even just doing it mathematically. Nothing has worked so far. Can someone point me towards a good example online? Or maybe post a small code snippet showing me how to do it properly? Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
Not exactly what you're asking about, but check out http://faq.javaranch.com/java/MiceProblem

It draws curves by repeatedly changing angles between successive lines.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Probably best to put the square into its own class, and pass a Graphics object to the draw method.
Copy the Graphics object with its create() method.
Cast it to Graphics2D.
Call its rotate method.

See whether that works; rotate does in fact apply an AffineTransform, but is easier to use.
Edmond Corrola
Greenhorn

Joined: May 23, 2008
Posts: 15
I'm trying to rotate a rectangle. So imagine drawing a rectangle at x and y coordinates, than rotating it by some value of degrees. It's like putting a picture of a rectangle on the wall, sticking a tack in the bottom left corner, and moving the rectangle around the tack (rotating it).

As for graphics 2D, I saw the rotate method in the API, but unfortunately I have other things drawn on the graphics object, so by rotating the graphics object, i essentially will ruin the coordinates of everything else i think.

I wrote a primitive algorithm, but its off when rotating the rectangle. Maybe you know something about math and you can see whats wrong?

Takes an original rectangle and rotates it based on the "angle" parameter. Redraws it as two triangles that form the new rectangle.

[ October 13, 2008: Message edited by: Edmond Corrola ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Much too complicated.

You want something more likeIn the drawYourself() method you copy the Graphics object with its create() method, then cast the copy to Graphics2D. Set up a rotation field in the Square class, which you can rotate the Graphics2D object with.
The real maths is much more complicated; it involves putting the locations into a column matrix and multiplying by that matrix by a square matrix. Go through the AffineTransform class API and it shows you what the matrices are.

You do realise that the rotation is around the (0, 0) point, the "origin," so you may need to move the origin with the translate method before rotating. If you have x y coordinates for your square you can translate the Graphics by x and y (or maybe it's -x -y; you will find out soon enough!).

Hiding inside the Square class, have a Rectangle2D object and use the appropriate method of Graphics2D to draw that. You can change that to Ellipse2D and rotate a circle if you wish. I know it works; I had to do an assignment for my MSc which included such transformations.
Edmond Corrola
Greenhorn

Joined: May 23, 2008
Posts: 15
Okay I think I get what your saying. I wasn't able to implement a solution yet but I'll work on something soon and post my result. Thanks!
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Edmond Corrola:
As for graphics 2D, I saw the rotate method in the API, but unfortunately I have other things drawn on the graphics object, so by rotating the graphics object, i essentially will ruin the coordinates of everything else i think.


The create() method returns another Graphics object. Any rotations/translations you make to that one will not affect the original.

It's even reasonably fast, though not necessarily faster than unrotating/untranslating.


bitguru blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Agree, Brian.

I prefer to use create() rather than rotate(x) . . . rotate(-x) even though there may be a delay. This is because the last time I tried that trick I found transforms interfering with later transforms. Rotation and translation were all right, but when I combined rotation with shear, there was a slight distortion for all objects added subsequently. That was an MSc assignment; shortly after I handed it in I realised taking a copy of the Graphics object would have worked better than transform...untransform.
[ October 14, 2008: Message edited by: Campbell Ritchie ]
Alexandr Racheev
Greenhorn

Joined: Oct 14, 2008
Posts: 2
maybe this example will help you


[ October 14, 2008: Message edited by: Alexandr Racheev ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Campbell Ritchie:

I prefer to use create() rather than rotate(x) . . . rotate(-x) even though there may be a delay. This is because the last time I tried that trick I found transforms interfering with later transforms. Rotation and translation were all right, but when I combined rotation with shear, there was a slight distortion for all objects added subsequently. That was an MSc assignment; shortly after I handed it in I realised taking a copy of the Graphics object would have worked better than transform...untransform.


Seconded. This is pretty well known in computer graphics and computational physics circles. Small roundoff errors accumulate more quickly than you'd think over many cycles. It's always better to work relative to the reference coordinates on each frame, rather than repeatedly modifying the previous frame's data.


[Jess in Action][AskingGoodQuestions]
Edmond Corrola
Greenhorn

Joined: May 23, 2008
Posts: 15
Turns out I can't use AffineTransform or the built in rotate function. Not even supposed to use my own rectangle class. What I was doing before was on the right track (making my own rotation algorithm. I ended up working up a final solution for my specific problem. If you guys are interested in the algorithm I could post it.
[ October 14, 2008: Message edited by: Edmond Corrola ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Yes, please do; if you have managed something that complicated you might as well show it off

And well done.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rotating Squares in java