How to draw a simple line

Hello.
I want to draw a simple line, but all the examples are to difficult and to much complex for me.
I think it should be a 1 row order like: canvas1.DrawLine(0,20,190,20)
But it doesn’t work :frowning:
And there is no sufficient (easy) documetation.
Who can help?

Well, you wouldn’t do it like that. All drawing should occur when the framework tells you, in the Paint event of the canvas. There is a parameter, g as graphics, that gives you a context for drawing. But it’s almost as easy as you want. Just set a color first:

Sub Paint(g As Graphics, areas() As REALbasic.Rect) g.ForeColor = &c000000 g.DrawLine(0, 20, 190, 20) End Sub

The coordinates will be local to the top-left of the Canvas.

Ok. Xojo installs a method called “Paint”.
But how can i call that method?
g.Paint no
Paint(g) no :frowning:

Paint is not a method but an Canvas Event. However you can tell the OS to refresh this executing paint by calling the invalidate method if you need to immediately tell the OS to refresh.

canvas1.invalidate(false)

Here is a link to the Canvas.Paint event that explains the Paint Event in detail.
Xojo Canvas.Paint Documentation

Hello, i know that examples, but they doesn’t work too…
Now i know that there is missing the “End sub”. (why?)
But how can i start that method?
Paint(g) doesn’t work.
I tried for days, but there is no working example i understand :slight_smile:

There is also a Line control. Just drag and drop it from the Library, and resize it accordingly. It won’t help you learn how to use the Graphics class but it might still be useful.

However, if you send me a private message with your email address, I’ll send you a very quick example with a Canvas control and the Graphics.DrawLine method. It’s nothing that Brad and Mike haven’t said but it might help put it into context for you.

Peter, it sounds like you might want to make sure you are starting with the Introduction to Programming with Xojo book or the User Guide. Those will give you an overview of how Xojo works in general, including event handling. After which you will have the necessary background to understand how drawing in a Canvas using the Paint event handler works.

There are specific examples here:

  • Introduction to Programming with Xojo: Chapter 7: Just Browsing, Section 3: Decor
  • Xojo User Guide Book 3: Framework, Chapter 3: Graphics and Multimedia, Section 2: Pictures
  • Lastly, you can look at the Language Reference examples for Canvas, but you ought to start with the first two.

I can draw a line now…
But i think my problem is to control the events.
I need to call different methods for one canvas
i.e. one for a rectangle, circle, line and so on.
For me it seems that it is only one method per canvas possible…?
(many thanks to Gavin Smith)
My problem is that only the small introduction has more than 200 pages of difficult english…
How is it possible to switch between the methods?

[quote=40332:@Peter Marx]I can draw a line now…
But i think my problem is to control the events.
I need to call different methods for one canvas
i.e. one for a rectangle, circle, line and so on.
For me it seems that it is only one method per canvas possible…?
(many thanks to Gavin Smith)
My problem is that only the small introduction has more than 200 pages of difficult english…
How is it possible to switch between the methods?[/quote]

I’m not sure what you mean. The example I sent you has a few shapes - lines and an oval if I remember correctly.

Generally, you don’t control events. Events are things that happen to your app and its controls. Events happen as the app is running. For example, the user might click a PushButton (that event is PushButton.Action), the user might resize the window (that event is Window.Resized) etc. In those events, you put code to respond to these events. In this case, the relevant event is Canvas.Paint. I used the Canvas.Invalidate method to force the Canvas to redraw itself (which causes the Paint event).

Methods are functions or procedures. You can call these when you want to.
Events are things that happen to your app.

Yes, i like to use one event to draw with a circle method and another event to start a rectangle method. In your example all events start only one method. (I am a little confused, because i have german introduction with english manual and german software and the names and terminology is different and the alphabetic order and so on.)
With invalidate i can refresh allways the same but i want to draw with different methods…

Perhaps the simplest thing for you to do would be to draw directly to a Picture object and then draw that Picture using a Canvas.

For example, add a Picture as a property to a Window:

MyPicture As Picture

Add a Canvas to the Window. In the Canvas.Open event handler, initialize the Picture:

MyPicture = New Picture( 500, 500, 32)

In the Paint event handler of the Canvas, draw the Picture:

g.DrawPicture(MyPicture, 0, 0)

Now you can create your own methods that draw to the Picture. When the Canvas refreshes, it will then draw what you have in the Picture. Here is an example method, called DrawRectangle, to draw a filled rectangle:

[code] MyPicture.Graphics.ForeColor = &c0000ff
MyPicture.Graphics.FillRect(10, 10, 50, 50)

// Tell the canvas it needs to be updated, which
// calls its Paint event handler.
Canvas1.Invalidate[/code]

You can now call this method from something like a PushButton:

DrawRectangle

Hello,

my problem is that i understand NOTHING!
You wrote: “MyPicture As Picture”, but Xojo has no pictures.
You mean an “ImageWell” or a “Canvas”?
I think i have a have a serious translation problem with Xojo!

Xojo indeed has a Picture class (NOT a control,) and @Paul Lefebvre’s suggestion is a very good one.

Ah ok! That works fine!
That is very complicated…
More than: Picture1. DrawLine(X1,Y1,X2,Y2)
(The Xojo docs doesn’t show the Picture class, a server problem?)

Thank you! :slight_smile: