' Gambas class file
Private $hImg As Picture
Private $hATimer As New Timer As "ATimer"
Private iX As Integer ' logo X pos
Private iY As Integer ' logo Y pos
Private bXRev As Boolean ' logo X direction (reverse toggle)
Private bYRev As Boolean ' logo Y direction
Private iMoveAmount As Integer = 2
Public Sub Form_Open()
$hImg = Picture.Load("icon:/128/gambas") ' load logo
$hATimer.Delay = 20 ' (about 50fps)
$hATimer.Start
End
Public Sub Form_Close()
$hATimer.Stop
End
Public Sub ATimer_Timer()
' increase or decrease X and Y positions depending on direction
ix += If(bXRev, -iMoveAmount, iMoveAmount)
iy += If(bYRev, -iMoveAmount, iMoveAmount)
' change X direction if on an edge
If ix < iMoveAmount Then bXRev = False
If ix > DrawingArea1.W - $hImg.W - iMoveAmount Then bXRev = True
' change Y direction if on an edge
If iY < iMoveAmount Then bYRev = False
If iY > DrawingArea1.H - $hImg.H - iMoveAmount Then bYRev = True
DrawingArea1.Refresh ' trigger redraw
End
Public Sub DrawingArea1_Draw()
' nice gradient background
Paint.Brush = Paint.LinearGradient(0, 0, 0, Last.H, [Color.LightGray, Color.Gray, Color.DarkGray], [0, 0.5, 1])
Paint.Rectangle(0, 0, Last.W, Last.H)
Paint.Fill
' nice gradient logo background
Paint.Brush = Paint.LinearGradient(iX, iY, iX, iY + $hImg.H, [Color.Yellow, Color.Green], [0, 1])
Paint.Rectangle(iX, iY, $hImg.W, $hImg.H, 30)
Paint.Fill
Paint.DrawPicture($hImg, iX, iY, $hImg.W, $hImg.H)
End
Public Sub SpinBox1_Change()
iMoveAmount = Last.Value
End
In summary...
I create a timer with a delay of 20 and start it
In the timer event I increase or decrease the X and Y logo positions the distance of "iMoveAmount" (2) depending on their direction.
If X or Y have gone to a far edge then the direction is reversed.
"Bounce" is thus achieved.
The attached example program lets you change iMoveAmount to increase or decrease speed of movement.