Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Rotate a figure around the center
Rotate a figure around the center [message #461158] Sun, 18 September 2005 18:19 Go to next message
Jens Bartelheimer is currently offline Jens BartelheimerFriend
Messages: 44
Registered: July 2009
Member
Hello,

I like to rotate a Figure depending on degree.

I have a connection with a symbol in the middle and depending on the
direction of the connection I have to align the symbol.

I have try it with gc.rotate() but unfortunately this rotates the figure
not around the center. Is there an other way to rotate a figure.

Here is my current, not suitable code:

Image img = new Image(display, 31, 31);
GC gc = new GC(img);
Transform tr = new Transform(display);
tr.rotate(45);
gc.setTransform(tr);

gc.drawLine(0,15,30,15);
g.drawImage(img,new Point(r.x+10,r.y-10));
Re: Rotate a figure around the center [message #461163 is a reply to message #461158] Mon, 19 September 2005 09:52 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex BlewittFriend
Messages: 946
Registered: July 2009
Senior Member
The normal way is to translate the image, so that the centre of the image is where you want to rotate around, do the rotation, and then translate back again. So if you have a 100x100 image, and you want to rotate around the centre, you translate it (-50,-50), rotate (45) and then translate it back again (50,50).

However, this will result in some image data being pushed outside out of the drawable area, and I'm not sure how the Eclipse libraries handle that having never tried it myself...
Re: Rotate a figure around the center [message #461194 is a reply to message #461163] Mon, 19 September 2005 13:17 Go to previous messageGo to next message
Jens Bartelheimer is currently offline Jens BartelheimerFriend
Messages: 44
Registered: July 2009
Member
Alex Blewitt wrote:
> The normal way is to translate the image, so that the centre of the image is where you want to rotate around, do the rotation, and then translate back again. So if you have a 100x100 image, and you want to rotate around the centre, you translate it (-50,-50), rotate (45) and then translate it back again (50,50).
>
> However, this will result in some image data being pushed outside out of the drawable area, and I'm not sure how the Eclipse libraries handle that having never tried it myself...

Thanks for your answer. I understand your idee but I don't know how I
translate the image?

I have try it with gc.translate, but this do not bring the right
results. Further I do not understand how I can translate my image back
again because the translater is already set to gc. Or is this possible
with the Graphics g object?

Rectangle r = getBounds().getCopy();
Display display = PlatformUI.getWorkbench().getDisplay();
Image img = new Image(display, 30, 30);


GC gc = new GC(img);
Transform tr = new Transform(display);
tr.translate(-15,-15); //??????
tr.rotate(45);

gc.setTransform(tr);


gc.drawLine(0,15,30,15);
g.translate(+15,+15);
g.drawImage(img,new Point(r.x,r.y));
Re: Rotate a figure around the center [message #461257 is a reply to message #461194] Mon, 19 September 2005 17:08 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex BlewittFriend
Messages: 946
Registered: July 2009
Senior Member
I'm not sure why you're doing some transforms on the Transform, and why you're doing others on the Graphics Context.

I believe (but do not know for sure) that you can do the transforms on the same Transform instance. So you would have:

Transform tr = new Transform(display);
tr.translate(startX,startY); // centre of image
tr.translate(15,15); // half the width/height of image
tr.rotate(45); // degrees to rotate
tr.translate(-15,-15); // half the width/height of image
gc.drawImage(img,0,0); // draw at 'notional' zero
gc.setTransform(tr); // associate with the transform

Hope that helps,

Alex.
Re: Rotate a figure around the center [message #461275 is a reply to message #461257] Tue, 20 September 2005 09:44 Go to previous message
Jens Bartelheimer is currently offline Jens BartelheimerFriend
Messages: 44
Registered: July 2009
Member
Yes, your answer helps.

Here my solution:
Image img = new Image(display, 16,16);
Image img2 = new Image(display, "c:\\test.gif");

GC gc = new GC(img);
Transform tr = new Transform(display);
tr.translate(8,8);
tr.rotate(60);
tr.translate(-8,-8);
gc.setTransform(tr);
gc.drawImage(img2,0,0);

I am not sure if I have to work with to images but it works.

Jens

Alex Blewitt wrote:
> I'm not sure why you're doing some transforms on the Transform, and why you're doing others on the Graphics Context.
>
> I believe (but do not know for sure) that you can do the transforms on the same Transform instance. So you would have:
>
> Transform tr = new Transform(display);
> tr.translate(startX,startY); // centre of image
> tr.translate(15,15); // half the width/height of image
> tr.rotate(45); // degrees to rotate
> tr.translate(-15,-15); // half the width/height of image
> gc.drawImage(img,0,0); // draw at 'notional' zero
> gc.setTransform(tr); // associate with the transform
>
> Hope that helps,
>
> Alex.
Previous Topic:Custom widget
Next Topic:Query for positioning the views
Goto Forum:
  


Current Time: Thu Apr 25 13:15:24 GMT 2024

Powered by FUDForum. Page generated in 0.02858 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top