How to Use a Matrix to Transform Objects in VB.NET

The Visual Basic .NET (VB.NET) Matrix class contains a Translate method that can help you transform a 2D drawing such as an ellipse or a rectangle. This lets you move a GraphicsPath object across the screen without having to create multiple versions of the object. A GraphicsPath object contains a series of curves and lines. Utilize the GraphicsPath object's Transform method and a Matrix object's Translate method to transform your object.

Instructions

    • 1

      Open your Visual Basic .NET file in an editor, such as Microsoft Visual Studio.

    • 2

      Create a new subroutine to perform the matrix transformation and pass it the "PaintEventArgs" data by adding the following code in your file:

      Public Sub TransformRectangle(ByVal e As PaintEventArgs)

    • 3

      Create new GraphicsPath, RectangleF and Matrix objects by adding the following code at the top of the subroutine:

      Dim gpath As New GraphicsPath
      Dim rectf As RectangleF = New RectangleF(0,0,75,75)
      Dim transmatrix As New Matrix

      The rectangle will appear in the top-left corner at the coordinates (0,0) with a width of 75 units and a height of 75 units.

    • 4

      Draw the initial position of the rectangle on the screen with the following code:

      e.Graphics.DrawPath(Pens.Black, gpath)

    • 5

      Translate and then transform the object with this code:

      transmatrix.Translate(200,0)
      gpath.Transform(transmatrix)

    • 6

      Draw the transformed rectangle on the user's screen with the code:

      e.Graphics.DrawPath(Pens.Black, gpath)

      End Sub

      The transformed rectangle will appear 200 units to the right of the initial rectangle.

    • 7

      Save your VB.NET file, and compile and run the program to view the transformed rectangle.

Related Searches:

References

Comments

Related Ads

Featured