graphing program

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
Can someone help me with this code. I let the user input the x and y co-ordinates of a graph, and the points are plotted. THe part I'm having trouble with is drawing a connecting line in between the points. THis is more of a logic problem I can't seem to work my head around. I've calculated the rise and run between two given points, but now I need to use that correctly. Also, is there some built in way to draw a line, because I was planning on making many pictureboxes appear, and make a line out of many connected dots.
 
Graphics class has got the DrawLine method that draw a line between two points and DrawLines method that draw lines through all points in given array of points. Either do the drawing in PictureBox Paint event through given graphics instance 'e.graphics' from event handler input parameter, or draw to a created Bitmap with graphics instance given by shared method Graphics.FromImage, if you choose the latter you assign the finished drawn bitmap image to the Image property of the Picturebox afterwards to display it. Here is link to Graphics class documentation where you can look into all and mentioned members for information and code examples: http://msdn2.microsoft.com/en-us/system.drawing.graphics.aspx
 
I've found that now, but I'm still having troubles. I looked up the graphics.drawline in the help menu and found this code:
Public Sub DrawLinePoint(e As PaintEventArgs)
' Create pen.
Dim blackPen As New Pen(Color.Black, 3)
' Create points that define line.
Dim point1 As New Point(100, 100)
Dim point2 As New Point(500, 100)
' Draw line to screen.
e.Graphics.DrawLine(blackPen, point1, point2)
End Sub

In my program, the points of the line are defined when the user hits the graph button. The progrm then reads the inputed data( x and y values for the points), and plots the points. However, when I try to draw a line between these two points it causes problems. It says something about not being set to an instance of the object. Do you have any ideas?
 
It says:
it requires PaintEventArgs e, which is a parameter of the Paint event handler
So you can put it in the Paint event:
VB.NET:
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Panel1.Paint
    DrawLinePoint(e)
End Sub
 
Back
Top