B4J Question b4xCanvas seems to have changed its coordinate system when drawing a bitmap from assets and drawing a gradient bitmap from bitmap creator.

Hello guys,
In an exercise, I first drew bitmapA loaded from the asset folder and then drew bitmapB created by BitmapCreator.drawFillGradient. Both images have targeted the same rectangle.
I expect the second image to be the same size and position as the first image.
But as you can see in the attached screenshot, the size of the second drawing is only 1/4 of the size of the first one. And the coordinate origin seems to be wrong.
I would like to ask, is there something I set wrong? Or is the coordinate system itself designed to be different between drawing a loaded image and drawing a created graphic?


here is the full code:
the code in b4J:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    
    Private cvsX As B4XCanvas
    Private RectX As B4XRect
    Private GradientColors() As Int
    Private Pane1 As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    cvsX.Initialize(Pane1)
    RectX.Initialize(100dip,100dip,300dip,300dip)
    GradientColors=Array As Int(xui.Color_Blue,xui.Color_red)
    
    'load and draw bitmapA
    Private btmp As B4XBitmap=xui.LoadBitmapResize(File.DirAssets,"scene.jpg",RectX.Width,RectX.Height,False)
    cvsX.DrawBitmap(btmp,RectX)
    
    'draw bitmapB
    FillGradient(GradientColors,RectX,"LEFT_RIGHT")
End Sub



Private Sub FillGradient(colors() As Int, rect As B4XRect,orientation As String)
    Dim btmpCreator As BitmapCreator
    btmpCreator.Initialize(rect.Width,rect.Height)
    btmpCreator.FillGradient(colors,rect,orientation)   
    cvsX.DrawBitmap(btmpCreator.Bitmap,rect)
    

End Sub
 

Attachments

  • Snipaste_2024-03-02_20-13-40--.png
    Snipaste_2024-03-02_20-13-40--.png
    26.2 KB · Views: 42
Solution
Hi, Erel, I think I found where I did wrong.
When using bitmapCreator.fillgradient, I assumed that the rect parameter of it was in the same coordinate system as the rect parameter in canvas.drawBitmap - with the upper left corner of pane1 as the coordinate origin.
After more tests on bitmapCreator, I discovered this is not the case. The rect parameter in bitmapCreator.fillgradient actually takes the upper left corner of the bitmapCreator's internal bitmap as the origin.
Once I adjusted the code to look like this below, it displayed properly.

modified code:
Private Sub FillGradient(colors() As Int, rect As B4XRect,orientation As String)
    Dim btmpCreator As BitmapCreator
    
    'define a local rect for internal use
    Dim...
Hi, Erel, I think I found where I did wrong.
When using bitmapCreator.fillgradient, I assumed that the rect parameter of it was in the same coordinate system as the rect parameter in canvas.drawBitmap - with the upper left corner of pane1 as the coordinate origin.
After more tests on bitmapCreator, I discovered this is not the case. The rect parameter in bitmapCreator.fillgradient actually takes the upper left corner of the bitmapCreator's internal bitmap as the origin.
Once I adjusted the code to look like this below, it displayed properly.

modified code:
Private Sub FillGradient(colors() As Int, rect As B4XRect,orientation As String)
    Dim btmpCreator As BitmapCreator
    
    'define a local rect for internal use
    Dim localRect As B4XRect
    localRect.Initialize(0,0,rect.Width,rect.Height) 'set the left and top value to be 0, so that the btmpCreator can fill the whole internal bitmap.
    
    btmpCreator.Initialize(rect.Width,rect.Height)
    btmpCreator.FillGradient(colors,localRect,orientation)   
    cvsX.DrawBitmap(btmpCreator.Bitmap,rect)
    

End Sub
 
Upvote 0
Solution
Top