• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to rotate my arrow?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i want to rotate this shape using AffineTransform without changing its position and its size. i want the arrow to be in a straight line. how can i proceed??

I have use this but the place of the arrow is changing.


here is my code for arrow
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need an AffineTransform object because the Graphics2D object supports affine transforms already. Look at the link: rotate scale shear and transform are there already.
I think you ought to duplicate your graphics object. Don't use clone, but it has a create() method. Note that JComponent#paintComponent() copies the Graphics object so any changes you make in this method are not reflected in other components, but you probably want to confine the transforms to part of your painting codeThe Graphics object is an instance of the 2D class, so the cast should work.

Then you want to translate the object behind g2d to the location of the arrow, then rotate. Scale translate and rotate can usually be undone without difficulty but shear often cannot be undone completely; it leaves the graphics slightly distorted, which is why I suggest duplicating the object.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice rotate is overloaded so you can translate and rotate in a single method call.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't want to override my paint method because with the paint method i am drawing a number different shape. I want to be able to rotate my arrow in the method drawArrow itself.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you got a Shape interface?Now you can create an Arrow class which implements one of those interfaces.
You will have to override paintComponent somehow, otherwise you won't get anything to appear. Don't override paint(). Consider different names for those interfaces; if you search this forum you will find I have posted that sort of thing before and people said I was using poor names for my interfaces. And I have forgotten what they suggested
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find the disagreement about names here.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any way to do it withing my method drawArrow??
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends whether you want to write object‑oriented code or procedural programming.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i implement the interface it will affect my other code. i really want to be able to rotate the arrow within the method drawArrow itself. can you tell me how to do it please?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Goodbye object‑orientation (OO) then

Pass a reference to the Graphics object to the method with the arrow in. There are doubtless lots of other ways to do it. You can even do it in good OO style, as I showed you earlier.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to do the rotation about a point. In this case you can use:



I always have trouble figuring out what the translation should be. Normally when I play with Shapes I try to do the painting relative to (0, 0) to make the translation easier. You just rotate around the center point:



So now you have a general rotated Shape that you can paint anywhere. In your painting code you would now need to add:




Also, you may want to check out Playing With Shapes for some classes that might help make your code more reusable so you aren't always doing custom rotations/translations. To use these classes your code could be:



In this case the outline is painted differently. The arrow lines seem darker. I'm not sure why the difference. But for other complete Shapes you might find these classes easier to use.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually my problem is that i have an arraylist. In this arrayList i have a number of shapes stored in it, then i use for loop to draw these shapes according to the user choice ( just like paint) then at the end i have a number of methods to draw these object. so i cannot pass a graphic object in my drawArrow method because it will not be accessible.
 
Rob Camick
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i really want to be able to rotate the arrow within the method drawArrow itself



The easiest way to do this is to just draw the arrow properly the first time so you don't need to do any rotations.

It can't be that hard to figure out the points of the various lines.

For example to draw a arrow with a size of 50 you would use:



 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, i'm becoming mad with these points :P
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rob Camick and Campbell Ritchie for your precious comments, i have been able to figure it out
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

Please show us what you achieved.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic