B4J Question mouse pointer icon to png image

Douglas Farias

Expert
Licensed User
Longtime User
hi all.
i m working on a game on b4j and i need change the icon of the mouse pointer in some places, like doors etc...

how can i change the mouse icon to a png icon?
i found here on the forum how change the icon to another icon of java fx but not how change to a png image.

i found this on google
have a example to change a icon to batman icon.
https://blog.idrsolutions.com/2014/05/tutorial-change-default-cursor-javafx/

i m tryed this here on b4j with this code

B4X:
    Private mouseimage As Image
    mouseimage.Initialize(File.DirAssets,"cursor.png")
    Dim joObj As JavaObject = MainForm.RootPane
    joObj.RunMethod("setCursor", Array(mouseimage))

but dont works.

how can i make this on b4j?

many thx
 

Cableguy

Expert
Licensed User
Longtime User
In b4j the cursor is set with

.MouseCursor = fx.cursors.xxxx

Can't remember if you can add an image to use
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Example
B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim img As Image
Dim b As Button
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
b.Initialize("")
b.Text = "Dummy"
MainForm.RootPane.AddNode(b,10,10,100,20)
img.Initialize("C:\temp","icontest.png")
imageCursor(b, img)
End Sub

Sub imageCursor(n As JavaObject,im As Image)
Dim icur As JavaObject
icur.InitializeNewInstance("javafx.scene.ImageCursor",Array(im,im.Width/2,im.Height/2))
n.RunMethod("setCursor",Array(icur))
End Sub

Sub asJO(o As JavaObject) As JavaObject
Return o
End Sub

The im.width/2 & im.height/2 just sets the position of the hotspot, in this case, to the centre of the image.
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Daestrum, your method works very well even with animated gifs, I attached a simple example.
 

Attachments

  • MousePointerAsAnimatedGif.zip
    240.2 KB · Views: 338
Last edited:
Upvote 0
Top