one click away

Kamis, 11 Oktober 2012

Jam Analog Menggunakan VB2010

Untuk membuat sebuah jam analog menggunakan VB2010 kita membutuhkan 12 label yang harus kita masukkan ke dalam form. Ubah text properties ke 12 labrl tersebut menjadi angka 1-12. Ubah Backcolor label tersebut menjadi White.



Tambahkan 2 timer dan 1 label lagi ke dalam form. Ubah nama label tersebut menjadi “Time”. Klik form1 kemudian klik view code dan masukkan coding berikut:

Public Class Form1

' tick digunakan untuk menggambar 60 tanda detik pada jam

Dim tick As Integer = 270

'tick2 digunakan untuk menggambar 12 tanda jam

Dim tick2 As Integer = 270

End Class


Klik 2 kali form1 dan masukkan coding berikut:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'lokasi jam pada form

Label1.Location = New Point(505, 203)

Label2.Location = New Point(561, 261)

Label3.Location = New Point(592, 335)

Label4.Location = New Point(561, 404)

Label5.Location = New Point(505, 464)

Label6.Location = New Point(430, 489)

Label7.Location = New Point(357, 463)

Label8.Location = New Point(305, 408)

Label9.Location = New Point(272, 334)

Label10.Location = New Point(302, 259)

Label11.Location = New Point(354, 200)

Label12.Location = New Point(430, 177)




Timer1.Enabled = True

Timer2.Enabled = True

End Sub


Klik 2 kali timer1 dan masukkan coding berikut:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

'mengubah detik pada lingkaran

Dim seconds As Integer = (Now.Second * 6) + 270

'mengubah menit pada lingkaran

Dim minutes As Integer = (Now.Minute * 6) + 270

'mengubah jam pada lingkaran

Dim hours As Integer = (Now.Hour * 30) + 270

'label text akan menampilkan jam pada komputer saat ini

Time.Text = Now

'membuat gambar jam

Dim g As Graphics

g = Me.CreateGraphics

Dim hour As New Pen(Color.Blue)

Dim hour2 As New Pen(Color.White)

Dim second As New Pen(Color.Black)

Dim minute As New Pen(Color.Red)

Dim minute2 As New Pen(Color.White)

Dim white As New Pen(Color.White)

Dim circle As New Pen(Color.Black)

hour.Width = 8

hour2.Width = 10

second.Width = 1

minute.Width = 4

minute2.Width = 4

white.Width = 10

circle.Width = 5

g.DrawPie(hour2, 319, 219, 240, 240, hours - 30, 360)

g.DrawPie(minute2, 289, 189, 300, 300, minutes - 6, 360)

g.DrawPie(Pens.White, 269, 169, 340, 340, seconds - 6, 360)

g.DrawPie(hour, 319, 219, 240, 240, hours, 360)

g.DrawEllipse(white, 319, 219, 240, 240)

g.DrawPie(minute, 289, 189, 300, 300, minutes, 360)

g.DrawEllipse(white, 289, 189, 300, 300)

g.DrawPie(second, 269, 169, 340, 340, seconds, 360)

g.DrawEllipse(white, 269, 169, 340, 340)

g.DrawEllipse(circle, 249, 149, 380, 380)

End Sub


Klik 2 kali timer2 dan masukkan coding berikut:

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

tick += 6

tick2 += 30

Dim g As Graphics

Dim hoursMarks As New Pen(Color.Black)

hoursMarks.Width = 5

g = Me.CreateGraphics

g.DrawPie(Pens.Black, 249, 149, 380, 380, tick, 360)

g.DrawPie(hoursMarks, 249, 149, 380, 380, tick2, 360)

g.DrawEllipse(Pens.White, 269, 169, 340, 340)

g.FillEllipse(Brushes.White, 269, 169, 340, 340)

If tick > 800 Then

Timer2.Stop()

tick = 270

tick2 = 270

End If

End Sub

Ubah WindowState properties pada form1 menjadi Maximized unutk melihat hasil yang lebih baik.

Tidak ada komentar:

Posting Komentar

Silahkan anda berikan komentar tapi sopan ya