• 0

[VB.NET] Random Colors


Question

Hi,

I'm momentarely programming with GDI+ in Visual Basic .NET 2003.

I'm making a circle that gets filled with a LinearGradientBrush, no big deal so far.

Now I want it to be posible that each time i click a certain button, my brush consists of 2 random colors.

I got the following piece of code :

Private Sub CreateNewCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

		Dim paper As Graphics = Label1.CreateGraphics
		Dim radius As Integer = 280
		Dim circle1 As Rectangle = New Rectangle(300, 0, radius, radius)
		Dim colorA As Color = RandomRGBColor(255)
		Dim colorB As Color = RandomRGBColor(255)
		Dim brush1 As LinearGradientBrush = New LinearGradientBrush(circle1, colorA, colorB, LinearGradientMode.BackwardDiagonal)
		FillCircle(paper, circle1, brush1)

'Return a random RGB color.
	Private Function RandomRGBColor(ByVal alpha As Integer) As Color
		Dim m_Rnd As New Random()
		Return Color.FromArgb(alpha, _
			m_Rnd.Next(0, 255), _
			m_Rnd.Next(0, 255), _
			m_Rnd.Next(0, 255))
	End Function

Private Sub FillCircle(ByVal paper As Graphics, _
			ByVal circle As Rectangle, _
			ByVal brush As LinearGradientBrush)

		paper.FillEllipse(brush, circle)
	End Sub

Both colorA and colorB are random colors, though they always are the same ... (how come?)

So my circle always has 1 solid color as layout, instead of one color changing to a second one.

Am i overseeying something?

Thx in advance,

dfhwze

Link to comment
https://www.neowin.net/forum/topic/454494-vbnet-random-colors/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Randomize, that's what you need ;)

PS: Quick Re-write of your Code

Private Sub CreateNewCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

		Dim paper As Graphics = Label1.CreateGraphics
		Dim radius As Integer = 280
		Dim circle1 As Rectangle = New Rectangle(300, 0, radius, radius)
		Dim colorA As Color = RandomRGBColor(255)
		Dim colorB As Color = RandomRGBColor(255)
		Dim brush1 As LinearGradientBrush = New LinearGradientBrush(circle1, colorA, colorB, LinearGradientMode.BackwardDiagonal)
		FillCircle(paper, circle1, brush1)

'Return a random RGB color.
	Private Function RandomRGBColor(ByVal alpha As Integer) As Color
		Randomize()
		Dim m_Rnd As New Random()
		Return Color.FromArgb(alpha, _
			m_Rnd.Next(0, 255), _
			m_Rnd.Next(0, 255), _
			m_Rnd.Next(0, 255))
	End Function

Private Sub FillCircle(ByVal paper As Graphics, _
			ByVal circle As Rectangle, _
			ByVal brush As LinearGradientBrush)

		paper.FillEllipse(brush, circle)
	End Sub

Edited by phedot
  • 0

not necessarily

the way you were using the Random object was fine, othe than the fact that you were creating a new Random object in each call to randomcolor()

make a single instance once for the whole application and use that

[edit] the reason behind the colors being the same: the Random object uses the system tick count as a seed if no seed was given to create a seemingly-random number. (there actually is a pattern to the results :p). since you are creating new Random objects in each call to your randomized color method, they return the same value because they have the same seed.

is that understandable?

Edited by Ianmac45
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.