UserGuide

Vector Graphics

From Xojo Documentation

A vector graphic (as opposed to a bitmap graphic) is composed entirely of primitive objects — lines, rectangles, text, circles and ovals, and so forth — that retain their identity in the graphic. They can be resized to display at any size and do not pixelate or otherwise “decompose” like bitmap graphics can. The Object2D class is the base class for all the classes that create primitive objects, which include: ArcShape, CurveShape, FigureShape, OvalShape, PixMapShape, RectShape, RoundRectShape and StringShape. Each of these classes allow you to specify borders, fill and fill colors, rotation, scale and positioning.

Drawing and Displaying a Vector Object

You draw a single vector object simply by instantiating it and specifying its properties. For example, the following code defines a RoundRectShape:

Dim r As New RoundRectShape
r.Width = 120
r.Height = 120
r.Border = 100
r.BorderColor = RGB(0, 0, 0) // black
r.FillColor = RGB(255, 102, 102)
r.CornerHeight = 15
r.CornerWidth = 15
r.BorderWidth = 2.5

The only problem with this is that the shape doesn’t appear anywhere. It’s just “defined” — ready for your use. You need to add a command to draw the vector object in another object. As you learned in the previous section, a Canvas is a great place to draw stuff.

Use the DrawObject method of Graphics to draw Vector graphics. Adding DrawObject to the above code and adding it to the Paint event of a Canvas control:

Dim r As New RoundRectShape
r.Width = 120
r.Height = 120
r.Border = 100
r.BorderColor = RGB(0, 0, 0) // black
r.FillColor = RGB(255, 102, 102)
r.CornerHeight = 15
r.CornerWidth = 15
r.BorderWidth = 2.5
g.DrawObject(r, 100, 100)

Combining Vector Objects

You can also create composite vector graphics objects that are made up of several individual vector graphics objects. The composite object is a Group2D object—it’s just a group of Object2D objects. Use the Append or Insert methods of the Group2D class to add individual vector graphic objects to the Group2D object. When you are finished, draw the object using one call to the DrawObject method.

To illustrate this, take the above code and add a second RoundRectShape object offset from the original by 20 pixels. Use Append to add the two RoundRectShapes to the Group2D object and then draw it in the Canvas:

Dim r As New RoundRectShape
r.Width = 120
r.Height = 120
r.Border = 100
r.BorderColor = RGB(0, 0, 0) // black
r.FillColor = RGB(255, 102, 102)
r.CornerHeight = 15
r.CornerWidth = 15
r.BorderWidth = 2.5

Dim s As New RoundRectShape
s.Width = 120
s.Height = 120
s.Border = 100
s.BorderColor = RGB(0, 0, 0) // black
s.FillColor = RGB(255, 102, 102)
s.CornerHeight = 15
s.CornerWidth = 15
s.BorderWidth = 2.5
s.X = r.X + 20
s.Y = r.Y + 20

Dim group As New Group2D
group.Append(r)
group.Append(s)
g.DrawObject(group, 100, 100)

Combining Bitmap and Vector Graphics

Object2D Text Rotation

You can combine both bitmap and vector graphics by first loading the bitmap graphic into a vector object using the PixmapShape class. You then group this with your other vector objects to create a single image. This example combines a bitmap graphic that was added to the project with a StringShape:

Dim px As PixmapShape
px = New PixmapShape(PictureName)

Dim s as StringShape
s = New StringShape
s.Y = 70
s.Text = "Hello, world!"
s.TextFont = "Helvetica"
s.Bold = True
Dim d as New Group2D
d.Append(px)
d.Append(s)
g.DrawObject(d, 100, 10)
Saving and Loading Vector Graphics

Vector graphics can be saved using the Save method of the Picture class. Vector graphics are stored as PICT files on macOS and as EMF files on Windows.

fa-info-circle-32.png
Saving and Loading vector graphics is not supported on Linux.

Since you need a Picture object to call save, you should draw your vector graphics to a picture first rather than directly to the Canvas. This gives you the Picture that you can save and allows you to also draw the picture to the Canvas if needed. In this example, SavePicture is a Picture property on the window:

Dim r As New RoundRectShape
r.Width = 120
r.Height = 120
r.Border = 100
r.BorderColor = RGB(0, 0, 0) // black
r.FillColor = RGB(255, 102, 102)
r.CornerHeight = 15
r.CornerWidth = 15
r.BorderWidth = 2.5

SavePicture.Graphics.DrawObject(r, 100, 100)
g.DrawPicture(SavePicture, 0, 0) // Draw SavePicture in the Canvas

To save this picture, add a button called Save to the window and use this code:

Dim file As FolderItem
If TargetMacOS Then
file = GetSaveFolderItem("", "vector.pict")
Else
file = GetSaveFolderItem("", "vector.emf")
End If

If file <> Nil Then
SavePicture.Save(file, Picture.SaveAsDefaultVector)
End If

To load a vector image, you use the OpenAsVectorPicture method of FolderItem. This code prompts the user to select a vector image and then displays it in an ImageWell:

Dim file As FolderItem
file = GetOpenFolderItem("")
If file <> Nil Then
Dim p As Picture
p = file.OpenAsVectorPicture
If p <> Nil Then
VectorWell.Image = p
End If
End If

Example Projects

  • Examples/Graphics and Multimedia/Obj2DTextRotation
  • Examples/Graphics and Multimedia/VectorGraphics

See Also

Object2D class; UserGuide:Framework topic