Class AffineTransform is used for Coordinate Transformation on two-dimensional plane. Coordinates can be translated, scaled, or rotated. The following example shows the usage of coordinate transformation.
[Csharp]
Private void Transform ()
{
Path path;
/* The first matrix */
AffineTransform matrix1 = new AffineTransform ();
/* The second matrix */
AffineTransform matrix2 = new AffineTransform ();
/* The third matrix */
AffineTransform matrix3 = new AffineTransform ();
/** Colors */
Color blackColor = new Color (0xff000000, false );
Color redColor = new Color (0xffff0000, false );
Color greenColor = new Color (0xff00ff00, false );
Color blueColor = new Color (0xff0000ff, false );
Color fillColor = new Color (0x4f0000ff, true );
/* Define the path */
Path = new Path ();
Path. MoveTo (50, 0 );
Path. LineTo (0, 0 );
Path. LineTo (0, 50 );
/* Define the matrix1 as "translate (50, 50 )"*/
Matrix1.Translate (50, 50 );
/* Define the matrix2 as "translate (50, 50) + rotate (-45 )"*/
Matrix2 = new AffineTransform (matrix1 );
AffineTransform m = new AffineTransform ();
MB. Rotate (-45 * Math. PI/180.0, 0, 0 );
/* Concatenates the m to the matrix2.
* [Matrix2] = [matrix2] * [m];
*/
Matrix2.Concatenate (m );
/* Define the matrix3
* "Translate (50, 50) + rotate (-45) + translate (-20, 80 )"*/
/* Copy the matrix2 to the matrix3 */
Matrix3 = new AffineTransform (matrix2 );
M = new AffineTransform ();
M. Translate (-20, 80 );
/* Concatenates the m to the matrix3.
* [Matrix3] = [matrix3] * [m]
*/
Matrix3.Concatenate (m );
// Clear the canvas with white color.
Graphics2D. Clear (Color. White );
Graphics2D. AffineTransform = (matrix1 );
SolidBrush brush = new SolidBrush (fillColor );
Pen pen = new Pen (redColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
Graphics2D. AffineTransform = (matrix2 );
Pen = new Pen (greenColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
Graphics2D. AffineTransform = (matrix3 );
Pen = new Pen (blueColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
}
Private void Transform ()
{
Path path;
/* The first matrix */
AffineTransform matrix1 = new AffineTransform ();
/* The second matrix */
AffineTransform matrix2 = new AffineTransform ();
/* The third matrix */
AffineTransform matrix3 = new AffineTransform ();
/** Colors */
Color blackColor = new Color (0xff000000, false );
Color redColor = new Color (0xffff0000, false );
Color greenColor = new Color (0xff00ff00, false );
Color blueColor = new Color (0xff0000ff, false );
Color fillColor = new Color (0x4f0000ff, true );
/* Define the path */
Path = new Path ();
Path. MoveTo (50, 0 );
Path. LineTo (0, 0 );
Path. LineTo (0, 50 );
/* Define the matrix1 as "translate (50, 50 )"*/
Matrix1.Translate (50, 50 );
/* Define the matrix2 as "translate (50, 50) + rotate (-45 )"*/
Matrix2 = new AffineTransform (matrix1 );
AffineTransform m = new AffineTransform ();
MB. Rotate (-45 * Math. PI/180.0, 0, 0 );
/* Concatenates the m to the matrix2.
* [Matrix2] = [matrix2] * [m];
*/
Matrix2.Concatenate (m );
/* Define the matrix3
* "Translate (50, 50) + rotate (-45) + translate (-20, 80 )"*/
/* Copy the matrix2 to the matrix3 */
Matrix3 = new AffineTransform (matrix2 );
M = new AffineTransform ();
M. Translate (-20, 80 );
/* Concatenates the m to the matrix3.
* [Matrix3] = [matrix3] * [m]
*/
Matrix3.Concatenate (m );
// Clear the canvas with white color.
Graphics2D. Clear (Color. White );
Graphics2D. AffineTransform = (matrix1 );
SolidBrush brush = new SolidBrush (fillColor );
Pen pen = new Pen (redColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
Graphics2D. AffineTransform = (matrix2 );
Pen = new Pen (greenColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
Graphics2D. AffineTransform = (matrix3 );
Pen = new Pen (blueColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
}
Private void Transform ()
{
Path path;
/* The first matrix */
AffineTransform matrix1 = new AffineTransform ();
/* The second matrix */
AffineTransform matrix2 = new AffineTransform ();
/* The third matrix */
AffineTransform matrix3 = new AffineTransform ();
/** Colors */
Color blackColor = new Color (0xff000000, false );
Color redColor = new Color (0xffff0000, false );
Color greenColor = new Color (0xff00ff00, false );
Color blueColor = new Color (0xff0000ff, false );
Color fillColor = new Color (0x4f0000ff, true );
/* Define the path */
Path = new Path ();
Path. MoveTo (50, 0 );
Path. LineTo (0, 0 );
Path. LineTo (0, 50 );
/* Define the matrix1 as "translate (50, 50 )"*/
Matrix1.Translate (50, 50 );
/* Define the matrix2 as "translate (50, 50) + rotate (-45 )"*/
Matrix2 = new AffineTransform (matrix1 );
AffineTransform m = new AffineTransform ();
MB. Rotate (-45 * Math. PI/180.0, 0, 0 );
/* Concatenates the m to the matrix2.
* [Matrix2] = [matrix2] * [m];
*/
Matrix2.Concatenate (m );
/* Define the matrix3
* "Translate (50, 50) + rotate (-45) + translate (-20, 80 )"*/
/* Copy the matrix2 to the matrix3 */
Matrix3 = new AffineTransform (matrix2 );
M = new AffineTransform ();
M. Translate (-20, 80 );
/* Concatenates the m to the matrix3.
* [Matrix3] = [matrix3] * [m]
*/
Matrix3.Concatenate (m );
// Clear the canvas with white color.
Graphics2D. Clear (Color. White );
Graphics2D. AffineTransform = (matrix1 );
SolidBrush brush = new SolidBrush (fillColor );
Pen pen = new Pen (redColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
Graphics2D. AffineTransform = (matrix2 );
Pen = new Pen (greenColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
Graphics2D. AffineTransform = (matrix3 );
Pen = new Pen (blueColor, 4 );
Graphics2D. SetPenAndBrush (pen, brush );
Graphics2D. Draw (null, path );
}
Author: mapdigit