How to draw the points continues?

jimmylost

Member
Joined
Sep 9, 2005
Messages
6
Programming Experience
Beginner
I am not good at English, so hope you can understand my problem . :)
I want to ask why I need to add If mPoints.Count > 1 in the following program,?If I del this command,the program cannot be run.
And How can I draw the ellipse by more than one ? Now , I can only draw one ellipse and then stop.
Thx

Dim mPen As Pen = New Pen(Color.DarkBlue)
Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue)
Dim z As Integer
Private mPoints As ArrayList = New ArrayList
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim graphicsObject As Graphics = e.Graphics
If mPoints.Count > 1 Then
Dim pointArray() As Point = mPoints.ToArray(mPoints(0).GetType())
If rd_points.Checked Then
graphicsObject.DrawEllipse(mPen, pointArray(z).X, pointArray(z).Y, 10, 10)
End If
End If
End Sub
Private Sub Panel1_mousedown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
mPoints.Add(New Point(e.X, e.Y))
Panel1.Invalidate()
End Sub
Private Sub cmd_clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_clear.Click
mPoints = New ArrayList
End Sub
Private Sub cmd_changecolor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_changecolor.Click
Dim colorBox As ColorDialog = New ColorDialog
Dim result As DialogResult = colorBox.ShowDialog()
If result = DialogResult.Cancel Then
Return
End If
mPen.Color = colorBox.Color
mBrush.Color = colorBox.Color
Panel1.Invalidate()
End Sub
Private Sub rd_fillpoints_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rd_fillpoints.CheckedChanged
Panel1.Invalidate()
End Sub
 
Hey jimmylost,

you could try the following changes to get more than one ellipse

VB.NET:
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim graphicsObject As Graphics = e.Graphics
If mPoints.Count > 1 Then
Dim pointArray() As Point = mPoints.ToArray(mPoints(0).GetType())
 
dim TempPoint as Point
 
If rd_points.Checked Then
 
for each TempPoint in pointArray
graphicsObject.DrawEllipse(mPen, TempPoint.X, TempPoint.Y, 10, 10)
next
 
End If
End If
End Sub

I'm guessing the "If mPoints.Count > 1" is needed to stop it trying to paint ellipses if there are none in the array.
 
Thank you very much, I can draw more than one ellipse now :)
But why can't
For z = 0 To i
graphicsObject.DrawEllipse(mPen, pointArray(z).X, pointArray(z).Y, 10, 10)
Next
If I write this, only 2 ellipse can be draw.

If I want to link up the points by lines , how can I do?
 
Last edited:
VB.NET:
For z = 0 To i
graphicsObject.DrawEllipse(mPen, pointArray(z).X, pointArray(z).Y, 10, 10)
Next

Should work just as well, as long as (i) is set to the number of points.

To Join up lines, something like:-

VB.NET:
For z = 0 To pointArray.Length - 2
graphicsObject.DrawLine(mPen, pointArray(z).X, pointArray(z).Y, pointArray(z+1).X, pointArray(z+1).Y)
Next
 
Thank you very much :)
VB.NET:
Private Sub cmd_changecolor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_changecolor.Click
		Dim colorBox As ColorDialog = New ColorDialog
		Dim result As DialogResult = colorBox.ShowDialog()
		If result = DialogResult.Cancel Then
			Return
		End If
		mPen.Color = colorBox.Color
		mBrush.Color = colorBox.Color
		Panel1.Invalidate()
	End Sub
So I can change the color

If I want to use
line.jpg


to change the width, How can I do?

VB.NET:
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
		Panel1.Invalidate()
	End Sub
 
You need to create an integer that takes the value of the slider/track bar and then uses it as the pen's width when you are drawing your ellipse. I don't think it's necessarry to create a sub specifically to handle this, you can just call the value of the slider/track bar whenever you want to draw your ellipse.


VB.NET:
Dim penWidth as Integer

' ... and then within what ever sub is handling the drawing of your ellipse

penWidth = TrackBar1.Value()
 Dim mPen As Pen = New Pen(Color.DarkBlue, penWidth)
graphicsObject.drawEllipse(mPen, pointArray(z).X, pointArray(z).Y, 10, 10)
 
Back
Top