• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the rotated rectangle points

 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends
i have a question related to rectangle in java
I had drawn a rectangle with x,y,w,h coordinates and i am rotating that rectangle to 45 degree without changing the center
and i need the coordinate points of the new rotated rectangle .
And using this rotated rectangle when my mouse is right clicked should display a message. how to do this

This is my code



please help me
 
Piet Souris
Rancher
Posts: 1638
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Prajna,

strange: in the forum it says that this post hs 3 replies, yet I see none. Well anyway.

I would advise to have a look at the AffineTransform class in the Java API from Oracle.
Also have a look at the Shape class. If you combine these two, you get Java
to do a lot of work that you are doing manually now.

Do you need the coordinates? Or do you only want to see the rotated rectangle?

If you only want to rotate your rectangle, then all you have to do is to look at
a suitable method in the Graphics2D class. you will see a method there
('rotate') that allows you to set a rotation while specifying the center of rotation.
Then draw your rectangle in the normal way.

If you want to know the coordinates of the rotated rectangle, then it is easiest
to define your rectangle with corner points relative to this origin. Create an
AffineTransform that rotates to a given angle. You will see that an AffineTransform
also has a method that translate a point to a new point. That method is
'deltaTransform'.

Yes, it may sound a bit complex, but if you get the hang of it, it will become
second nature.

And of course, you can always calculate these coordinates by hand.

Greetz,
Piet
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir i need the coordinates to check the points when the mouse is clicked inside that rectangle. i need the rotated rectangle points .

Now i am getting the points of the original rectangle after rotation i am not getting the rotated xy coordinates . Please help me
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote: . . .
strange: in the forum it says that this post hs 3 replies, yet I see none. Well anyway.
. . .
There is a know error which turns out to be very difficult to fix.
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir my rectangle is rotating .i need the co ordinates of the rotated rectangle so that i can check and display the mouse click condition to display a message

 
Piet Souris
Rancher
Posts: 1638
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Piet Souris wrote: . . .
strange: in the forum it says that this post hs 3 replies, yet I see none. Well anyway.
. . .
There is a know error which turns out to be very difficult to fix.

Okay. I was afraid that some replies were lost, and that I was answering to an already
solved problem. Thanks for the reply.

Greetz,
Piet
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello problem is not solved please help me... please how to get the xy coordinates of the rotated rectangle
 
Piet Souris
Rancher
Posts: 1638
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Prajna,

well, let the pc do the hard work, in stead of you. You will have to do
some study, though, but if you like a bit of Linear Algebra, then that
should not be any problem.

Here goes.

If you look at the Graphics2D class, you see a method called 'rotate',
that lets you specify both the rotation angle and the rotation center.

Use a Rectangle2D.xxxx, in stead of a normal Rectangle.
A Rectangle implements the Shape interface, and as such it has the methods
'CenterX' and 'CenterY'. That will give you easily the center of rotation.

Now, invoke the method 'g2d.rotate(angle, x, y), and then extract the
AffineTransform from it. There is, again, a method in the g2d that
does this.

Having this AffineTransform (which is also easy to create "by hand"),
it has a method that creates a Shape, given some input shape.
Now, this input shape is your originale rectangle.

Having now this transformed shape, it has the method 'contains'.
Use the mouse coordinates of your JPanel for the coordinates
to inspect.

This way, you let the pc do all the hard work for you.

And if you really want to know the coordinates of the corner points:
your AffineTransform has methods that will calculate these
for you.

It really is that simple, but it takes a bit of time to get acquainted
to the matter.

Greetz,
Piet
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my edited code
 
Piet Souris
Rancher
Posts: 1638
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not have Java available at the moment, so I cannot test it.
But you can, so let me know.

Greetz,
Piet

 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any others please help me.. i am not able to display the message when the mouse is clicked inside a rotated ellipse.please suggest me .
fed of by thinking so much please help me thanks in advance
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me Mr.Piet Souris
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PLease help me to solve this ....Thanks in advance
 
Piet Souris
Rancher
Posts: 1638
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You make it sound as if it was a matter of life & death...

First of all: do you see two ellipses in your panel?

Then: it is the first time I see someone adding a
MouseListener to a panel in its paint method. I don't know
the effect of that.

You do not put any value to e1, you only draw a rotated
ellips.

So: first thing to do is:
create a proper constructor for your Panel, that initializes e and e1,
and adds a MouseListener to it.

Then, do not override the 'paint' method. Use 'paintComponent'
instead.

And, foremost: drop the test for 'isMetaDown'. Just see if you get
that ConfirmDialog when there has been a click.

As said, I am at work, and I do not have Java at my disposal.
So until this evening (in about 6 hours) I am unable to help you
with any practical test. If that is too log for you, I hope someone else
will step in. In the mean time: try many things, you know the ideas
that I wrote.

Greetz,
Piet


 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to your advise i had changed my code but still same problem
 
Swastik Dey
Rancher
Posts: 1686
7
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isMetaDown will trigger only if it is a right button click , right?
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya exactly
 
Swastik Dey
Rancher
Posts: 1686
7
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So,

What exactly are you trying to achieve?
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above code i am rotating a ellipse and collecting its coordinates .
when the mouse right click condition is occured i need to display the message only when the mouse is inside the rotated ellipse
in my case when i click in the original ellipse(without rotation) it will display the message so wat to do.
 
Piet Souris
Rancher
Posts: 1638
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this without testing, so it might contain the odd error.
Also you must do the imports.

But try this:



Greetz,
Piet
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Mr. Piet Souris ..It is working fine.
But still i have a doubt what was the mistake i made in that code ..
if possible explain me whenever you are free.

Thank you
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain me what was the problem in my code
Please Thanks in advance
 
Piet Souris
Rancher
Posts: 1638
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Prajna,

well, basically there is not much wrong with your program,
although there are some important things to remark.

I will do that when I get home, that'll be in about 5 hours.
Have patience!

Greetz,
Piet
 
Piet Souris
Rancher
Posts: 1638
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as promised...

There was basically not much wrong with your latest program, it was just
a matter of clicking in the right figure. BUT

NEVER create the variables and NEVER add a MouseListener in your paintComponent method!

These things belong in a proper constructor, and in the code below that is just what
I do.

Then, in your code, you created no less than 4 figures, so it was very hard to
determine on which you actually clicked. I draw all four of them, in different
colors, with a JTextField above indicating which color is what figure.
Only figue 'e1' responds to a mouseclick, as was the intention.

Part of the confusion arises from the fact that the method 'getBounds2D' does NOT
give the circumference, but it gives a rectangle just big enough to contain completely
the shape.

But if you run this program, you see something remarkable.
The green rectangle is the 'getBounds2D' rectangle of the rotated ellipse. As you
can see, this rectangle is too big! I'm not sure what causes this.
Anyway: your e1 is the ellipse from this too wide rectangle.
But the mouse clicks are accurate. Now try to detect
clicks in figure s, the blue one.

I hope you enjoyed this thread. And as I promised in my first reply:
we did let the computer do all the hard work!

Greetz,
Piet

 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your advice and
Thanks for the patience to explain me the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!