Graphics.DrawPicture vs g.DrawPicture

At first I thought that Graphics.DrawPicture had been depreciated for g.DrawPicture.

After testing both I realised that they both act differently. Graphics.DrawPicture moves the canvas point of origin relative to the window, whereas g.DrawPicture moves the picture within the canvas at the point of origin. The LR on Graphics.DrawPicture uses examples that use g.DrawPicture - which is very confusing to me.

Nevertheless, what I’m trying to achieve is to draw a simple XY graph. It’s a MUST that the graph can be cropped/scaled/zoomed. I’ll be using DrawLine.

At the moment I’ve got the basics together. I’ve used this pseudo code that was posted on this forum (Paul Lefebvre) but adjusted to suit my purposes:

[code]Add a Picture as a property to WindowMain:
GraphPicture As Picture

Add a Canvas to the Window: (cnvGRAPH)

In the Canvas Open event handler, initialize the Picture: (X, Y, Colour depth)

GraphPicture = New Picture(600, 500, 32)

In the Paint Event handler of the Canvas, draw the Picture: (Picture Name, Origin X, Y)

g.DrawPicture (GraphPicture, 0, 0)

Create a Method to draw to the Picture. When the Canvas refreshes, it will then draw what you have in the Picture.

Example:

Method: DrawGraph

GraphPicture.Graphics.ForeColor = &c0000ff
GraphPicture.Graphics.FillRect(10, 10, 50, 50)

// Tell the canvas it needs to be updated, which
// calls its Paint event handler.

cnvGRAPH.Invalidate

Now call this method from something like a PushButton:

DrawGraph[/code]

This appears to work fine. I haven’t tried any scaling yet because I’m still unsure which way to go or what the potential issues might be if I stick with the above method.

[quote]At first I thought that Graphics.DrawPicture had been depreciated for g.DrawPicture.

After testing both I realised that they both act differently. Graphics.DrawPicture moves the canvas point of origin relative to the window, whereas g.DrawPicture moves the picture within the canvas at the point of origin. The LR on Graphics.DrawPicture uses examples that use g.DrawPicture - which is very confusing to me.[/quote]

Nooooo…

In both cases, top left X is 0,0
Positive values of X and Y move you down and right.

If you have a picture and draw on the picture, thats exactly what you expect.
If you have a canvas on screen, and you draw in the paint event of that, then 0,0 of the DRAWING AREA is the top left corner of the canvas.
The Paint() event gives you the graphics of the canvas , but doesnt care where it is on screen.

And your code above always draws in a picture, then displays the picture

For scaling, you may want to look into using Object2D classes.
get a set of drawing instructions that work at the starting size, then if you want to blow them up and keep them sharp, change the .scale property of the Object Group2d that contains them.

If you blow up the PICTURE you create above, its likely to look fuzzy

Thanks Jeff, but some things still don’t make sense. Yes, I understand that the top left is X0Y0, and X is across and Y is down (to be inverted later).

The picture does draw within the canvas, but Graphics.DrawPicture moves the actual canvas whereas g.DrawPicture retains the original position as per my placement in the IDE.

The reality is that this is a difficult point I’ve reached in the program. I assumed that ‘scaling’ would scale up the data. What I mean is that vector data is scaled and re-drawn - not a scaling of a bitmap image.

I’ll elaborate. What I’m wanting to achieve is replicating an old software program I wrote many years back using Quickbasic.

Quickbasic had a function called “ViewPort/View” or similar. What that did was enable you to create a new view based on your current screen co-ordinates which you could enlarge or reduce. Not with bitmaps but with vector data. Worked fantastic.

I think I can achieve what I want using scaling variables in the method before calling paint event - I was hoping that there may be an easier solution. I guess I’ll look at the Object2D classes, may be what I’m looking for. It does seem overly complicated.

When I look back at that old Quickbasic code, I’m surprised how it all worked - but it did, perfectly. That what I’m wanting to reproduce using Xojo.

Using graphics still work, but as it is deprecated, it may stop working with any new coming version.

Thanks Michel, that’s what I sort of thought was the case.

But what about this:

GraphPicture.Graphics.FillRect (10,10,100,100) // works fine
GraphPicture.g.FillRect (10,10,100,100) // creates an error

There must be something crucial I’m missing.

Picture Class does not have an entry named g.

g appears in the Canvas1.Graphics Event.

Have a look at DataPlotClasses for Xojo (http://opensource.the-meiers.org/). The code is free, and there are many nice examples to learn from.

Yes indeed Bryan, I’ve had a look at Roger Meier’s DataPlot Classes. I don’t think it suits my purpose.

I appreciate what Roger has done, but I have two issues with the DataPlot Classes:

  1. I don’t believe that scale-ability is included or easily achieved
  2. I require a specific function that enables zoom and a cursor to read data at recorded samples

My software requires very high levels of data analysis where the graph can be zoomed into, to reveal each point. That point could be at a resolution of 40,000 samples in the Y direction and 2000 in the X direction.

Like I said, I was able to do this with Quickbasic.

Lots of things for me to read and review.

[quote=291190:@Steve Kelepouris]GraphPicture.Graphics.FillRect (10,10,100,100) // works fine
GraphPicture.g.FillRect (10,10,100,100) // creates an error

There must be something crucial I’m missing.[/quote]

g is the name of graphics in the Paint event. It means nothing outside.

What you can do is :

Dim g as Graphics = GraphPicture.Graphics

Then you can use g as you describe.

[quote=291204:@Steve Kelepouris]Yes indeed Bryan, I’ve had a look at Roger Meier’s DataPlot Classes. I don’t think it suits my purpose.

I appreciate what Roger has done, but I have two issues with the DataPlot Classes:

  1. I don’t believe that scale-ability is included or easily achieved
  2. I require a specific function that enables zoom and a cursor to read data at recorded samples

My software requires very high levels of data analysis where the graph can be zoomed into, to reveal each point. That point could be at a resolution of 40,000 samples in the Y direction and 2000 in the X direction.

Like I said, I was able to do this with Quickbasic.

Lots of things for me to read and review.[/quote]

I believe the best route to do this is to create a picture to work on, and use DrawPicture with the scaling feature to zoom in or out and move around. So in essence you never show the actual large picture, but only a view of it.

No it doesnt.
The canvas stays put.

You can either just draw on the canvas when it paints
(the Paint event. Draw on the G object)
or you can draw in a picture and invalidate the canvas, intending to display the picture in the paint event
with g.drawpicture thepicture,0,0

Object2D classes are the vector classes.
Create a Group2D
Add to it Object2D items to draw lines and boxes and text.

In teh canvas.paint() event, display the objects using

g.drawobject MyGroup2d,0,0

If you want to zoom, change the .scale property of the Group2D
If you want to scroll, use

g.drawobject MyGroup2d,offsetx,offsety

in the paint event

That is exactly what I’m saying Michel, thanks for putting it into simpler terms. I already know what’s required. Algorithms and UI is something I’m good at - creativity is my major strength. How to take those ideas and make something workable with coding tools is a struggle at the moment.

It’s not an issue with Xojo, but just an overall learning phase I’m going through and I’ll get there in the end.

Start coding and when you fall into a trap (be careful), ask question(s) here. And if the explanation is too technical for your current Xojo / developement skill, ask for more simple explanation.

[quote=291213:@Jeff Tullin]No it doesnt.
The canvas stays put.[/quote]

On the contraire, the canvas does not stay put in my version. I must be doing something different. I clearly have to do more reading.

Thanks all, it’s coming up to midnight and I have to get up early to perform my “real” job.

Yep! that’s pretty much my method. I don’t pretend to be anything other. In the tradition of Clint Eastwood’s Dirty Harry “A Man has to know his limitations”

:slight_smile:

OK.

If the canvas is called Canvas1

Search your code for Canvas1.left or Canvas1.top
If you change those, it will move.

In Canvas1 events, if you have me.left = or me.top = then the same will occur

if the Canvas is set in the designer to be aligned to the bottom or right edges of a window, it may move about when the window is resized.

To be clear:

Canvas.Graphics was deprecated back in 2011. To draw to a Canvas you should now only use the Paint event handler with its g As Graphics parameter.

As noted by others above, you can of course still access the Graphics object of a Picture to draw directly to the Picture.

Error
You do not have permission to perform this action.


Now it works and I’ve just lost everything I typed!!!

In short - it looks like I’ll be using Object2D.

Do I still use the same methodology, ie. create a Picture, create a Method then draw the Picture to the Canvas?

you either paint the Picture into the Canvas1.Graphics Event or draw it to Canvas1.Backdrop.

In both cases, you pass your Object2D to an Offscreen Picture.

Thanks Emile.

I’ve spent the last few hours trying to work this out. Painting the Picture into the canvas is what I assumed would work, but It doesn’t at this point.

Unfortunately the LR doesn’t show clear worked through examples and I’ve found myself just going around in circles - who knows if what I’m looking at is relevant or redundant. Give me a book any day.

I have gathered that I’ll need to use CurveShape with Order set to zero for straight lines and .scale is used for obvious reasons.

Thanks for the info above Jeff, I’ve tried that but get an error in the paint event that this item doesn’t exist. I won’t post the code because at this point I’ve tried a few different things and will have to revert back to my previous version.

I certainly appreciate everyone’s input into this issue and all the other things that people on this forum have helped me with - without this forum, I would not have progressed with my software.

BUT, here’s the problem. The LR in my view is flawed, confusing and inadequate. Like I said, I appreciate peoples input, but I do like to relax and read through a manual or instructions in Book format. I guess I don’t like to rely on other people - I like to give it a go myself first, then ask questions later if required.

There’s nothing wrong with Xojo, I love using it. The problem for me is finding out HOW to use it - that part shouldn’t have to be an issue where clarity is paramount.

Are there any books on Object2D - Paper or e-books?