Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rotating Squares in java

 
Edmond Corrola
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe this example will help you


[ October 14, 2008: Message edited by: Alexandr Racheev ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Edmond Corrola
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, please do; if you have managed something that complicated you might as well show it off

And well done.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic