# Drawing on a Canvas

Sorry guys; another new starter question…

Can someone show me the .Paint code to draw a rectangle on a canvas that is drawn with 20 pixels gap at the top and to the left and right; and that is sized to the width of the canvas area?

I am just not getting my head around the widths and deltas, so an example that does this will help massively.

Thanks

Phil

``````g.forecolor=&cFF0000 // red
g.drawrect 20,20,g.width-20,g.height-20``````

I think that is what you are asking for

Thanks that works, but for my education can you tell me why this doesn’t work (ie. it doesn’t draw to the width of the canvas it is on)…

``````Dim r As New RectShape
r.Width = g.Width
r.Height = 100
r.FillColor = RGB(0, 200, 0)
g.DrawObject(r, 20, 20)``````

[quote=186375:@Phil Corley]Thanks that works, but for my education can you tell me why this doesn’t work (ie. it doesn’t draw to the width of the canvas it is on)…

``` Dim r As New RectShape r.Width = g.Width r.Height = 100 r.FillColor = RGB(0, 200, 0) g.DrawObject(r, 20, 20) ```[/quote]

You need to set borderwidth.

isn’t it that the co-ordinates of an object2d shape are the centre x,y not the top left corner?

r, 20,20 is saying put the rectshape r on screen so that its centre is at 20,20

Maybe:

Dim r As New RectShape
r.Width = g.Width-40
r.Height = g.height - 40
r.FillColor = RGB(0, 200, 0)
g.DrawObject(r, g.width/2,g.height/2)

HI Jeff - well that works better

But according to the documentation it shouldn’t. The help says “The optional parameters DeltaX and DeltaY are offsets from the top-left corner of the Graphics object to the top-left corner of the Object.”

So not sure why working from the centre works ?

Phil

ah.
So when you create the rectshape its x,y are both 0

Then you are applying offsets of g.width/2 etc

So better might be

Dim r As New RectShape
r.Width = g.Width-40
r.Height = g.height - 40
r.FillColor = RGB(0, 200, 0)
r.x = g.width/2
r.y = g.height/2

g.DrawObject r

my experience is that Obj2D shapes always draw from the center.
And they consume more overhead if all you need are simple lines, rectangles, or ovals (circle)

Jeff - yes that works better

Dave - so if all I need to draw is boxes, lines and text on a canvas, what should I use? I thought RectShape was the way to do the boxes, but still learning so there may be a better way?

Thanks

Phil

for plain basic shapes use

DrawLine
DrawRect
DrawOval
FillRect
FillOval
there is also a polygon method , but I don’t recall it off the top of my head

These are much faster that the Obj2d, and remember one IMPORTANT THING (especially if you come from a VB background)… The XOJO Canvas is NON-PERSISTENT, unlike the VB PictureBox

Use the Obj2d if you have complex shapes (bezier curves, etc)

Thanks Dave, just read up on the Graphics object and realise this is much better and easier

Phil

The Object2D classes are nice if you need to implement scaling and rotation of your objects. These things are incredibly hard to do (not impossible though) with the standard graphics class. If you’re going to do rotation of text you’ll almost always have to use Object2D unless you go through a plugin.

Thanks Bob - hopefully I won’t need the Object2D stuff as the needs are simple - produce a simple report on screen and also be able to print it off; so I am assuming I will be able to reuse the code to display to a canvas in a window and also to route to a printer canvas. He hopes

If you develop for Target Win32 than Object2D is something you should use for some stuff like RoundRect (esp. if they are small) you will see a difference from the render engine. But they sure have more overhead and are slower.

Also be aware that the graphics objects have different resolutions so you’ll have to do the internal math for display versus print.

FWIW, we have a set of reporting classes that helps simplify some of this. BKeeney Shorts is set of reporting classes that allows you to render to screen, printer, html, and PDF (using the MBS DynaPDF plugin). It doesn’t have a GUI designer and it’s strength is that you get to control everything. It’s drawback is that you get to control everything. More information at http://www.bkeeney.com/allproducts/bkeeney-shorts/