Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Use transformations to draw an animated atom in Visual Basic .NET This example shows how to use transformations to draw an animated atom in Visual Basic .NET. animation, atom, animated atom, transformation, ellipse, rotated ellipse, rotation, VB.NET Algorithms, Graphics, Multimedia, VB.NET

When the form's Timer fires, the Timer event handler invalidates the form to force a redraw. The form's Paint event handler draws the atom.

For each electron, the Paint event handler uses the Graphics object's RotateTransform and TranslateTransform methods to prepare to draw rotated and translated objects. It then draws an ellipse centered at the origin. The rotation and scaling position it appropriately for the atom.

Then code then uses trigonometry to figure out where the electron should be on its orbit and draws a circle there.

The angle m_Alpha determines the electrons' positions. Note that the code multiplies m_Alpha by different values for the different electrons so they move at different speeds.

```Private m_Alpha As Single = 0
Private Const DALPHA As Single = PI / 10

' Draw the atom.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles _
MyBase.Paint
Const DTHETA As Double = Math.PI / 5
Static theta As Double = 0

e.Graphics.Clear(Me.BackColor)
e.Graphics.SmoothingMode = _
Drawing2D.SmoothingMode.AntiAlias
theta += DTHETA

Const E_RADIUS As Integer = 3
Dim cx As Integer = 50
Dim cy As Integer = 50
Dim rx As Integer = CInt(45)
Dim ry As Integer = CInt(15)
Dim rect As New Rectangle(-rx, -ry, 2 * rx, 2 * ry)
Dim x, y As Double
e.Graphics.RotateTransform(60, _
Drawing2D.MatrixOrder.Append)
e.Graphics.TranslateTransform(cx, cy, _
Drawing2D.MatrixOrder.Append)
e.Graphics.DrawEllipse(Pens.Red, rect)
x = rx * Cos(theta)
y = ry * Sin(theta)
e.Graphics.FillEllipse(Brushes.Red, _
CInt(x - E_RADIUS), CInt(y - E_RADIUS), _
2 * E_RADIUS, 2 * E_RADIUS)

e.Graphics.ResetTransform()
e.Graphics.RotateTransform(-60, _
Drawing2D.MatrixOrder.Append)
e.Graphics.TranslateTransform(cx, cy, _
Drawing2D.MatrixOrder.Append)
e.Graphics.DrawEllipse(Pens.Red, rect)
x = rx * Cos(-theta * 0.9)
y = ry * Sin(-theta * 0.9)
e.Graphics.FillEllipse(Brushes.Green, _
CInt(x - E_RADIUS), CInt(y - E_RADIUS), _
2 * E_RADIUS, 2 * E_RADIUS)

e.Graphics.ResetTransform()
e.Graphics.TranslateTransform(cx, cy, _
Drawing2D.MatrixOrder.Append)
e.Graphics.DrawEllipse(Pens.Red, rect)
x = rx * Cos(theta * 0.8)
y = ry * Sin(theta * 0.8)
e.Graphics.FillEllipse(Brushes.Blue, _
CInt(x - E_RADIUS), CInt(y - E_RADIUS), _
2 * E_RADIUS, 2 * E_RADIUS)

e.Graphics.ResetTransform()
Const N_RADIUS As Integer = 4
e.Graphics.FillEllipse(Brushes.Black, _
cx - N_RADIUS, cy - N_RADIUS, _
2 * N_RADIUS, 2 * N_RADIUS)
End Sub```

(Yes, I know this is not how atoms really work, but it looks cooler than a probability fog.)

 Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved. Updated