Graphics.DrawPicture vs g.DrawPicture

I am afraid paper books are on their way out. But for eBooks, you may want to bookmark http://xdevlibrary.com/

And for general use, the paperback 2001 RealBasic for Dummies is still available at a bargain price on Amazon ($7.20 used). Sure, it does not have Object2D and the latest gizmos, but you may find it adequate for all regular stuff.

There are other titles available if you simply type RealBasic.

That said, and even if I am born way before ebooks, I have grown accustomed to consult the LR first, and to consider the forum a natural source of informations. It is very rare that any question not be answered within the hour. Don’t be shy.

I am not aware of any book about Object2D, but we do have people here who are real good, and will usually gladly lend a hand. I also suppose you had a look at Obj2DTextRotation.xojo_binary_project in the Graphics and Multimedia folder within the examples.

Steve:

Open the Xojo example project called Obj2DTextRotation

Look in the Canvas Paint Event

You will see the event has this code:

[code]Const pi = 3.14159

Dim ss As New StringShape
ss.Text = DisplayTextField.Text
ss.TextSize = 24

ss.Rotation = RotationSlider.Value * (pi/180)

g.DrawObject(ss, g.Width\2, g.Height\2)[/code]

The stringshape is defined inside the event but more on that later.
lets add a line:

[code]Const pi = 3.14159

Dim ss As New StringShape
ss.Text = DisplayTextField.Text
ss.TextSize = 24

ss.Rotation = RotationSlider.Value * (pi/180)

g.DrawObject(ss, g.Width\2, g.Height\2)

dim lin as new curveshape
lin.X = 10
lin.y = 10
lin.x2 = 200
lin.y2 = 200
g.DrawObject(lin, 20,20)
[/code]

Now the line draws too.

lets put these things into a group, and draw the group instead:

[code]Const pi = 3.14159

Dim ss As New StringShape
ss.Text = DisplayTextField.Text
ss.TextSize = 24

ss.Rotation = RotationSlider.Value * (pi/180)
//g.DrawObject(ss, g.Width\2, g.Height\2)
dim lin as new curveshape
lin.X = 10
lin.y = 10
lin.x2 = 200
lin.y2 = 200
//g.DrawObject(lin, 20,20)

dim g2d as new Group2D
g2d.Append ss
g2d.Append lin
g.drawobject g2d,g.Width\2, g.Height\2
[/code]

let’s scale this to half the size:

  dim g2d as new Group2D
  g2d.Append ss
  g2d.Append lin
  
  g2d.Scale = 0.5
  g.drawobject g2d,g.Width\\2, g.Height\\2

Now, the only thing we need to do is move the Group2D object out of the Paint event, (make it a property of the window) and fill it in our ‘create graph’ code.

Assuming it is populated elsewhere, the whole of the code above in the Paint Event becomes:

Sub Paint(g as graphics) g.drawobject g2d,g.Width\\2, g.Height\\2 end sub

And the whole group is redrawn when the canvas needs to be refreshed

Thanks Michel, your words are always welcome and appreciated. I’ll definitely look into “RealBasic for Old Dummies”. :slight_smile:

I’m a “hobby programmer” and perhaps get what I deserve. Fine. It’s a means to an end. That end is that the software is an adjunct, but important component to a hardware device, and unlikely to ever make a profit - only for personal gain and accomplishment.

I think I can do what’s required with the graphing scenario without Object2D and simply use math for scaling. I know it will work but a rather convoluted approach.

Perhaps if I stuck at it I could be a good programmer. In the past I’ve written some good solutions for PIC chips, and other successful solutions using Quickbasic, VisualBasic and FileMaker.

I’ve also studied HTML and CSS in an effort to possibly change my career. I gave that up when I realised I would be competing with others (especially on the sub-continent) that could/would do what I could for $5 a day and 2 bowls of rice.

Check out 99designs - perhaps that’s where real programming is heading. “Level Playing Field - My Arsss…”

[quote=291655:@Jeff Tullin]Steve:

Open the Xojo example project called Obj2DTextRotation

Look in the Canvas Paint Event

You will see the event has this code:

[code]Const pi = 3.14159

Dim ss As New StringShape
ss.Text = DisplayTextField.Text
ss.TextSize = 24

ss.Rotation = RotationSlider.Value * (pi/180)

g.DrawObject(ss, g.Width\2, g.Height\2)[/code]

The stringshape is defined inside the event but more on that later.
lets add a line:

[code]Const pi = 3.14159

Dim ss As New StringShape
ss.Text = DisplayTextField.Text
ss.TextSize = 24

ss.Rotation = RotationSlider.Value * (pi/180)

g.DrawObject(ss, g.Width\2, g.Height\2)

dim lin as new curveshape
lin.X = 10
lin.y = 10
lin.x2 = 200
lin.y2 = 200
g.DrawObject(lin, 20,20)
[/code]

Now the line draws too.

lets put these things into a group, and draw the group instead:

[code]Const pi = 3.14159

Dim ss As New StringShape
ss.Text = DisplayTextField.Text
ss.TextSize = 24

ss.Rotation = RotationSlider.Value * (pi/180)
//g.DrawObject(ss, g.Width\2, g.Height\2)
dim lin as new curveshape
lin.X = 10
lin.y = 10
lin.x2 = 200
lin.y2 = 200
//g.DrawObject(lin, 20,20)

dim g2d as new Group2D
g2d.Append ss
g2d.Append lin
g.drawobject g2d,g.Width\2, g.Height\2
[/code]

let’s scale this to half the size:

  dim g2d as new Group2D
  g2d.Append ss
  g2d.Append lin
  
  g2d.Scale = 0.5
  g.drawobject g2d,g.Width\\2, g.Height\\2

Now, the only thing we need to do is move the Group2D object out of the Paint event, (make it a property of the window) and fill it in our ‘create graph’ code.

Assuming it is populated elsewhere, the whole of the code above in the Paint Event becomes:

Sub Paint(g as graphics) g.drawobject g2d,g.Width\\2, g.Height\\2 end sub

And the whole group is redrawn when the canvas needs to be refreshed[/quote]

Thanks muchly for that Jeff. I’ll carefully go through it this weekend. My problem (well, one of them) is that I’m coming home from a hard days work of mental torture, then trying to work on and comprehend issues with my program. Last weekend I spent around 8hrs on Saturday and Sunday and finally resolved some other issues with the program.

Things take time and time seems to be getting harder to find.

When I was 35, it was a very good year, It was a very good year for blue blooded girls of independent means - we’d ride in limousines, their chauffeurs would drive, when I was 35.

When I was 53… :slight_smile:

Welcome.
Trust me, work through those 3 steps above and it will quickly make sense.
Im amazed people dont use Object2D stuff more.
The main ‘gotcha’ is the x,y position being the centre of an object.

(I once had hopes that we might be able to save these as SVG or WMF files, but that went nowhere.
And the PICT format on Mac which supported vectors has been deprecated, leaving only PDF for my needs.
Still good for the screen however)

How do you save to disk / open from display them ?

Print to PDF is good.

Well I’m 55… :slight_smile:

I’ll work through your examples. What about DXF? that was also vector based - I think it’s still used.

I’ve actually got a postscript book lying around somewhere. I used it for creating small graphic images for printing purposes. Then PDF came out. It’s still a form of the original postscript language - I think.

Welcome Steve.

Did you go to http://www.xdevmag.com/ and http://www.xdevlibrary.com/ to see what exists there ?
xDev Issue 7.5, July / August 2009:
Canvas 101: Canvas Basics

in xdevlibrary, search: Program the Canvas Control with Xojo Desktop.

HTH.

Thats the point. You used to be able to do that in a PICT file.
Nothing else works, and PICT is dead.
Thats not an issue here though. As a display tool for vector functionality, they are pretty good.

[quote=291659:@Steve Kelepouris]I think I can do what’s required with the graphing scenario without Object2D and simply use math for scaling. I know it will work but a rather convoluted approach.
[/quote]

I have a very simple philosophy that carried me out since the Basica and GWBasic era back in 1982, through QuickBasic, Turbo Pascal, VisualBasic, RealBasic and now Xojo : keep it simple. If you know how to make it work, don’t try to do rocket science. Make it work. In computing, elegant and abstract is not necessarily what works. Down to earth is often preferable _ and less bug prone.

Start with a simple proof of concept with Canvas. See if it does what you want. If so, go on.

If you are somehow familiar already with vector primitives, you can have a look at Object2D. It is not that difficult to grasp. Well, it depends how much geometry you are familiar with.

My point is, as a programmer, you have the privilege of doing mistakes, trial and error, until you find something you are comfortable with.

As a hobbyist, make sure you enjoy the ride :wink:

[quote=291655:@Jeff Tullin]. . . Now, the only thing we need to do is move the Group2D object out of the Paint event, (make it a property of the window) and fill it in our ‘create graph’ code.

Assuming it is populated elsewhere, the whole of the code above in the Paint Event becomes:

Sub Paint(g as graphics)
g.drawobject g2d,g.Width\2, g.Height\2
end sub

And the whole group is redrawn when the canvas needs to be refreshed[/quote]

Thanks for talking the time to post that Jeff.

I did the example and all works as expected until moved the code to a property. There must be something basic I’ve missed. I moved all the code (except for the g.DrawObject line) out of the Canvas Paint event into the code window of the property myDraw.

In the canvas paint event had:
g.drawobject g2d,g.Width\2, g.Height\2

I got an error that g2d didn’t exist so I changed the line to:
g.drawobject myDraw,g.Width\2, g.Height\2 thinking that was the issue.

It ran with no error, but also no image?. Not a waste of time because I’ve learned about grouping primitives, scaling rotating etc. which is very useful to know.

There are a couple of things with using Object2D that may be an issue for me. Scaling up also scales the line thickness, which of course is exactly how it should work. I end up with a very thick line when zoomed in too far, which will make viewing the graph points difficult. A work around might be to inversely and proportionally scale the BorderWidth?.

The .Scale parameter was my main interest, but it looks like .Scale isn’t able to scale independently on the X and Y axis - or is it?. So after procrastinating for a couple of weeks on which way to go, I think I’ll take the simpler route and use g.DrawPicture. I’ll do the scaling programmatically.

“The Philosophy of Software Development in the Free World - Michel Bujardet, 2016” does come into play. I’m taking a step back and reviewing what are the needs of the software, as opposed to my wants. With almost every piece of software I’ve ever written I keep adding “features” to make it even better.!!! . . . which in the end has been detrimental to the point that the solution worked but was never properly finished.

I don’t think I really NEED zoom for version 1.0 because I will have a data readout of x/y anyway. Phase 1 was reading data off the Serial Port. Graphing is phase 2 of development. Phase 3 will be calculations and analysis of the collected data. Phase 4 will be file saving and data retrieval. Phase 5 will be saving preferences. Phase 6, perhaps deployment?.. I’m sure there’s more.

A lot of work to be done and I’ll be asking many more questions on this forum. </gulp!>

This thread has helped me crystalise in my mind a direction to take. Also thanks for the links to the xdevlibrary - looks there’s some great e-books at minimal cost. I’ll need to get myself a Kindle device or similar.

Until the next episode . . . Cheers :slight_smile:

[EDIT] btw. Jeff, for future reference I would like to know where/why/how/what I did wrong when moving that code to a Property.
I’m not saying that there’s a problem with the code and methodology you suggested. I’m saying that it’s likely a case of the “Jamie Olivers’ 15 minute meals” syndrome. Where one collects the ingredients together, follows the recipe, but forgets (or is unaware) that the frying pan needs to be turned on and hot! :slight_smile:

[quote]In the canvas paint event had:
g.drawobject g2d,g.Width\2, g.Height\2
I got an error that g2d didn’t exist[/quote]

The question there is : moved it to a property of what?
"The window " would be the right answer.

So Window1 has a control of Canvas1 , and a property of g2d.

If you made the g2d object a property of the app, or a module, then Canvas1 wouldnt be able to see it unless you said
app.g2d or mymodule.g2d

What was myDraw??

Well, myDraw is a property that I created under the main window with the Type Object2D.

Don’t stress Jeff. I obviously don’t understand what’s required and that’s ok. I’ve bitten off more than I can chew. Lots of work to do. There are fundamentals that I don’t know and/or understand. I have to go back to square one.

Hi Steve:

In a brand new project,
Set size of Window1 to 600 x 420,
add a Canvas (Name: Canvas1, Size: 400 x 400), and a PushButton (name: PushButton1)

In the Pushbutton, copy / paste this code:

[code] Dim OffscreenPicture As Picture
Dim a As New ArcShape

OffscreenPicture = New Picture(400, 400)

a.ArcAngle = 1.57
a.StartAngle = -1.57
a.FillColor = RGB(0, 255, 127)

OffscreenPicture.Graphics.DrawObject(a, 100, 100)

Canvas1.Backdrop = OffscreenPicture[/code]

With that, you can concentrate yourself to draw your graph. Once this will be done (you are satisfied with the resulting graph), you can open a new conversation to ask help for changing the way to display it (the chart) if needed.

[quote=292170:@Jeff Tullin]The question there is : moved it to a property of what?
"The window " would be the right answer.

So Window1 has a control of Canvas1 , and a property of g2d.

If you made the g2d object a property of the app, or a module, then Canvas1 wouldnt be able to see it unless you said
app.g2d or mymodule.g2d

What was myDraw??[/quote]

Yes, I created the property g2d under the MainWindow.
MainWindow now has the control DisplayCanvas, and a property called g2d

[quote]Assuming it is populated elsewhere, the whole of the code above in the Paint Event becomes:

Sub Paint(g as graphics)
g.drawobject g2d,g.Width\2, g.Height\2
end sub[/quote]

myDraw was a method I created “elsewhere” then moved all the code into - which is obviously incorrect.

So, I guess the issue for me is, if we are moving all the code, except for g.drawobject g2d,g.Width\2, g.Height\2 which remains in the canvas paint event - where are we moving it to?

My apologies to all those, who at this moment are pulling their hair out or banging their head against a hard object :slight_smile:

You can perfectly draw to g in an external method, as long as it is called from the Paint event. Simply pass it g.

The external method was the technique I used as described in post #1 which worked perfectly.

The main differences now being the use of Object2D g.DrawObject, as apposed to g.DrawPicture. Also, I was drawing to a picture, then displaying that picture in the canvas.

I think part of the issue with my testing is that I started with the Object2D template from the examples folder. The canvas updates when the slider is moved using refresh. Perhaps there’s a clue there?

I would like to get it working, but it’s more out of annoyance that I can’t solve it, and courtesy to others that this thread be resolved in some positive manner.

At this point I’ve gone back to g.DrawPicture. The .Scale function in Object2D doesn’t scale independently on each axis, so therefore Object2D doesn’t suit my requirements for this project.