8

I need to create 3d depth effect for my image. Number 1 is what I have and number 2 is shape where I want to transform number 1. So is there any method for that in Java standard graphics libraries or some other open source libraries?

alt text

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
newbie
  • 24,286
  • 80
  • 201
  • 301
  • Maybe [this](https://stackoverflow.com/questions/7366099/skew-or-distort-image-object-in-java/52187377#52187377) is of help. It uses no external libraries, only JavaFX. – Matthias Braun Sep 05 '18 at 14:23

2 Answers2

5

This can not be done using the AffineTransform class. See Wikipedia article on affine transformation:

In general, an affine transformation is composed of linear transformations (rotation, scaling or shear) and a translation (or "shift").

What you need is some form of perspective transform. From http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/PerspectiveTransform.html

A perspective transformation is capable of mapping an arbitrary quadrilateral into another arbitrary quadrilateral, while preserving the straightness of lines. Unlike an affine transformation, the parallelism of lines in the source is not necessarily preserved in the output.

From http://answers.google.com/answers/threadview/id/515829.html

The Java Advanced Imaging API allows you to easily perform perspective transform.

As in Java2D and Java3D, these routines are optimised, they are not run in the usual java interpreted manner - so they are very fast as well.

The JAI is downloadable from

http://java.sun.com/products/java-media/jai/downloads/download-1_1_2.html

You can find info on how to run perspective transform in:

http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Thanx for help, but I didn't find any example how to use JAI perspective transformation, and it's JavaDocs are pretty uninformative... – newbie Jul 08 '10 at 18:06
  • Someone helped me out with the math portion of this question over here http://stackoverflow.com/questions/4217370/how-can-i-project-an-arbitrary-plane-identified-by-4-points-unto-a-2d-plane – Scott Jan 28 '11 at 02:25
0

If you want to do a lot of fast drawing in 3D then I'd suggest looking into a 3D rendering solution such as OpenGL / JOGL.

If it is just a quick one-off transformation then you can simulate this pretty easily by

  • Looping over all the lines in 2.
  • Scaling the corresponding line in 1. and drawing it over the line in 2, stretching it to the right proportion (you'll need some basic maths to work this out)
mikera
  • 105,238
  • 25
  • 256
  • 415