|
-
Jul 28th, 2012, 09:40 PM
#1
Thread Starter
New Member
"Erasing" a "randomly" colored line in VB?
Hello all,
I'm looking for a way of "erasing" a randomly colored line in vb.net. I don't actually think that there is a way to destroy the line, rather, I'm looking for a way to change the line color to the same color as the background, effectively "erasing" it. Also, I'm looking for a way to randomly generate the line colors, then change them to the background of the form as previously stated. The code for that is commented out.
It's a pretty simple program in general. I have it randomly creating the lines (every 4 seconds) in a single color, just not randomly generating line color and changing them to the background color to be "erased"
The buttons just speeds/slows the model.
lines Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BackColor = Color.Aquamarine
End Sub
Private Sub btnFaster_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFaster.Click
tmrTimer.Interval = tmrTimer.Interval - 10
End Sub
Private Sub btnSlower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSlower.Click
tmrTimer.Interval = tmrTimer.Interval + 10
End Sub
Private Sub tmrTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
'Dim inicolor As New Random()
'Dim intR, intG, intB As Integer
'intR = inicolor.Next(0, 256) 'generates random rgb number
'intG = inicolor.Next(0, 256)
'intB = inicolor.Next(0, 256)
'Dim hexR, hexG, hexB As String
'hexR = intR.ToString("X").PadLeft(2, "0"c) 'converts it to hex
'hexG = intG.ToString("X").PadLeft(2, "0"c)
'hexB = intB.ToString("X").PadLeft(2, "0"c)
'Dim strColor As String
'strColor = "#" & hexR & hexG & hexB
'Dim var = Drawing.Color.FromArgb(intR, intG, intB)
'Me.BackColor = var
Dim formSurface As Graphics = Me.CreateGraphics 'creates surface
Dim line As Integer = (Int((5 - 1 + 1) * Rnd() + 1))
Dim pen As New Pen(Color.Green, line)
Dim p1horizontal As Integer = (Int((300 - 0 + 1) * Rnd() + 0)) 'generates 4 random line coordinates
Dim p1vertical As Integer = (Int((300 - 0 + 1) * Rnd() + 0))
Dim p2horizontal As Integer = (Int((300 - 0 + 1) * Rnd() + 0))
Dim p2vertical As Integer = (Int((300 - 0 + 1) * Rnd() + 0))
formSurface.DrawLine(pen, p1horizontal, p1vertical, p2horizontal, p2vertical)
Media.SystemSounds.Beep.Play()
formSurface.DrawLine(pen, p1horizontal, p1vertical, p2horizontal, p2vertical)
End Sub
End Class
Thanks!
-
Jul 28th, 2012, 10:02 PM
#2
Re: "Erasing" a "randomly" colored line in VB?
You're going about this in very much the wrong way. You basically never call CreateGraphics. Anything you draw using GDI+ you draw on the Paint event of the control. Every time the control is repainted, which happens when the form is minimised and restored, has another form dragged over it and on many other occasions, all the drawing gets erased. That's why you have to draw it again.
To learn how to handle this sort of thing properly, follow the CodeBank link in my signature and check out my Simple Drawing thread. You just have to use the appropriate member variables to store the data that describes what you want to draw and add logic to the Paint event handler to turn that data into a drawing. If you want to change what is drawn, e.g. erase it, you simply modify the data ass appropriate and then force a Paint event to be raised.
-
Jul 28th, 2012, 10:17 PM
#3
Thread Starter
New Member
Re: "Erasing" a "randomly" colored line in VB?
I'm sorry, I can only use these specifications with creategraphics... it's dumb, i realize now but it's for school and I've gotta do what they tell me to do.
-
Jul 28th, 2012, 10:31 PM
#4
Re: "Erasing" a "randomly" colored line in VB?
Like I said, any GDI+ drawing gets erased every time the control you draw on gets repainted. If you want to erase your line you simply call Refresh on the control you drew on.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|