Rabu, 30 November 2016

Program Animasi LED menggunakan Visual Basic .Net

Tidak ada komentar:
Untuk membuat program yang kita buat agar terlihat menarik, adakalanya kita tambahankan berupa hiasan animasi pada program tersebut. Animasi LED cukup menarik jika kita kombinasikan ke dalam program yang kita buat

Sekarang anda buka Microsoft Visual Studio anda, buat project baru dengan memilih menu File -> New - Project dan simpan dengan nama Led.
 


Pertama kita akan membuat sebuah Control yang berfungsi untuk menampilkan animasi LED. Caranya dengan memilih menu Project -> Add User Control. Ubah objek Control tersebuat dengan nama Leder kemudian pilih tombol Add.
 

Desainlah Control Leder menjadi seperti dibawah ini
 

Klik kanan objek Leder kemudian pilih View Code lalu ketikkan kode berikut.


Option Explicit On

Imports System.Drawing.Drawing2D
Imports System.ComponentModel

Public Class Leder
    Dim InputString As String = ""
    Dim MyNumber As String = ""
    Dim CurrentDigit As Integer
    Dim OffSet As Double
    Dim MySharpie As Pen

    ' Show Border property.
    Private ShowBorder As Boolean = vbTrue
     Public Property ShowLEDBorder() As Boolean
        Get
            Return ShowBorder
        End Get
        Set(ByVal value As Boolean)
            ShowBorder = value
            Me.Refresh()
        End Set
    End Property

    ' Display String property.
    Private DisplayVal As String = ""
     Public Property DisplayValue() As String
        Get
            Return DisplayVal
        End Get
        Set(ByVal value As String)
            DisplayVal = value
            Me.Refresh()
        End Set
    End Property

    ' DigitCount property.
    Private LEDz As Integer = 2
     Public Property DigitCount() As Integer
        Get
            Return LEDz
        End Get
        Set(ByVal value As Integer)
            LEDz = value
            If LEDz < 1 Then
                LEDz = 1
            End If
            Me.Refresh()
        End Set
    End Property

    ' DigitColor property.
    Private SegBorderColor As Pen = Pens.Aquamarine
     Public Property SegmentBorderColor() As Pen
        Get
            Return SegBorderColor
        End Get
        Set(ByVal value As Pen)
            SegBorderColor = value
            Me.Refresh()
        End Set
    End Property

    ' DarkBlipColor property.
    Private UnLitColor As Brush = Brushes.Black
     Public Property SegmentOffColor() As Brush
        Get
            Return UnLitColor
        End Get
        Set(ByVal value As Brush)
            UnLitColor = value
            Me.Refresh()
        End Set
    End Property

    ' DigitColor property.
    Private LEDBackColor As Brush = Brushes.Black
     Public Property BackLightColor() As Brush
        Get
            Return LEDBackColor
        End Get
        Set(ByVal value As Brush)
            LEDBackColor = value
            Me.Refresh()
        End Set
    End Property

    ' DigitColor property.
    Private LEDColor As Brush = Brushes.Teal
     Public Property DigitColor() As Brush
        Get
            Return LEDColor
        End Get
        Set(ByVal value As Brush)
            LEDColor = value
            Me.Refresh()
        End Set
    End Property

    Private Sub Segment7_ControlRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlRemoved
        Dispose()
    End Sub

    Private Sub Segment7_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.ResizeRedraw = True
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        e.Graphics.ScaleTransform((Me.Width / DigitCount) / 120, Me.Height / 160, MatrixOrder.Append)
        MySharpie = SegBorderColor
        Dim CTR As Integer
        CurrentDigit = 0
        InputString = DisplayValue
        InputString = UCase(InputString)
        If InputString = "" Then
            For CTR = DigitCount - 1 To 0 Step -1
                MyNumber = ""
                OffSet = CTR * 120
                DrawBar1(e.Graphics)
                DrawBar2(e.Graphics)
                DrawBar3(e.Graphics)
                DrawBar4(e.Graphics)
                DrawBar5(e.Graphics)
                DrawBar6(e.Graphics)
                DrawBar7a(e.Graphics)
                DrawBar7b(e.Graphics)
                DrawBarUL(e.Graphics)
                DrawBarUR(e.Graphics)
                DrawBarLL(e.Graphics)
                DrawBarLR(e.Graphics)
                DrawDots(e.Graphics)
                DrawBarCT(e.Graphics)
                DrawBarCB(e.Graphics)
                OffSet = OffSet + 120
            Next
            Exit Sub
        End If
        If Len(InputString) > DigitCount Then InputString = "E"
        InputString = StrReverse(InputString)
        For CTR = DigitCount - 1 To 0 Step -1
            CurrentDigit = CurrentDigit + 1
            If CurrentDigit <= Len(InputString) Then
                MyNumber = Mid(InputString, CurrentDigit, 1)
            Else
                MyNumber = ""
            End If
            OffSet = CTR * 120
            DrawBar1(e.Graphics)
            DrawBar2(e.Graphics)
            DrawBar3(e.Graphics)
            DrawBar4(e.Graphics)
            DrawBar5(e.Graphics)
            DrawBar6(e.Graphics)
            DrawBar7a(e.Graphics)
            DrawBar7b(e.Graphics)
            DrawBarUL(e.Graphics)
            DrawBarUR(e.Graphics)
            DrawBarLL(e.Graphics)
            DrawBarLR(e.Graphics)
            DrawDots(e.Graphics)
            DrawBarCT(e.Graphics)
            DrawBarCB(e.Graphics)
            OffSet = OffSet + 120
        Next
    End Sub

    Private Sub DrawBar1(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 12, 8), New Point(OffSet + 28, 24), New Point(OffSet + 28, 72), New Point(OffSet + 20, 79), New Point(OffSet + 12, 79)}
        Select Case MyNumber
            Case "4", "5", "6", "8", "9", "0", "A", "B", "C", "D", "E", "F", "G", "H", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "U", "V", "W", "?"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder = True Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBar2(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 14, 6), New Point(OffSet + 106, 6), New Point(OffSet + 90, 22), New Point(OffSet + 30, 22)}
        Select Case MyNumber
            Case "2", "3", "5", "6", "7", "8", "9", "0", "A", "B", "C", "E", "F", "G", "I", "O", "P", "Q", "R", "S", "T", "Z", "?"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBar3(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 108, 8), New Point(OffSet + 108, 79), New Point(OffSet + 100, 79), New Point(OffSet + 92, 72), New Point(OffSet + 92, 24)}
        Select Case MyNumber
            Case "1", "2", "3", "4", "7", "8", "9", "0", "A", "H", "J", "M", "N", "O", "P", "Q", "R", "U", "W", "?"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBar4(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 12, 82), New Point(OffSet + 20, 82), New Point(OffSet + 28, 90), New Point(OffSet + 28, 136), New Point(OffSet + 12, 152)}
        Select Case MyNumber
            Case "2", "6", "8", "0", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "U", "V", "W"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBar5(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 14, 154), New Point(OffSet + 30, 138), New Point(OffSet + 90, 138), New Point(OffSet + 106, 154)}
        Select Case MyNumber
            Case "2", "3", "5", "6", "8", "0", "B", "C", "E", "G", "I", "J", "L", "O", "Q", "S", "U", "Z"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBar6(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 100, 82), New Point(OffSet + 108, 82), New Point(OffSet + 108, 152), New Point(OffSet + 92, 136), New Point(OffSet + 92, 90)}
        Select Case MyNumber
            Case "1", "3", "4", "5", "6", "7", "8", "9", "0", "A", "B", "G", "H", "J", "M", "N", "O", "Q", "S", "U", "W"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBar7a(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 23, 81), New Point(OffSet + 31, 73), New Point(OffSet + 51, 73), New Point(OffSet + 59, 81), New Point(OffSet + 51, 90), New Point(OffSet + 32, 90)}
        Select Case MyNumber
            Case "2", "3", "4", "5", "6", "8", "9", "A", "B", "E", "F", "H", "K", "P", "R", "S", "-"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBar7b(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 61, 81), New Point(OffSet + 68, 73), New Point(OffSet + 89, 73), New Point(OffSet + 97, 81), New Point(OffSet + 88, 90), New Point(OffSet + 68, 90)}
        GRFX.FillPolygon(LEDColor, PNTZ)
        Select Case MyNumber
            Case "2", "3", "4", "5", "6", "8", "9", "A", "B", "G", "H", "P", "R", "S", "-", "?"
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBarUL(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 30, 24), New Point(OffSet + 40, 24), New Point(OffSet + 59, 58), New Point(OffSet + 59, 78), New Point(OffSet + 53, 72), New Point(OffSet + 30, 34)}
        Select Case MyNumber
            Case "D", "M", "N", "X", "Y"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBarUR(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 80, 24), New Point(OffSet + 90, 24), New Point(OffSet + 90, 34), New Point(OffSet + 66, 72), New Point(OffSet + 61, 78), New Point(OffSet + 61, 58)}
        Select Case MyNumber
            Case "B", "K", "M", "Y", "V", "X", "Z", "/", "'"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBarLL(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 30, 136), New Point(OffSet + 30, 126), New Point(OffSet + 52, 92), New Point(OffSet + 59, 84), New Point(OffSet + 59, 108), New Point(OffSet + 40, 136)}
        Select Case MyNumber
            Case "D", "V", "W", "X", "Z", "/"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBarLR(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 61, 84), New Point(OffSet + 67, 92), New Point(OffSet + 90, 126), New Point(OffSet + 90, 136), New Point(OffSet + 80, 136), New Point(OffSet + 61, 108)}
        Select Case MyNumber
            Case "K", "N", "Q", "R", "W", "X"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                GRFX.FillPolygon(UnLitColor, PNTZ)
        End Select
    End Sub

    Private Sub DrawBarCT(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 56, 24), New Point(OffSet + 68, 24), New Point(OffSet + 68, 44), New Point(OffSet + 60, 54), New Point(OffSet + 56, 44)}
        Select Case MyNumber
            Case "I", "T"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder = True Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case Else
                Dim PNTZ2() As Point = {New Point(OffSet + 56, 24), New Point(OffSet + 68, 24), New Point(OffSet + 68, 44), New Point(OffSet + 60, 54), New Point(OffSet + 56, 44)}
                GRFX.FillPolygon(UnLitColor, PNTZ2)
        End Select
    End Sub

    Private Sub DrawBarCB(ByVal GRFX As Graphics)
        Dim PNTZ() As Point = {New Point(OffSet + 56, 154), New Point(OffSet + 56, 92), New Point(OffSet + 60, 82), New Point(OffSet + 68, 92), New Point(OffSet + 68, 154)}
        Select Case MyNumber
            Case "T", "Y"
                GRFX.FillPolygon(LEDColor, PNTZ)
                If ShowBorder = True Then GRFX.DrawPolygon(SegBorderColor, PNTZ)
            Case "I", "?"
                Dim PNTZ3() As Point = {New Point(OffSet + 56, 136), New Point(OffSet + 56, 92), New Point(OffSet + 60, 82), New Point(OffSet + 68, 92), New Point(OffSet + 68, 136)}
                GRFX.FillPolygon(LEDColor, PNTZ3)
                If ShowBorder = True Then GRFX.DrawPolygon(SegBorderColor, PNTZ3)
            Case Else
                Dim PNTZ2() As Point = {New Point(OffSet + 56, 136), New Point(OffSet + 56, 118), New Point(OffSet + 60, 110), New Point(OffSet + 68, 118), New Point(OffSet + 68, 136)}
                GRFX.FillPolygon(UnLitColor, PNTZ2)
        End Select
    End Sub

    Private Sub DrawDots(ByVal GRFX As Graphics)
        Dim MYX As Integer
        MYX = OffSet
        If DisplayValue = "" Then Exit Sub
        Select Case MyNumber
            Case ":"
                Dim dot_Pen1 As Pen
                dot_Pen1 = SegBorderColor
                GRFX.FillEllipse(LEDColor, MYX + 52, 49, 16, 16)
                If ShowBorder = True Then GRFX.DrawEllipse(dot_Pen1, MYX + 52, 49, 16, 16)

                GRFX.FillEllipse(LEDColor, MYX + 52, 101, 16, 16)
                If ShowBorder = True Then GRFX.DrawEllipse(dot_Pen1, MYX + 52, 101, 16, 16)
            Case "."
                Dim dot_Pen2 As Pen
                dot_Pen2 = SegBorderColor
                GRFX.FillEllipse(LEDColor, MYX + 52, 138, 16, 16)
                If ShowBorder = True Then GRFX.DrawEllipse(dot_Pen2, MYX + 52, 138, 16, 16)
            Case "?"
                Dim dot_Pen3 As Pen
                dot_Pen3 = SegBorderColor
                GRFX.FillEllipse(LEDColor, MYX + 52, 141, 16, 16)
                If ShowBorder = True Then GRFX.DrawEllipse(dot_Pen3, MYX + 52, 141, 16, 16)
        End Select
    End Sub
End Class

Desain Form1 menjadi seperti gambar dibawah ini. Adapun Control LED Leder merupakan Control yang telah dibuat sebelumnya. Ambil dari Tool Box yang berada di sebelah kiri Visual Studio anda.
 
Tambahkan Timer dan ubah bagian properties pada Interval menjadi 200. Klik kanan Form1 kemudian pilih View Code lalu ketikkan kode berikut.


Public Class frmMain
    Dim STR As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        STR = "Selamat Data di Situs salamilmu.com, Terima Kasih...                                     "
        Static CTR As Integer
        CTR = CTR + 1
        If CTR < led.DigitCount Then
            led.DisplayValue = Mid(STR, 1, CTR)
        Else
            led.DisplayValue = Mid(STR, CTR - 17, led.DigitCount)
        End If
        If CTR = Len(STR) - led.DigitCount - 1 Then
            CTR = 0
            Timer1.Enabled = False
        End If
    End Sub

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        led.DigitColor = Brushes.DarkViolet
        led.SegmentBorderColor = Pens.Fuchsia
        led.SegmentOffColor = Brushes.Black
        led.BackColor = Color.Black
    End Sub

End Class


Jika sudah selesai jalan project anda dengan menekan tombol F5. Klik pada tombol Start maka animasi Running Text LED pun berjalan.
 

Anda bisa mendownload contoh programnya disini https://www.4shared.com/rar/gD8oZJBhce/Led.html
, ,

Tidak ada komentar:

Posting Komentar