Matrices in Java and Flash (Flex)

Ran into an interesting detail concerning differences between Flex and Java, having to do with their matrix representations.  If you are unfamiliar with Matrices, as I am, you can review Matrices and Transformations in Flash at http://www.senocular.com/flash/tutorials/transformmatrix/.  The detail is this:  Java and Flash have their second and third at the opposite to each other.  So, for example, in this matrix:

Matrix Picture

In Flash, you would say:

matrix = new Matrix(a, b, c, d, tx, ty);

and in Java, it look like:

matrix = new Matrix(a, c, b, d, tx, ty);

(Well, their matrix is spelled a little differently.  See the javadoc page at : http://java.sun.com/j2se/1.4.2/docs/api/java/awt/geom/AffineTransform.html

[  m00  m01  m02  ] 

[  m10  m11  m12  ]

 This was one thorny detail on top of the rest of the great fun of the iText library.  The other interesting piece is that the y coordinates are reversed.  The PDF was designed for print media, so the y starts at the bottom of the page, with increasing values as it goes up.  In Flash, it is the opposite.  The y origin is at the top of the screen, increasing in value as you go downward.  This is all in general, and default.  It is possible to apply a transformation to the PDF to reverse the Y values, and likewise with the Flash.

With the PDF, you can apply the following transofmation:

matrix(1.o, 0.0, 0.0, -1.0, 0.0,  h*)

using concatCTM most likely.  The d value reverses the direction of the y axis, and the h* is a special value which is on the overall height of the page.   Because once you reverse the y value, it is still originated at the bottom left.  But when it increases it goes off the page into oblivion.  The h* value moves the origin to the upper left hand corner of the page.
 

Leave a Reply