B4J Question Help is needed to draw rotated text

Erel

B4X founder
Staff member
Licensed User
Longtime User
Better, not perfect:
B4X:
Public Sub DrawTexts(Pn As B4XView, lstTexts As List, FontSize As Float, lstColors As List, Radius As Int, StrokeWidth As Int, CircleColor As Int)
    Dim xCanvas As B4XCanvas
    xCanvas.Initialize(Pn)
    Dim NumOfSectors As Int = lstTexts.Size
    
    Dim SectorsOffset As Double = 360 / NumOfSectors
    Dim CenterX, CenterY As Float
    CenterX = Pn.Width / 2
    CenterY = Pn.Height / 2
    Dim Angle As Float
    Dim xFont As B4XFont = xui.CreateDefaultFont(FontSize)
    Dim Text As String
    For i = 0 To NumOfSectors - 1
        Text = lstTexts.Get(i)
        Angle = 270 + i * SectorsOffset + SectorsOffset / 2
        Dim r As B4XRect = xCanvas.MeasureText(Text, xFont)
        Dim BaseLine As Int = CenterY - r.Height / 2 - r.Top
        xCanvas.DrawTextRotated(Text, CenterX + 100dip * CosD(Angle), BaseLine + 100dip * SinD(Angle), xFont, xui.Color_Black, "CENTER", Angle)
    Next
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thanks, Erel, I'll try.

The final version - random number of sectors, different icons & texts, sector colors - should look like this (drawn with Paint Net and not even this perfect ? :confused:):

Senza nome.png
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Sorry but why don't you follow Erel's advice to use B4XCanvas?

Sounds like good advice to me. The result doesn't look great
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Have you tried this library?

Maybe you shouldn't write vertically but horizontally, it wouldn't be warped
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Try adding a degree to the text rotation. Or bring back the rotation of the wheel with respect to the written.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Try adding a degree to the text rotation. Or bring back the rotation of the wheel with respect to the written.
I had already tried, even trying to calculate how much that increase must be.

I'm attaching the project (some things need to be changed; it is not correct that the number of sectors is deduced from the list of colors provided - and other stuff to change/remove, added just for tests)
 

Attachments

  • CreateWheel.zip
    15.5 KB · Views: 193
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I had already tried, even trying to calculate how much that increase must be.

I'm attaching the project (some things need to be changed; it is not correct that the number of sectors is deduced from the list of colors provided - and other stuff to change/remove, added just for tests)
I looked at my code and didn't understand anything about it.
So by exclusion I went to trial and error.
It seems to me, if I'm not mistaken because you don't understand anything, that I put half a point of a degree less on the wheel and one point more on the text
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Are you expecting this?

1609935440081.png


A text is, by default, positioned vertically on the text base line.
The base line is represented by the value 0 in in xRect you get with xRect = xCanvas.MeasureText(Sector.Text, xFont).
You need to take into account the distance between the base line and the center of the text.

This is taken into account in the code below:
B4X:
xRect = xCanvas.MeasureText(Sector.Text, xFont)
X = CenterX + HalfRadius * CosD(MiddleAngle) + SinD(MiddleAngle) * xRect.CenterY
Y = CenterY + HalfRadius * SinD(MiddleAngle) - CosD(MiddleAngle) * xRect.CenterY
xCanvas.DrawTextRotated(Sector.Text, X, Y, xFont, Sector.TextColor, "CENTER", MiddleAngle)
 

Attachments

  • CreateWheelNew.zip
    15.3 KB · Views: 183
Last edited:
Upvote 0
Top