Flipping a vector drawing in Java

An ongoing project required some 2D vector paths to be mirrored. I was programming in Java and using Area object to represent these paths. Though the obvious thing was to do this myself, I turned to the libraries for help.  I have not used AffineTransform before, but it seemed the right tool for the job. Unfortunately, either my understanding is shrinking (which may well be the case) or the existing documentation is not good enough.

After trying several matrices from different posts online I realized I was not getting any closer to a good solution. The main problem is that while you can mirror an Area object with just one matrix (-1,0,0,1,0,0) you can't avoid the translation that will happen at the same time.

The solution to my problem ended up being quite simple (I wanted to mirror the image in the X axis).

// object a is the area I want to flip
AffineTransform at = new AffineTransform();
at.scale(-1,1);
Rectangle r = a.getBounds();
at.translate(-( r.width + 2 * r.x ), 0.0);
a.transform(at); 

This way I create an affine transform that using scale method will create the mirrored image that later will be restored to its original location by means of a translation. I wish I had found something like this before.

Comments

Jayson James said…
Actually I have a passion on it.I like Vector Drawing very much.
Sumon Chowdhury said…
I think its really very helpful. thanks for sharing this.
Rustam Ali said…
This comment has been removed by a blog administrator.
Md.Rasel said…
Your shared post is informative and very helpful and writing style also so excellent.
java is very heard language, thank you so much for your share with me
Thank you, I did enjoyed reading it

Regard

Clipping path Service
Sharlin Smith said…
Drawing is the good art human life who trying to show their creativity. For good result you need to follow some instructions and passion on your life. I think I got the possibility from some art and design company. You can check to clipping path service company
So much informative blog.Always got the best tips from here.Many many thanks.
Nice post.Thanks for sharing with us this informative post
Clipping path service
your post really helpful, thanks for sharing.
The information is very good and very useful. I really like it and I am also waiting for the post information. And do not forget my friends visit my website :) thank you

clipping path service provider
clipping path service
Background Removal
Image clip said…
Thanks for ones marvelous posting! I really enjoyed reading it, you must be a great author. I will be sure to bookmark your blog and definitely will come back sometime soon. I want to encourage that you continue your great work, have a nice morning!
clipping way said…
Thanks for nice information. I would love to learn more vector graphics .

Popular posts from this blog

VFD control with Arduino using RS485 link

4xiDraw: Another pen plotter

One Arduino controlling two brushless DC motors