Freehand drawing application

trouble with brush color and brush size

Page 1 of 1

4 Replies - 8771 Views - Last Post: 19 December 2008 - 09:21 PM Rate Topic: -----

#1 mylifeishz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 20-May 08

Freehand drawing application

Post icon  Posted 18 December 2008 - 07:27 PM

Quote

Write a program that allows the user to draw 'free-hand' images with the mouse in a PictureBox. Be sure to provide the following:

A Button to allow users to clear the image in the PictureBox
A ComboBox to allow users to change the color of the pen
A set of radio buttons to set the drawing width
A menu containing at least the following items:
Clear
Change Color (then provide the colors that are in your ComboBox)
Change width (then provide the sizes in your RadioButtons)
Be sure that when the user makes a selection from the menu, the appropriate control (Button, ComboBox or RadioButton) is updated to reflect the user's choice



I have gotten to the point where the user is able to draw in my picture box but I cannot figure out how to change the color or size of the brush. Right now, you can see below, I have both the color and the size set to a specific color and size. Unfortunately I do not know how to take this out and put in a string that reads the combobox colors and the radiobuttons selection. I would like to set the default color to black and the default size to 10, 10. If you can help please let me know. Thanks so much.


Public Class Form1
	'Set shouldpaint to true/false to use for mousedown/mouseup
	Dim shouldpaint As Boolean = False

	'Form load section
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		ColorBox.SelectedItem(0) = Color.Aqua
		ColorBox.SelectedItem(1) = Color.Blue
		ColorBox.SelectedItem(2) = Color.Red
	End Sub
	Private Sub Drawpb_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseDown
		'Set drawpd to draw when mouse was held down
		shouldpaint = True
	End Sub

	Private Sub Drawpb_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseUp
		'Set drawpd not to draw when mouse was up
		shouldpaint = False
	End Sub
	Private Sub Drawpb_mousemove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseMove
		If shouldpaint Then
			'Declare g for graphics
			Dim g As System.Drawing.Graphics
			'set drawpad as site for freehand drawing
			g = Drawpb.CreateGraphics()
			'Set brush color as black and size of brush to 20, 20
			g.FillEllipse( _
New SolidBrush(Color.Black), e.X, e.Y, 20, 20)
		End If
	End Sub
	'Exit Button
	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		'End program
		Me.Close()
		End
	End Sub
	'Clear Button 
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		'Clear Drawpad
		Drawpb.Refresh()
	End Sub
	'Menu strip: Close
	Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
		'End Program
		Me.Close()
		End
	End Sub
	'Menu Strip: Clear
	Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
		'Clear Drawpad
		Drawpb.Refresh()
	End Sub
	'Color ComboBox
	Private Sub ColorBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorBox.SelectedIndexChanged
	
	End Sub
End Class


I also included a screen shot of my gui.

Please let me know if you can help me. Thanks again.

Attached File(s)

  • Attached File  Doc1.doc (164.5K)
    Number of downloads: 524


Is This A Good Question/Topic? 0
  • +

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9187
  • View blog
  • Posts: 34,489
  • Joined: 12-June 08

Re: Freehand drawing application

Posted 18 December 2008 - 09:36 PM

You need two variables one for color and one for size, defaulted, and your graphics program would use these.

As you can see in the load I have two combo boxes setup as list boxes and filled with various sizes and colors.

When those combo box values are changed then you use a case statement to set the global variables which are uses in your drawing.

Integrate as needed.



 '-- variable to hold the brush color
	Dim _CurrentColor As System.Drawing.Color = Color.Black
	'-- variable to hold the brush size
	Dim _lBrushSize As Int32 = 20
	'Form load section
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'-- ComboBox1 is a drop down list
		ComboBox1.Items.Add("Black")
		ComboBox1.Items.Add("Aqua")
		ComboBox1.Items.Add("Blue")
		ComboBox1.Items.Add("Red")
		ComboBox1.SelectedIndex = 0
		'-- ComboBox2 is a drop down list
		ComboBox2.Items.Add("20")
		ComboBox2.Items.Add("30")
		ComboBox2.Items.Add("40")
		ComboBox2.SelectedIndex = 0
	End Sub

	Private Sub Drawpb_mousemove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
		If shouldpaint Then
			'Declare g for graphics
			Dim g As System.Drawing.Graphics
			'set drawpad as site for freehand drawing
			g = Me.CreateGraphics()
			'Set brush color as black and size of brush to 20, 20
			'-- user your variables here for color and size
			g.FillEllipse(New SolidBrush(_CurrentColor), e.X, e.Y, _lBrushSize, _lBrushSize)
		End If
	End Sub

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		'-- when the index changes.. see what value there is and set the selected color
		Select Case ComboBox1.SelectedItem.ToString
			Case "Black"
				_CurrentColor = Color.Black
			Case "Aqua"
				_CurrentColor = Color.Aqua
			Case "Blue"
				_CurrentColor = Color.Blue
			Case "Red"
				_CurrentColor = Color.Red
			Case Else
				_CurrentColor = Color.Black
		End Select
	End Sub


	Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
		'-- when the index changes.. see what value there is and set the selected size
		Select Case ComboBox2.SelectedItem.ToString
			Case "20"
				_lBrushSize = 20
			Case "30"
				_lBrushSize = 30
			Case "40"
				_lBrushSize = 40
			Case Else
				_lBrushSize = 20
		End Select
	End Sub

Was This Post Helpful? 1

#4 mylifeishz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 20-May 08

Re: Freehand drawing application

Posted 19 December 2008 - 02:45 AM

Hi, thanks so much. I got some ideas from your code and now I have one more problem. I have everything working except the menu strip change color and menu strip change width. I am not sure how to code those to match what I have for the color combobox and the width radiobox.

Here is my code:
Public Class Form1
	Private shouldPaint As Boolean = False
	Private backgroundColor As Color
	Private foregroundColor As Color = Color.FromArgb(100, 0, 0, 255)
	Private buttontype As MessageBoxButtons
	Dim BrushSize As Integer
	Private Sub Drawpb_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseDown
		'Set drawpd to draw when mouse was held down
		shouldpaint = True
	End Sub

	Private Sub Drawpb_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseUp
		'Set drawpd not to draw when mouse was up
		shouldpaint = False
	End Sub
	Private Sub Drawpb_mousemove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Drawpb.MouseMove
		If rb1.Checked Then
			BrushSize = 5
		ElseIf rb2.Checked Then
			BrushSize = 10
		ElseIf rb3.Checked Then
			BrushSize = 15
		ElseIf rb4.Checked Then
			BrushSize = 20
		End If

		If shouldPaint Then
			Dim brush As New SolidBrush(Color.Black)
			brush.Color = foregroundColor
			Dim g As Graphics = CreateGraphics()
			g = Drawpb.CreateGraphics()
			g.FillEllipse(New SolidBrush(brush.Color), e.X, e.Y, BrushSize, BrushSize)
			g.Dispose()
		End If
	End Sub
	Private Sub ColorBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorBox.SelectedIndexChanged
		foregroundColor = Color.FromName(ColorBox.SelectedItem)
		Invalidate()
	End Sub
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Drawpb.Invalidate()
	End Sub
	Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
		Drawpb.Invalidate()
	End Sub
	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Me.Close()
		End
	End Sub
	Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
		Me.Close()
		End
	End Sub

	Private Sub ChangeWidthToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeWidthToolStripMenuItem.Click
		Dim result As DialogResult
		result = Windows.Forms.DialogResult.OK


	End Sub
End Class



Thanks again for helping me out. You have been a life saver.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9187
  • View blog
  • Posts: 34,489
  • Joined: 12-June 08

Re: Freehand drawing application

Posted 19 December 2008 - 08:37 AM

Oh.. they are in menu strips? Okay.. so you have your menu, right? You have your different named variables right? One menu strip called 'color' with four sub buttons and teh same for something called 'size', right?

Well each of those sub menus are just button clicks. in the GUI you double click to make them auto create an event method for you. You will be breaking apart my select statements and just having it assign the color in the method (or size)

---
menu_strip_Black_click
START
_CurrentColor = Color.Black
END

menu_strip_.Red_click
START
_CurrentColor = Color.Red
END

.. etc


Was This Post Helpful? 0
  • +
  • -

#6 mylifeishz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 20-May 08

Re: Freehand drawing application

Posted 19 December 2008 - 09:21 PM

Thank you so much. I got it. You are always helping me out and you are wonderful!!!!!

Thanks again!!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1