OOP Question: accessing object values from a class?

P.S. Mike: I will change my workaround to yours - seems more elegant to reference the canvas :slight_smile:

But in all cases your instances of your classes need to know which instances of other classes they are referring to when they reference their properties.

For example, if you put a Button on a window called myButton. You could not then from a class say PushButton.Text = “Hello” and expect myButton’s text to change. Your class must know the instance you are referring to and the instance is myButton not PushButton.

Then as I said, in your class you should not put myButton.Text = “Hello” as myButton is an instance name. What if you had 3 button, myButton, yourButton and hisButton and you wanted to effect all their Text properties. So therefore you would pass the instance to your class and it would effect the text of the relevant button.

So you could have in your classes constructor:

Sub Constructor(buttonToChange as PushButton) buttonToChange.Text = "Hello" End Sub

As you can see it is now reusable as the class can reference any pushbutton not just myButton.

How is raised the cFragment paint event?
You said cCanProteinPrint has an ObjectList as cDrawableObject but in your error sample there is no objectList
if you complete your example with your paint strategy we can reply with a OOP solution.

Thanks Mike, will read that carefully.

[quote=117926:@Antonio Rinaldi]How is raised the cFragment paint event?
You said cCanProteinPrint has an ObjectList as cDrawableObject but in your error sample there is no objectList
if you complete your example with your paint strategy we can reply with a OOP solution.[/quote]

cDrawableObject has a Draw method and a Paint event

In the canvas paint event I pass g to the cDrawableObject Draw method

// draw fragments for i as integer = 0 to ObjectList.Ubound ObjectList( i ).Draw( g ) next

and in the Draw method of cDrawableObject I call paint as required

Paint g DrawSelection( g )

See the old REALbasic University articles 85 to 93 for more info http://www.applelinks.com/rbu/085/

Markus, do you have the SuperDraw project? I can only find it in rb format which I cant open. This may help me somewhat with a current project I am working on. Thanks

I can open it fine in RS2012 R2.1 and save as a rbp file

https://dl.dropboxusercontent.com/u/992591/REALbasic/Forum/superdraw6.zip

However there is an error which I haven’t tracked down yet when you try to run it.

However I just used the bits I wanted by reading my way through the RBU articles. It’s really a very good design.

A simple solution is:
redefine your cDrawableObject.paint(g as graphics) to cDrawableObject.paint(g as graphics,c as canvas=nil)
(so it’s back compatible)

Redefine it’s draw method to:
draw(g as graphics, c as canvas)
Paint g,c
DrawSelection( g )

you cFragment paint event now can be:
for each key as variant in cCanProteinPrint©.DictMods.Keys

Maybe a c isA cCanProteinPrint before is even better.

Not the best solution, but a solution

Thanks Antonio, I might just do that by passing in c as a cCanProteinPrint (then I don’t need the extra check)

but then cDrawableObject will be no more a general class
it’s your cFragment subclass that knows that c can (and must be) a cCanProteinPrint

[quote=117941:@Antonio Rinaldi]but then cDrawableObject will be no more a general class
it’s your cFragment subclass that knows that c can (and must be) a cCanProteinPrint[/quote]
True. Thanks for thinking ahead - sometimes I’m just too focused on the problem at hand :slight_smile:

[quote=117934:@Markus Winter]I can open it fine in RS2012 R2.1 and save as a rbp file

https://dl.dropboxusercontent.com/u/992591/REALbasic/Forum/superdraw6.zip

However there is an error which I haven’t tracked down yet when you try to run it.

However I just used the bits I wanted by reading my way through the RBU articles. It’s really a very good design.[/quote]

Many thanks Markus, appreciated.

I am interested how he implemented is drawing objects. In my app I have one object called a classDrawObject which can take on any shape i.e. Rect, Oval, Line etc etc, it just has a property of objectType which differentiates which type of shape it is. All the properties are common to all objects although some are not used for instance ‘Text’ is not used in a Rect object.

I preferred it that way so that if I wanted to add another property to my object it would be common to all.

[quote=117957:@Mike Charlesworth]Many thanks Markus, appreciated.

I am interested how he implemented is drawing objects. In my app I have one object called a classDrawObject which can take on any shape i.e. Rect, Oval, Line etc etc, it just has a property of objectType which differentiates which type of shape it is. All the properties are common to all objects although some are not used for instance ‘Text’ is not used in a Rect object.

I preferred it that way so that if I wanted to add another property to my object it would be common to all.[/quote]
He has a class DrawableObject and all the shapes (including text) are subclasses.

DrawableObject doesn’t need to know the type, it is up to the subclasses to draw themselves however they see fit.

The DrawableObject also needs to handle selection of a DrawableObject, e.g. paint selection handles to increase its size, or move it.

To that end DrawableObject has a draw method and a paint event

The Draw method allows to do more than just call the paint event, and in SuperDraw’s case calls paint and DrawSelection

So on the canvas you have an array of DrawableObjects.

In the canvas paint event you iterate over the array of DrawableObjects and call their draw method, which in turn calls the specific paint event.

I think Marc did exactly that with the early version he had called SimpleDraw (RBU 85?) but then changed it.

Yes, I am toying with Marc’s way now to see what advantages it has for me.

Marc’s code looks very good but primitive. I cant get it running, all sorts of errors but looking through it, it seems you don’t really draw the shapes, rather they are dumped on the screen and then you size them to what you need. I am working on drawing them by point by point clicking. Also I don’t really see how his polygons work, I don’t think you can add more vertices, you are just given what it dumps on the screen and I don’t see how you would change the polygon, it seems you just change the overall width an height of the polygon and scales it. I want to be able to move each point of the polygon individually.

I’m nearly there with it all, just wondering what others did

Not sure I’m getting that (as I said I’m a newbie at graphics). Isn’t that how all graphics programming basically works?

Hi Mike,

I made a new “SuperDraw new” in the zip folder which runs fine in RS2012 R2.1

https://dl.dropboxusercontent.com/u/992591/REALbasic/Forum/superdraw6.zip

The last two bits (RBU92 and 93 are missing), but I’m running out of time for today.

Well what I have in my app is you click on for instance the Rectangle button and nothing appears on the screen. You then click on the canvas and that is your first X,Y point, you then move the mouse around and the rectangle sizes from your first X,Y point to where your mouse pointer is and then you click again and that is your second X,Y point and your rectangle is done. The rest is very similar to SuperDraw, just the first method of drawing.

The way it has been done in SuperDraw is the rectangle size is pre-defined, then drawn on the screen, you then size it afterwards how you want.

Neither is right or wrong, just the way i’ve done it is more standard in graphics applications, although takes a bit more coding to accomplish.

[quote=118062:@Markus Winter]Hi Mike,

I made a new “SuperDraw new” in the zip folder which runs fine in RS2012 R2.1

https://dl.dropboxusercontent.com/u/992591/REALbasic/Forum/superdraw6.zip

The last two bits (RBU92 and 93 are missing), but I’m running out of time for today.[/quote]

Thanks, thats better it runs now.

[quote=117997:@Mike Charlesworth]Yes, I am toying with Marc’s way now to see what advantages it has for me.

Marc’s code looks very good but primitive. [/quote]

It’s pretty old. You might try the improved version I used as part of my OOP talk at XDC this year:

http://xdevmag.com/downloads/xdc2014.zip

(The archive has all my demos, but you can find the Superdraw project.)

You can also see movies of me demonstrating how to add new objects to the drawing app:

http://xdevmag.com/downloads/xdc2014movies.zip

I’ll be writing about the Superdraw portion in next month’s "Beginner’s Corner "in xDev Magazine.