Link to home
Start Free TrialLog in
Avatar of KenDickinson
KenDickinsonFlag for United States of America

asked on

Creating a seperate class for graphics

Greetings experts,

Here's hoping for some enlightenment. In the past I have created class libraries and they appear to work fine but I'm having trouble with graphics. To keep it simple let's say I have a form and on this form I want to draw a line from point a to point b. I know how to do this. Now lets say I want to  put the code that draws the line into a class of it's own in a method called DrawALine,so that I can use it on many different forms. I can't make it work. What parameters do I need pass when I call this method from the form. Thanks in advance for your help.

Ken
SOLUTION
Avatar of wguerram
wguerram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wguerram
wguerram

Wow! all that code to draw a line...
all that code to...

>> Here is an an example of how to allow the user to draw lines, move the endpoints, move the whole line, rotate the lines and delete the lines.  The drawing can then be saved and then loaded back again from a file, allowing the lines to still be changed.

I did give a short answer at the top of my post.  ;)

>> The short answer:
You pass in the graphics from the "e" argument in the paint event of the control you are drawing onto.

Specifically, the app has a Panel that the lines are manipulated on.  Here is the Panels paint subroutine:

    Private Sub shapesPanel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles shapesPanel.Paint
        Dim storedLine As Line

        For Each storedLine In lines
            storedLine.showHandles = showHandle
            storedLine.Paint(e.Graphics)
        Next storedLine
    End Sub

Note that I am passing the Graphics context of the "e" parameter into the Paint sub of multiple instances of the Line class that are stored in an ArrayList.  Here is the Paint sub in the Line class:

    Public Overridable Sub Paint(ByRef g As System.Drawing.Graphics)
        ' draw the line segments first (if they are toggled on), and
        ' then draw the handles and intersections on top by calling
        ' PaintHandles()
        If numPoints = 1 Then
            ' can't have a segment with only one point, go to PaintHandles()
            PaintHandles(g)
        ElseIf numPoints > 1 Then
            Dim p As New Pen(penClr, penWdth)

            ' Rotate the world so the line will draw at the correct angle
            ' if it has been rotated
            transformWorld(g)

            g.DrawLines(p, pts)
            p.Dispose()

            ' draw the handles and intersections on top of the lines
            PaintHandles(g)
        End If
    End Sub

Yes, it is a lot of code...but I think it will give a complete picture of the bigger problem of how to handle graphics manipulation in a class outside the GUI.

~IM
it was just a joke :-). you are free to post the code you consider will be useful for the asker.
I know it was joke. =)

But...I think you should build the app and play with it since

    me.creategraphics

is usually not a good way to create graphics on a form.  For the graphics to be persistent, they should be drawn from the paint event using the supplied graphics in the "e" parameter.

~IM
yeah you are right, actually i made a control to draw a line in a form.

Since he said he wanted a method to draw a line i gave him that code.

in that case he should call the DrawLine method from the form paint event and should use the "e" parameter as you proposed.

that's why i consider he should use the graphics object to draw lines in the form:

instead of

dim g as system.drawing.graphics = me.creategraphics

g.draw...

He should use the

e.Graphics.Draw....

in the paint event
Avatar of KenDickinson

ASKER

Thanks for the feedback folks...
A little more background. I'm fairly new to vb.net coming from the somewhat obscure Clarion language. In the past I've developed several graphics intensive programs (animated dots simulating marching bands on a football field, diagraming of greenhouses, benches within, and plants on benches for nurseries, and childrens games). These programs all share common code, for example, drawing a grid to assist in placing images. What I'm trying to do is place these common methods into their own class library (dll). That being said (and I apologize for not making that clear in the beginning), when I try the code submitted by both of you I get the following error:

type system.drawing not recognized.

I tried using the imports system.drawing but still got the error.

And then...   The light goes on...  I need to add a reference... Now everything works like it should.

To IdleMind - The code you provided answers a lot of other questions that I had. You should write a book. Thank you both for your responses. I am going to try and figure out how to award you both some points via the Split Points method.