B4J Code Snippet [ABMaterial] WindCompass on Canvas Method

next step for my transformation UI Domotica/weatherstation was the Compass
With help of the example of the clock it was transformed easy to a compass
iI7xTHn.jpg

B4X:
Sub Buildcompas(alfa As Double,knts As Int,Radius As Double) As ABMCanvasObject
   Dim compas As ABMCanvasObject

   Radius = Radius * 0.9
   Dim angle=(180-alfa)*cPI/180 As Double

'   // draw face
   compas.translate(Radius,Radius)
   compas.beginPath
   compas.arc(0,0,Radius, 0, 2*cPI)
   compas.fillStyleColor("#0000ff")
   compas.fill
   compas.strokeStyleRadialGradient(0,0,Radius*0.95,0,0,Radius*1.05, Array As Double(0,0.5,1), Array As String("#333", "white", "#7733"))
   compas.lineWidth(Radius*0.1)
   compas.stroke
   'draw needle
   compas.rotate(-angle) 'rotate first the picture
   compas.beginPath
   compas.lineWidth(1)
   compas.moveTo(-10,0)
   compas.lineTo(0,Radius*0.65)
   compas.lineTo(0,0)
   compas.lineTo(-10,0)
   compas.fillStyleColor("#ffff00")
   compas.fill

   compas.beginPath
   compas.moveTo(10,0)
   compas.lineTo(0,Radius*0.65)
   compas.lineTo(0,0)
   compas.lineTo(10,0)
   compas.fillStyleColor("#ff0000")
   compas.fill

   compas.rotate(angle) 'then rotate the picture back. The needle will be moved, the dial is back on its place

 
   compas.fillStyleColor("#ffffff")
   compas.font("arial", Radius*0.10)
   compas.textBaseline(ABM.CANVAS_TEXTBASELINE_MIDDLE)
   compas.textAlign(ABM.CANVAS_TEXTALIGN_CENTER)
   Dim const Bearing() As String=Array As String ("N","NNW","NW","WNW","W","WZW","ZW","ZZW","Z","ZOZ","ZO","OZO","O","ONO","NO","NNO")
 
   Dim ang As Double
   For num = 0 To  15
     ang = -num * cPI / 8
     compas.rotate(ang)
     compas.translate(0, -Radius*0.75)
     compas.rotate(-ang)
     compas.fillText("" & Bearing(num), 0, 0)
     compas.rotate(ang)
     compas.translate(0, Radius*0.75)
     compas.rotate(-ang)
   Next
   'draw center
   compas.beginPath
   compas.fillStyleColor("#ffff00")
   compas.arc(0,0,Radius*0.1,0,2*cPI)
   compas.fill
   'draw wind (knts)
   compas.fillStyleColor("#ffff00")
   compas.fillText(knts&" knts",0,Radius*0.5)
   Return compas
End Sub

call it in the connectpage with
B4X:
  Dim canvas2 As ABMCanvas
   canvas2.Initialize(page, "canvas2", ABM.COLOR_TRANSPARENT, ABM.INTENSITY_NORMAL, 300,300, False)

   Dim compas As ABMCanvasObject = Buildcompas(Main.meteovalues.winddir,Main.meteovalues.average,100)
   canvas2.AddObject(compas)

   page.Cell(1,1).AddComponent(canvas2)

update file: fixed bug in compass, add a line in rainbucket
 

Attachments

  • MeteoGauges.bas
    13.4 KB · Views: 422
Last edited:
Top