Drawing Multiple Lines

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 8
Reputation: Anticipation is an unknown quantity at this point 
Solved Threads: 0
Anticipation Anticipation is offline Offline
Newbie Poster

Drawing Multiple Lines

 
0
  #1
Sep 13th, 2008
I'm using the code
  1. Sub PicCanvasMouseDown(sender As Object, e As MouseEventArgs)
  2. XPrev = e.X
  3. YPrev = e.Y
  4. End Sub
  5.  
  6. Sub PicCanvasMouseUp(sender As Object, e As MouseEventArgs)
  7. XCur = e.X
  8. YCur = e.Y
  9. End Sub
  10.  
  11. Sub PicCanvasPaint(sender As Object, e As PaintEventArgs)
  12. Dim p As New Pen(Color.Black, 10)
  13. Dim g As Graphics = e.Graphics
  14. g.DrawLine(p, XPrev, YPrev, XCur, YCur)
  15. PicCanvas.Invalidate()
  16. 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 ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 520
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: Drawing Multiple Lines

 
0
  #2
Sep 13th, 2008
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
Selva
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 8
Reputation: Anticipation is an unknown quantity at this point 
Solved Threads: 0
Anticipation Anticipation is offline Offline
Newbie Poster

Re: Drawing Multiple Lines

 
0
  #3
Sep 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 520
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: Drawing Multiple Lines

 
0
  #4
Sep 13th, 2008
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
  1. Public Class Line
  2. Public StartPoint As Point
  3. Public EndPoint As Point
  4. End Class

Then create a new Line object whenever you draw line.
  1. Sub PicCanvasMouseUp(sender As Object, e As MouseEventArgs)
  2. XCur = e.X
  3. YCur = e.Y
  4. Dim CurLine As New Line
  5. CurLine.StartPoint = New Point( XPrev,YPrev)
  6. CurLine.EndPoint = e.Location
  7. 'Now Add to ArrayList for further use
  8. ArrayList.Add ( CurLine )
  9. End Sub
To repaint, Iterate through the ArrayList and Draw all the Lines in the ArrayList.
  1. Sub PicCanvasPaint(sender As Object, e As PaintEventArgs)
  2. Dim p As New Pen(Color.Black, 10)
  3. Dim g As Graphics = e.Graphics
  4.  
  5. 'Here Iterate all the Lines in the ArrayList and Draw It
  6.  
  7. PicCanvas.Invalidate()
  8. End Sub
This is what i meant.
Last edited by selvaganapathy; Sep 13th, 2008 at 3:17 pm.
Selva
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum


Views: 1284 | Replies: 3
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC