Selasa, 08 April 2014

Animasi Starfield dengan GDI+ VB.net

GDI+ merupakan evolusi dari GDI, yang membuat pengelolaan grafi k  menjadi jauh lebih mudah. Tip kali ini adalah memanfaatkan GDI+ untuk  mem buat animasi starfield.  Langkah-langkah untuk membuatnya adalah:  
  1. Buat New Project (Windows Application)  dengan sebuah form baru, dan sebuah picture  box ber nama pbstars. Property yang perlu diubah  adalah BackColor menjadi hitam, Dock menjad  Fill, WaitOnLoad menjadi True. 


  2. Buat sebuah class baru bernama clsStars. Class ini:                                                                                                                                                                                    

    Public Class clsStars
    Dim s_direction As Single = 0
    Dim s_angle As Double
    Dim center As Point
    Dim speed As Double = 0.2
    Dim s_accel As Double = 1.1
    Dim s_cd As Integer
    Dim s_x As Integer
    Dim s_y As Integer
    Dim s_pen() As Pen
    Dim s_hold As Double
    Dim s_depth As Integer = 2
    Dim s_count As Integer = 10
    Dim rn As New Random
    Private Structure fShip
    Dim spoint() As Point
    Dim cv As Single
    End Structure
    Private Structure Star
    Dim x As Single
    Dim y As Integer
    Dim z As Integer
    Dim xv As Double
    Dim yv As Double
    End Structure
    Dim Stars() As Star
  3. Beberapa subrutin yang dibutuhkan adalah untuk inisialisasi, dan mengatur  gerakan star yang muncul. Public Sub IntializeStar(ByVal depth As Integer, ByVal     
    count As Integer, ByVal x As Integer, ByVal y As Integer)    
    ReDim Stars(count)    
    ReDim s_pen(depth)    
    center = New Point(x / 2, y / 2)    
    s_count = count    
    s_depth = depth    
    s_x = x    
    s_y = y    
    s_cd = Int(255 / s_depth)    
    For x = 0 To s_count Step s_depth    
    For y = 1 To depth    
    Try        
        Stars(x + y).x = rn.Next(0, s_x)    
        Stars(x + y).y = rn.Next(0, s_y)    
        Stars(x + y).z = y    
        StarAngle(x + y)    
        If x = 0 Then s_pen(y) = New Pen(Color.     
    FromArgb(Int(s_cd * y), Int(s_cd * y), Int(s_cd * y)), 2)    
    Catch ex As Exception    
    End Try    
    Next    
    Next    
    End Sub    
    Private Sub SetShip(ByVal Value As Single)    
    s_angle += Value    
    If s_angle > 180 Then s_angle = -180    
    If s_angle < -180 Then s_angle = 180    
    If s_direction < 0 Then    
    s_direction += Value * 2   
     
     Else   
    s_direction += Value   
    End If   
    If s_direction > 360 Then s_direction = -360   
    End Sub   
    Private Sub StarAngle(ByVal Id As Integer)   
    Dim x As Integer = Stars(Id).x - (center.X)   
    Dim y As Integer = Stars(Id).y - (center.Y)   
    Dim a As Double = Math.Atan2(y, x)   
    Stars(Id).xv = 2 * Math.Cos(a)   
    Stars(Id).yv = 2 * Math.Sin(a)   
    End Sub   
    Public Sub DrawShip(ByVal b As System.Windows.Forms.    
    PaintEventArgs)   
    Dim x As Integer   
    b.Graphics.TranslateTransform(center.X, center.Y)   
    For x = 1 To s_depth   
    SetShip(0.01)   
    b.Graphics.RotateTransform(s_angle, MatrixOrder.    
    Prepend)   
    b.Graphics.DrawPie(s_pen(x), New RectangleF(0, 0,    
    150, 150), s_direction, s_direction)   
    b.Graphics.DrawPie(s_pen(x), New RectangleF(0, 0,    
    150, 150), s_direction, -s_direction)   
    Next   
    End Sub   
    Public Sub DrawStars(ByVal b As System.Windows.Forms.    
    PaintEventArgs)   
    Dim x As Integer   
    b.Graphics.Clear(Color.Black)   
    For x = 1 To UBound(Stars)   
    If Stars(x).y > s_y Or Stars(x).y < 0 Or Stars(x).x    
    > s_x Or Stars(x).x < 0 Then   
    Stars(x).x = rn.Next(0, s_x)   
    Stars(x).y = rn.Next(0, s_y)   
    StarAngle(x)   
    Else   
    Stars(x).x += Stars(x).xv   
    Stars(x).y += Stars(x).yv   
    Stars(x).xv *= s_accel + (0.001 * Stars(x).z)   
    Stars(x).yv *= s_accel + (0.001 * Stars(x).z)   
    End If   
    b.Graphics.DrawLine(s_pen(Stars(x).z),    
    CInt(Stars(x).x - Stars(x).xv), CInt(Stars(x).y - Stars(x).    
    yv), Stars(x).x, Stars(x).y)   
    Next   
    End Sub    
    End Class
       
  4. Event yang perlu dicegat untuk di class Form1 adalah sebagai                                                         Public Class Form1Dim sf As New clsStars
    Dim WithEvents tm As New Timer
    Private Sub Tm_Tick1(ByVal sender As Object, ByVal e
    As System.EventArgs) Handles tm.Tick
    Me.pbStars.Refresh()
    End Sub
    Private Sub Form1_FormClosing(ByVal sender As Object,
    ByVal e As System.Windows.Forms.FormClosingEventArgs)
    Handles Me.FormClosing
    sf = Nothing
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal
    e As System.EventArgs) Handles Me.Load
    tm.Interval = 15
    tm.Enabled = True
    Init()
    End Sub
    Private Sub Init()
    Dim tf As Font = New Font(“KaiTi”, 100,
    FontStyle.Regular, GraphicsUnit.Pixel)
    sf.IntializeStar(11, 190, Me.Width, Me.Height)
    End Sub
    Private Sub pbStars_Paint(ByVal sender As Object,
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles
    pbStars.Paint
    e.Graphics.SmoothingMode = SmoothingMode.
    AntiAlias
    sf.DrawStars(e)
    sf.DrawShip(e)
    End Sub
    End Class
  5. Dijalankan, tampilan yang dida patkan adalah:

Tidak ada komentar:

Posting Komentar

Artikel Terbaru