Graphics

From Xojo Documentation
Jump to: navigation, search


For web applications, see WebGraphics.

Class (inherits from Object)

Graphics class objects are used for drawing text, lines, rectangles, ovals, and pictures. Normally you use a graphics object in response to a Canvas.Paint event, but you can also perform direct drawing by using the Graphics property of a Canvas control. Alternatively, you can create vector graphics using the subclasses of the Object2D class. You cannot create a meaningful Graphics object via the New command.

On Windows, the Graphics class will use GDI+ if the Application.UseGDIPlus property is set to True.


Properties
AntiAlias Italic TextHeight ReadOnlyProperty.png
Bold LastPage ReadOnlyProperty.png TextSize
Copies ReadOnlyProperty.png PenHeight TextUnit
FirstPage ReadOnlyProperty.png PenWidth Transparency
ForeColor PrintingCancelled ReadOnlyProperty.png Underline
Handle ReadOnlyProperty.png TextAscent ReadOnlyProperty.png UseOldRenderer
Height ReadOnlyProperty.png TextFont Width ReadOnlyProperty.png


The X and Y parameters of the methods are the horizontal and vertical coordinates of the left-top corner of the object being drawn or cleared. The origin (0,0) is the top-left corner of the control or window in which the drawing is being done.
For example, 50,50 is 50 pixels to the right and 50 pixels down from the top-left of the window or control.

Methods
ClearRect DrawPolygon FillRoundRect
Clip DrawRect NextPage
DrawCautionIcon DrawRoundRect Pixel
DrawLine DrawStopIcon StringDirection
DrawNoteIcon DrawString StringHeight
DrawObject FillOval StringWidth
DrawOval FillPolygon
DrawPicture FillRect


Notes

By default, the Quartz graphics engine is used on OS X when you call methods of the Graphics class such as DrawLine, DrawRect, DrawOval, and so forth. The Quartz graphics engine uses anti-aliasing to make lines look smoother. The disadvantage is that it is slower than line drawing using the older Apple technology. For most applications, the Quartz engine will be fast enough and it does produce more attractive results on Mac OS X. However, if you need more speed, you can improve the performance of the drawing methods under Mac OS X by turning off the Quartz graphics engine and using the QuickDraw engine instead. Do this by setting the Graphics.UseOldRenderer property to True before you begin drawing.

Alpha Channel Support

The Graphics object has a new property, "Transparency As Double". This is a value between 0.0 (opaque) and 100.0 (transparent) that affects all drawing done to the Graphics object. Transparency of the Graphics object can be used to obtain "fading" effects (amongst other uses) by setting the transparency to a value greater than 0.0.

When rendering colors or pictures with alpha channel information, the Transparency property is composited with the alpha channel information. This allows you to perform uniform fading operations easily and efficiently.

On Windows, using the Transparency property only works on Picture.Graphics if the Picture was created with a depth of 32, or is a Picture with an alpha channel. Attempting to use it on other forms of Pictures will result in an UnsupportedOperationException.

Graphics transparency is supported on all platforms, but requires GDI+ to be enabled on Windows and cannot be used by the Printer Graphics. If GDI+ is not enabled on Windows or if used on a Printer Graphics, a PlatformNotSupported exception is raised when setting the Transparency property to anything other than opaque, or setting the ForeColor to a color that has a non-opaque alpha component.

Examples

This example uses the Paint event handler of a Canvas control to draw the text “The quick brown fox” in Helvetica bold, italic, 18 point, 50 pixels from the top of and 10 pixels from the left side of the control.

Sub Paint(g As Graphics)
g.Bold=True
g.Italic=True
g.TextFont="Helvetica"
g.TextSize=18
g.DrawString "The quick brown fox", 10, 50


This example draws a triangle in a Canvas control. It is placed in the Paint event. The parameter g as Graphics is passed into this event.

Dim Points() as Integer
Points=Array(10,10,100,50,10,200,10,10)
g.ForeColor=RGB(100,200,255)
g.FillPolygon Points


This example uses the Clip method to define child Graphics items within the parent Canvas. The code is in the Paint event of a Canvas. The two clippings define regions at the top of the canvas and the DrawOval method draws object in each one. Notice that the first call tries to draw an oval that is wider than the region. It is truncated in the drawing.

Dim myclip as Graphics = g.clip(0,0,150,15)
Dim myclip2 as Graphics=g.clip(150,0,150,15)

//draw the border of the Canvas..
g.forecolor=&c000000
g.DrawRect(0,0,g.width,g.height)

//draw into the first area...
myclip.ForeColor=&cff0000
myclip.DrawRect(0,0,myclip.width,myclip.height) //draw the border of the area..
myclip.DrawOval(0,0,200,15) //the oval does not appear outside the region
//despite the call


//draw into the second area...
myclip2.ForeColor=&c0000ff
myclip2.DrawRect(0,0,myclip2.width,myclip2.height) //draw the border of the area
myclip2.DrawOval(0,0,150,15)

See Also

Canvas control; Window, ContainerControl class.

Personal tools
Namespaces

Variants
Actions
Main
Documentation
Community
Toolbox
Help