MX2 – example – Analog Clock

This is a simple example, an analog clock. Code found around internet and adapted.

The only difference is the need to convert Degree to Radian (the system used by Monkey2)

Namespace myapp

#Import "<std>"
#Import "<mojo>"

Using std..
Using mojo..


Class MyWindow Extends Window

 Method New()
 Super.New( "Monkey Clock",640,480)',WindowFlags.Resizable )
 ClearColor=New Color( .10,.20,.75 )
 End

 Method OnRender( canvas:Canvas ) Override
 
 App.RequestRender()
 
 Local cx:=320
 Local cy:=240
 Local radius:=cy*.9
 
 canvas.TextureFilteringEnabled=false
 canvas.BlendMode=BlendMode.Alpha
 
 
 canvas.Color=Color.Black
 canvas.DrawCircle(cx,cy,radius+2)
 
 
 canvas.Color=Color.White
 canvas.DrawCircle(cx,cy,radius)
 
 canvas.Color=Color.Black

 'draw the numbers
 Local ang:Float
 Local px:Float,py:Float
 

 For Local n:=0 To 59
 px=cx+SinD(n*6)*radius*.8
 py=cy+CosD(n*6)*radius*.8
 
 canvas.DrawPoint(px,py)
 Next
 canvas.Color=Color.Red
 For Local n:=1 To 12

 px=cx+SinD(n*30)*radius*.9
 py=cy-CosD(n*30)*radius*.9
 canvas.Color=New Color(.92,.92,.92)'Color.LightGrey
 canvas.DrawCircle(px,py,15)
 canvas.Color=Color.Black
 canvas.DrawText(n,px,py,.5,.5)
 Next
 
 Local ti:=Time.Now()
 
 Local hh:Int,mm:Int,ss:Int
 Local sh:Float=4 'offset for the shadow
 hh=ti.Hours
 mm=ti.Minutes
 ss=ti.Seconds
 
 Local sx:Float,sy:Float,mx:Float,my:Float,hx:Float,hy:Float
 
 
 'Calc the seconds
 sx=cx+SinD(ss*6)*radius*.90
 sy=cy-CosD(ss*6)*radius*.90
 
 'calculate the minutes
 mx=cx+SinD(mm*6)*radius*.85
 my=cy-CosD(mm*6)*radius*.85
 
 
 'calculate the hours
 hx=cx+SinD(hh*30+mm*.2)*radius*.75
 hy=cy-CosD(hh*30+mm*.2)*radius*.75
 
 'draw the shadows

 canvas.LineWidth=2.0
 canvas.Color=Color.Grey
 canvas.Alpha=.75
 canvas.DrawCircle(cx+sh,cy+sh,10)
 canvas.DrawLine(cx+sh,cy+sh,sx+sh,sy+sh)
 canvas.DrawLine(cx+sh,cy+sh,mx+sh,my+sh)
 canvas.LineWidth=8.0
 canvas.DrawLine(cx+sh,cy+sh,hx+sh,hy+sh)
 canvas.LineWidth=2.0
 
 
 
 canvas.Alpha=1.0
 canvas.Color=Color.Red
 canvas.DrawLine(cx,cy,sx,sy)
 
 canvas.Color=Color.Black

 canvas.DrawLine(cx,cy,mx,my)
 canvas.LineWidth=8.0
 canvas.DrawLine(cx,cy,hx,hy)

 canvas.DrawCircle(cx,cy,10)

 
 End
 
 Method OnUpdate()
 End
 

 
End

Function Main()

 New AppInstance
 New MyWindow
 App.Run()
End



Function SinD:Float(v:Float)
 Return Sin(Deg2Rad(v))
End Function

Function CosD:Float(v:Float)
 Return Cos(Deg2Rad(v))
End Function


Function Deg2Rad:Float( degrees:Float )
 Return degrees*Pi/180
End Function

#Rem monkeydoc Converts passed radians to degrees.
#End
Function Rad2Deg:Float( radians:Float )
 Return radians*180/Pi
End Function

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close