Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Making a Nice Wait Effect

Rate me:
Please Sign up or sign in to vote.
4.65/5 (7 votes)
27 Mar 2014CPOL2 min read 23.8K   254   18   11
Making a nice tailed round effect

Introduction

I was looking for a better wait effect for my applications. Then I made my own. In this tip, I'll show you how I've made my round effect. Better looking gives applications quality I think :)

I've written this code in VB.NET, but I think you can understand even if you don't know VB.NET. It's easy!

Image 1

Using the Code

It's simple, let's start by adding dimensionals we will use:

VB.NET
Public Class Form1
    Dim lastpos() As Integer               '{x,y}
    Dim lastangle As Integer = 0           'between 0 - 360
    Dim last20pos As New List(Of Point)    'tail 

Now add a timer, and set its interval as 10. Go to Timer_Tick event.

Here, we will use a simple trigonometry. First, make sure angle is between 0 and 360. It would be much process if it goes to big numbers.

VB.NET
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
If lastangle = 360 Then
     lastangle = 0
 End If 

Now we will roll the point to get a round effect starting this point: (origin.x + r, origin.y).

This formula will give us the next point: (origin.x + cos(a) * r , origin.y + sin(a) * r).

I used (250,250) as origin, and 15 as radius. And set 10 as change of angle at every step.

VB.NET
lastpos = {250 + Math.Cos((lastangle / 360) * 2 * Math.PI) * 15, 250 + Math.Sin((lastangle / 360) * 2 * Math.PI) * 15}

lastangle += 10 

We will add the last position to last20pos list which we defined at the beginning of code at the end of step.

The code below will make a tail for effect (background should be black. Or you'll need to change equation for another backcolor):

VB.NET
For i = 1 To last20pos.Count - 1

    Panel1.CreateGraphics.FillEllipse(New SolidBrush(Color.FromArgb(255 / (20 - i), 255 / (20 - i), 255 / (20 - i))), last20pos.Item(i).X, last20pos.Item(i).Y, 4, 4)

Next

Tail has 20 points, so if the list has 20 points, the first added point should turn to black and then be removed from the list.

VB.NET
If last20pos.Count >= 20 Then   
     Panel1.CreateGraphics.FillEllipse(Brushes.Black, last20pos.Item(0).X, last20pos.Item(0).Y, 4, 4) 

     last20pos.RemoveAt(0)
 
End If  

Lastly, add the lastpos into last20pos list.

VB.NET
last20pos.Add(New Point(lastpos(0), lastpos(1)))

Points of Interest

With simple trigonometry knowledge, you can make beautiful effects for users to enjoy it. Have fun :)

You can change the color by changing 255s in this code:

VB.NET
Panel1.CreateGraphics.FillEllipse(New SolidBrush(Color.FromArgb(255 / (20 - i), 255 / (20 - i), 255 / (20 - i))), last20pos.Item(i).X, last20pos.Item(i).Y, 4, 4) 

For example, add some buttons with different colors. Define CL as color. In button click event, set CL = button.backcolor. Write CL.R , CL.G , CL.B instead 255.

You can see the effect running in this video:

Upon request, here are the project files:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Home office
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionProject code Pin
SureshTheCoder27-Mar-14 0:49
SureshTheCoder27-Mar-14 0:49 
AnswerRe: Project code Pin
Richard MacCutchan27-Mar-14 1:22
mveRichard MacCutchan27-Mar-14 1:22 
GeneralRe: Project code Pin
MuhsinFatih27-Mar-14 1:30
MuhsinFatih27-Mar-14 1:30 
GeneralRe: Project code Pin
Richard MacCutchan27-Mar-14 1:40
mveRichard MacCutchan27-Mar-14 1:40 
GeneralRe: Project code Pin
MuhsinFatih27-Mar-14 1:44
MuhsinFatih27-Mar-14 1:44 
NewsRe: Project code Pin
Richard MacCutchan27-Mar-14 1:50
mveRichard MacCutchan27-Mar-14 1:50 
GeneralRe: Project code Pin
MuhsinFatih27-Mar-14 1:59
MuhsinFatih27-Mar-14 1:59 
GeneralRe: Project code Pin
Richard MacCutchan27-Mar-14 2:29
mveRichard MacCutchan27-Mar-14 2:29 
AnswerRe: Project code (Project files in this comment) Pin
MuhsinFatih27-Mar-14 1:27
MuhsinFatih27-Mar-14 1:27 
GeneralRe: Project code (Project files in this comment) Pin
Ravi Bhavnani27-Mar-14 3:49
professionalRavi Bhavnani27-Mar-14 3:49 
AnswerRe: Project code (Project files in this comment) Pin
MuhsinFatih27-Mar-14 4:10
MuhsinFatih27-Mar-14 4:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.