Okay, so as a project for one of my classes I need to make a basic drawing program in VB that can do three things:
- "Draw" with a brush of some sort
- Clear the screen
- Perform a flood-fill (paintbucket action)
I decided to do the first two tasks first, and got them to work, but the third task completely eludes me. Every way I try seems to pile on exceptions, and every flood-fill algorithm I find seems to require a bitmap, rather than a Graphics() object. Is there any way to accomplish a flood-fill using a Graphics object, or must I restart the project using a bitmap?
Source code for reference:
Public Class Form1
Public gfx As Graphics
Public mainBrush As New SolidBrush(Color.Black)
'creates a graphics surface when the program starts, disposes it when form closes.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gfx = pnlDraw.CreateGraphics()
dlgColor.Color = Color.Black
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
gfx.Dispose()
End Sub
'"draws" when the left mosue button is held down and moved, so long as the radiobutton to select draw is checked
Private Sub pnlDraw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlDraw.MouseMove
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
If radDraw.Checked = True Then
Dim rect As Rectangle
With rect
.X = e.X - (sizeSlider.Value / 2)
.Y = e.Y - (sizeSlider.Value / 2)
.Width = sizeSlider.Value
.Height = sizeSlider.Value
End With
gfx.FillEllipse(mainBrush, rect)
End If
End Sub
Private Sub sizeSlider_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles sizeSlider.Scroll
lblSize.Text = sizeSlider.Value
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
gfx.Clear(Color.White)
End Sub
'changes "brush" color
Private Sub colorSelect_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles colorSelect.DoubleClick
If dlgColor.ShowDialog() = DialogResult.OK Then
colorSelect.BackColor = dlgColor.Color
mainBrush.Color = dlgColor.Color
End If
End Sub
End Class