DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   VB.NET (http://www.daniweb.com/forums/forum58.html)
-   -   Drawing Multiple Lines (http://www.daniweb.com/forums/thread145568.html)

Anticipation Sep 13th, 2008 10:04 am
Drawing Multiple Lines
 
I'm using the code
Sub PicCanvasMouseDown(sender As Object, e As MouseEventArgs)
        XPrev = e.X
        YPrev = e.Y
End Sub
       
Sub PicCanvasMouseUp(sender As Object, e As MouseEventArgs)
        XCur = e.X
        YCur = e.Y
End Sub
       
Sub PicCanvasPaint(sender As Object, e As PaintEventArgs)
        Dim p As New Pen(Color.Black, 10)
        Dim g As Graphics = e.Graphics
        g.DrawLine(p, XPrev, YPrev, XCur, YCur)
        PicCanvas.Invalidate()
End Sub
To draw a line on a PictureBox. It works fine, until you go to draw another line, when the first line dissapears, i presume this is because i'm using the same integer variables for each one (XPrev, YPrev, XCur, YCur.) How would i be able to use the same integer variables, but be able to draw more than one line ?

selvaganapathy Sep 13th, 2008 2:41 pm
Re: Drawing Multiple Lines
 
Hi,
> Save the All lines Points (Make a class Line Has X and Y member and Save it in an ArrayList)
> Draw All the Lines (ArrayList) in Paint Event

Anticipation Sep 13th, 2008 2:58 pm
Re: Drawing Multiple Lines
 
That's not really what i meant. I'm capturing the points on mouse up and down, and that works, but only for one line. i want to be able to keep that line on the picturebox, and draw another, using the same variables for capturing.

selvaganapathy Sep 13th, 2008 3:12 pm
Re: Drawing Multiple Lines
 
Yes, I am also meant the same thing.

To draw all the lines you need to store it. so you should have an ArrayList or some other dynamic data structure.

ArrayList can have any object. so you need to group your Line into a class like
Public Class Line
  Public StartPoint As Point
  Public EndPoint As Point
End Class

Then create a new Line object whenever you draw line.
Sub PicCanvasMouseUp(sender As Object, e As MouseEventArgs)
        XCur = e.X
        YCur = e.Y
        Dim CurLine As New Line
        CurLine.StartPoint = New Point( XPrev,YPrev)
        CurLine.EndPoint = e.Location
        'Now Add to ArrayList for further use
        ArrayList.Add ( CurLine )
End Sub
To repaint, Iterate through the ArrayList and Draw all the Lines in the ArrayList.
Sub PicCanvasPaint(sender As Object, e As PaintEventArgs)
        Dim p As New Pen(Color.Black, 10)
        Dim g As Graphics = e.Graphics

        'Here Iterate all the Lines in the ArrayList and Draw It

        PicCanvas.Invalidate()
End Sub
This is what i meant.


All times are GMT -4. The time now is 9:59 pm.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC