arrow moving?

meetarun007

Member
Joined
Apr 26, 2010
Messages
7
Programming Experience
Beginner
Hi to all,
In my project I need to display an arrow moving when the user press the START button & it need to stop when stop button is pressed.
The moving area of arrow is fixed .ie, consider 100 pixels for the arrow movement. Once it reaches the end it has to start from the first.
I've the image of arrow at 4 different location(To display simultaneously).Once 4th image is displayed i need start from 1st image.
My teacher told to use both GDI+ and Timer(separate program) to do this.
Can somebody give some snippets(both models GDI-Timer) to clear this?:confused::confused:
 
Hi meetarun
You need a timer ticking at about 20 to 30 ticks a second. Every time the Timer ticks, you shift the picture along a little bit. That's the simplest kind of image animation.

So start off by dragging a Timer onto your Form. Set its Interval property to 25, and leave Enabled set to False for the moment. Then double-click the Timer to get the Timer.Tick sub. I'll assume you are using a picture box called PictureBox1 to draw the arrow.

In the Timer.Tick Sub, work out the position of the arrow. Put this on your form:
VB.NET:
   Dim XPosition As Integer = <[I]starting position[/I]>
Then put this in the Timer1.Tick sub:
VB.NET:
   XPosition += <[I]number of pixels to move[/I]>
   If XPosition > <[I]maximum pixel distance to move[/I]> Then XPosition = 0
   PictureBox1.Invalidate
Replace the bits between pointed brackets by suitable numbers. XPosition's starting position is where you draw the first arrow, starting from the left side of the picture box.

Now to draw the arrow on the picture box. You probably already know about setting a picture box Image or Background image, but they don't let you choose a position to draw it. Instead you have use a GDI+ drawing method in the PictureBox Paint sub. You can get the boilerplate code for PictureBox1.Paint from the drop-down boxes at the top of the Code view. XPosition is used to draw the arrow at the right position:
VB.NET:
e.Graphics.DrawImage(<image name>, XPosition, <Y position>)

Finally, you start the animation by setting Timer1.Enabled = True and stop it by Timer1.Enabled = False. That's something you could do in a Button Click event sub.

I hope this will be enough to get you started.
Vic
 
Thanks for ur reply.........
which process will be more efficient?
Since my project is huge plz tell me way which is the best in terms of system performance i.e., Timer method or GDI+?
 
Hi meetarun,

You always need some kind of timer, however you do it (and that includes Threading.Thread.Sleep for the wise guys;-).

The method I suggested uses both a Timer control and GDI+. GDI+ is just the regular part of DotNet that does graphics. You can also do some simple graphics by just changing the Image in a picture box, but the possibilities are very limited compared to GDI+ commands like Graphics.DrawImage.

Maybe what you are wondering is, what is the difference between using a Timer to change the position, and using a Timer to show different images? The short answer is: changing the position is theoretically more efficient. It won't make a big difference for a small image, but it's a technique you need to learn about anyway.

If your image actually changes shape while you are animating it, you would clearly need different images.

BB
 

Latest posts

Back
Top