 |
 |

07-29-2012, 03:47 PM
|
|
Newcomer
|
|
Join Date: Jul 2012
Posts: 4
|
|
Erasing drawn lines
|
Hey all,
I'm creating a program that creates random lines on a VB form. I have it creating the lines every 4 seconds, but how do I get the lines to erase after being drawn? I'm trying to get the color of the old line, after 4 seconds change to the background color, "erasing" the line. Is there a way for the 4-second timer to call another timer that, in an instant, colors the old line to the background color? The buttons just speed up/slow down the timer.
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))
Call callTimer(formSurface, line, pen, p1horizontal, p1vertical, p2horizontal, p2vertical)
'formSurface.DrawLine(Pens.Aquamarine, p1horizontal, p1vertical, p2horizontal, p2vertical)
End Sub
Function callTimer(ByRef formSurface As Graphics, ByRef line As Integer, ByRef pen As Pen, ByVal p1horizontal As Integer, _
ByVal p1vertical As Integer, ByVal p2horizontal As Integer, ByVal p2vertical As Integer)
'statements()
formSurface.DrawLine(pen, p1horizontal, p1vertical, p2horizontal, p2vertical)
Media.SystemSounds.Beep.Play()
Return formSurface
End Function
End Class
I don't know how to get the function that is being called to call a timer. If I can get that, then I think I should be able to solve the problem. I learned that using was bad VB coding, but I need to because this is a school project. Thanks!
|
|

07-29-2012, 07:48 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,722
|
|
|
How are you going to erase the line if you are not keeping track of the coordinates of the lines drawn?
You won't know where to draw the background colored line.
Just because it is a school project, does that mean they're forcing you to use the CreateGraphics method as a rule?
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

07-29-2012, 07:51 PM
|
|
Contributor
|
|
Join Date: Oct 2009
Posts: 719
|
|
Clear a form of drawn graphics
Have you looked at the Graphics.Clear Method?
Something like:
Code:
Private Sub ClearFormBackground(ByVal e As PaintEventArgs)
' Clear screen with form backcolor
e.Graphics.Clear(Me.BackColor)
End Sub
Then at whatever point you want the background cleared, call:
Code:
ClearFormBackground(e)
|
|

07-30-2012, 04:30 AM
|
 |
Senior Contributor
* Expert *
|
|
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,285
|
|
|
Difficult to know how to help school projects cos if you're not careful then it's obvious you've got the code from somewhere.
But, basically, you don't erase stuff from being drawn. You just don't bother drawing it in the first place.
Try not to think of the canvas as a workspace. Think of it as a graphical representation of information your program is storing.
So your program stores information (the lines) and when required it draws those lines onto the canvas.
Erasing a line means removing that line from your store and then redrawing your stored information onto the canvas again.
But yeah, that raises more questions than actually answering anything. But I can't type out all the code because it's a school project.
|
__________________
There are no computers in heaven!
|

07-30-2012, 08:07 PM
|
|
Newcomer
|
|
Join Date: Jul 2012
Posts: 4
|
|
|
The clearformbackground doesn't seem to work. It says e is not declared due to its protection level. I tried declaring it as a global, but then I just got the same result as before.
I went to debugging and it said that "e", which i had declared globally, could not be equal to infinity.
|
Last edited by ParaChase; 07-30-2012 at 08:18 PM.
Reason: Adding details
|

07-31-2012, 01:09 AM
|
|
Newcomer
|
|
Join Date: Oct 2010
Posts: 2
|
|
Hello
I think you have to declare a generic list of points to keep the coordinates
of all the lines
Code:
Public Class coordsline
Public point1 As New Point
Public point2 As New Point
End Class
Private listcoordinates As New List(Of coordsline)
Private Sub CreateRandomlines()
'create random points and add them to the list of coordinates
Dim rd As New Random
Dim lines As Integer
'create between 1 and 20 lines
lines = rd.Next(1, 21)
listcoordinates.Clear()
For iter As Integer = 0 To lines - 1
Dim pointline As New coordsline
pointline.point1.X = rd.Next(1, Me.Width)
pointline.point1.Y = rd.Next(1, Me.Height)
pointline.point2.X = rd.Next(1, Me.Width)
pointline.point2.Y = rd.Next(1, Me.Height)
listcoordinates.Add(pointline)
Next
End Sub
Private Sub Drawlines(ByVal gr As System.Drawing.Graphics)
'drawing the lines
Dim pencolor As New Pen(Color.RoyalBlue, 2)
For Each item As coordsline In listcoordinates
gr.DrawLine(pencolor, item.point1, item.point2)
Next
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
DirectCast(sender, Timer).Interval = 4000
CreateRandomlines()
Invalidate()
End Sub
'call graphic in form paint event
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Drawlines(e.Graphics)
End Sub
'don't forget to add timer1.start in the load event of the form
|
Last edited by shayw; 07-31-2012 at 11:44 AM.
|

07-31-2012, 01:17 PM
|
|
Contributor
|
|
Join Date: Oct 2009
Posts: 719
|
|
Trying for a simpler approach
|
I can see you had trouble understanding how to integrate the
snippets I provided in my last post into your project code.
Since your understanding of .Net graphics is obvious limited,
I have made up a complete project / solution using a boolean approach instead.
The boolean toggle is set via a button but could be callable
from anywhere in your program.
Also --the magic number "300"
(provided the code in your first post of this thread),
is probably a reference to the height and width of some default form size.
Using "Me.Width" and Me.Height" instead will allow lines to be draw anywhere on the form,
no matter what resizing of the form is done. (see attachment)
Overall, though, I recommend passel's approach,
(in post #6 of this thread), because keeping track of the lines
will allow selective erasing.
(for instance you could erase the lines in the reverse order that you created them).
|
Last edited by surfR2911; 07-31-2012 at 01:25 PM.
|

07-31-2012, 04:00 PM
|
|
Newcomer
|
|
Join Date: Jul 2012
Posts: 4
|
|
|
thanks surfR2911 for that neat model you showed me! I especially appreciate the Me.Width and Me.Height part of the program, and yes, my understanding of graphics is extremely limited. so is passel's approach just the un-doing of drawn lines, then redrawing them? thanks a lot
|
|

07-31-2012, 04:43 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,722
|
|
|
I was just stating a fact, based on your proposed method of erasing a line.
Since you wanted to clear an individual line by redrawing it with the background color, I was just noting, regardless of method, that if you don't keep track of where lines are drawn, you can't redraw them in the background color since you don't know where they are.
You would have to keep track of the lines drawn even if you use the more appropriate method, which would be to simply in the paint event redraw the lines you've already drawn that you want to keep, and not draw the ones that are "erased".
When the paint event happens, all the lines have already been erased, so you don't do anything specific to erase them. You just redraw the ones that are active, which is what shayw was saying, and his code snippet above is along those lines.
I would not generally recommend redrawing a line in the background color to erase it.
It would leave gaps in other lines that it overlapped, and would not really fit the way things should be done in .Net.
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

07-31-2012, 06:18 PM
|
|
Newcomer
|
|
Join Date: Jul 2012
Posts: 4
|
|
|
I see. What you said also gives me an idea. I could store those coordinates as 4 variables, then have a separate part of the code draw a line with the same color as the background over it using those variables. I'll try that and let you know the results. I've only been with VB for about a month's worth of learning time (50 min each work-day) and I want to look into graphics more.
|
|

08-01-2012, 08:44 AM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,419
|
|
|
I think you're on the right track, but making things harder than they need to be. There's pretty much no reason to ever use CreateGraphics() to draw on a form. There's some thread somewhere that I went into more detail, but it all comes down to "If you use CreateGraphics(), you have to reimplement half of Windows Forms yourself."
Here's how drawing in Windows Forms works. Occasionally, Windows decides it's time for you to redraw the form. So it erases the form, then raises the Paint event. This percolates down through all the child controls, and the form is redrawn. So in this case, if you want to draw different lines you change your data that describes what lines to draw, Invalidate(), and let Windows take care of the rest.
Here's how it works with CreateGraphics(). You get a device context and draw on the form. Later, Windows might decide it's time to redraw, but since you're not handling Paint your lines get erased. Whoops! So if you're drawing in this way, you have to make sure you handle Paint and do the same drawing there you do outside of it. Yuck! Plus, it leaves you worrying about stuff like "To erase the line I need to remember where it was, then draw another line over it with the same background color." It's silly.
There is no reason to guide anyone to use CreateGraphics() for this. It confuses me every time I see it. Who keeps telling people this is the right way?
The proper way to draw things in Windows Forms is to decide on a data structure that describes your shapes, then draw the shapes based on that data structure in the Paint event. I've attached an application that draws a new set of 50 randomly sized dots of random colors every time you click a button. First, I created the "Dot" class to represent a dot. Then I created an array of those to represent the dots that should be drawn. When the form loads, 50 dots are created and the Paint event will draw them. When you click the button, 50 new dots are created and Invalidate() causes the Paint event to be raised. This should give you some ideas for adapting it to create lines.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|