B4J Question How to use a custom mouse cursor icon

Discussion in 'B4J Questions' started by ThRuST, Jul 15, 2019.

  1. ThRuST

    ThRuST Well-Known Member Licensed User

    I'd like to change mouse cursor into a custom icon design and not the standard ones, is it possible to load a custom icon?

    Here's the icon that I use and it's great on 3 large monitors. However I want to set it through code and not manually.

    Btw you can use a plugin for Photoshop to load .ICO files and save it as .PNG

    The Icon plugin for Photoshop can be found here

    Attaching the Large arrow in PNG format
     

    Attached Files:

    Last edited: Jul 15, 2019
    Mashiane likes this.
  2. jmon

    jmon Well-Known Member Licensed User

    Hi,

    You can do it like that:

    Code:
    Sub SetCustomCursor(Dir As String, Filename As String, Width As Int, Height As Int, TargetNode As Node)
        
    Dim img As Image
        img.InitializeSample(Dir, Filename, Width, Height)
        
    Dim cursor As JavaObject
        
    cursor.InitializeNewInstance("javafx.scene.ImageCursor"Array(img))  
        
    Dim joScene As JavaObject = TargetNode  
        joScene.RunMethod(
    "setCursor"Array(cursor))
    End Sub
    This code allows you to set the custom cursor on any node. :)

    EDIT: Better Solution HERE (POST #7)
     

    Attached Files:

    Last edited: Jul 16, 2019
  3. ThRuST

    ThRuST Well-Known Member Licensed User

    Whoa!! that's impressive, hat off for that , oh lord master of code :)
     
    Mashiane likes this.
  4. ThRuST

    ThRuST Well-Known Member Licensed User

    I love your cursors man, they are SUPERB. I would love to implement them in my project Athena if you don't mind :)
    I should make it possible to change cursor in settings pane. Can't wait to get that working.
     
    Mashiane and jmon like this.
  5. jmon

    jmon Well-Known Member Licensed User

    Thanks!
    I'm not sure you can use them for commercial use, please check the license, I just downloaded a few by doing a simple search like that
     
    ThRuST, Johan Hormaza and Mashiane like this.
  6. Mashiane

    Mashiane Expert Licensed User

    Some cool stuff here hey... wow!
     
    ThRuST likes this.
  7. ThRuST

    ThRuST Well-Known Member Licensed User

    I'll look into those icons and see if I can buy a few. All for one and one for all B4X B4X B4X B4X, we're growing a cult here lol :)
     
    jmon likes this.
  8. ThRuST

    ThRuST Well-Known Member Licensed User

    I found this free Junior Icon editor and I cannot find a way how to set a hotspot. Please help me in this quest :)

    Go here to boldly embark on the quest of the wholy icons lol
     
  9. ThRuST

    ThRuST Well-Known Member Licensed User

    It looks like this

    upload_2019-7-15_20-27-48.png
     
  10. ThRuST

    ThRuST Well-Known Member Licensed User

    About settings the hotspot, it's in the end of the textfile for the Photoshop ICON Plugin

    Full text
    And here's how to set the Ruler in Photoshop..

    Youtube
     
    Last edited: Jul 15, 2019
  11. ThRuST

    ThRuST Well-Known Member Licensed User

  12. ThRuST

    ThRuST Well-Known Member Licensed User

    RealWorld Cursor editor has a bug that removes the toolbar completely, so please don't bother to use it.

    I found this instead, an online cursor editor, seems much easier to use
    https://www.cursor.cc/
     
  13. jmon

    jmon Well-Known Member Licensed User

    ThRuST likes this.
  14. ThRuST

    ThRuST Well-Known Member Licensed User

    Thanks I will look into that. I found out that IcoFX is the king of icon editors, however as from what I experienced it couldn't set the hotspot for the PNG file I tried.
    Not sure about the difference in those formats like ICO, CON, ANI since B4J uses PNG so if anyone has advice on this please share it. The hot spot is just important as the design itself, people will find out when they need it, as from yesterdays scouting for tutorials it was annoying to not find any software that could do this properly.
     
    Last edited: Jul 16, 2019
  15. ThRuST

    ThRuST Well-Known Member Licensed User

    There's also DeviantArt. Check it out here
     
  16. ThRuST

    ThRuST Well-Known Member Licensed User

    @jmon The size parameters doesn't seem to work?
     
  17. jmon

    jmon Well-Known Member Licensed User

    Yeah you're right, I didn't notice.
    There is an option for the hotspot too, but I don't really know how to use it, maybe it will make sense to you.
    Try this (I changed the width/height to hotspotX/Y position):
    Code:
    'Dir, 'Filename: Directory and filename of the cursor image.
    'hotspotX, hotspotY: X and Y Position of the hotspot of the cursor (where the click happens).
    'TargetNode: The node on which the cursor will be changed.
    Sub SetCustomCursor(Dir As String, Filename As String, hotspotX  As Double, hotspotY  As Double, TargetNode As Node)
        
    Dim img As Image
        img.Initialize(Dir, Filename)
        
    Dim cursor As JavaObject
        
    cursor.InitializeNewInstance("javafx.scene.ImageCursor"Array(img, hotspotX, hotspotY))
        
    Dim joScene As JavaObject = TargetNode
        joScene.RunMethod(
    "setCursor"Array(cursor))
    End Sub
    Edit: this is from JavaFX help:
     
    Last edited: Jul 16, 2019
  18. jmon

    jmon Well-Known Member Licensed User

    Ok, I get it now. For Example my PNG image is 500x500px. If you set :
    Code:
    SetCustomCursor(File.DirAssets, "cursor.png"250250, MainForm.RootPane)
    Then the cursor hotspot (click position) will be centered in the image (500px / 2 = 250). So
    Code:
    SetCustomCursor(File.DirAssets, "cursor.png"500500, MainForm.RootPane)
    will set it at the bottom right of the image. The default should be 0,0 then.
     
    inakigarm likes this.
  19. ThRuST

    ThRuST Well-Known Member Licensed User

    @jmon Great. I have created my own arrow cursor for my project and it looks cool. The size is 200x300 but it looked twice as wide in my app and size parameters had no effect i.e 100, 100 so I'll lab with those values. Not sure what values that was used but it looked better. Personally I like those larger fancy styled icons even though taste is different so I should add in all kinds of styles to choose from. Not to seem picky, but a new source code example would be great to illustrate the hot spot in a cursor test app prefferably showing where the drawing takes place from the x, y position. The B4J community needs our very own cursor editor with the hot spot, yay!! :)
     
  20. ThRuST

    ThRuST Well-Known Member Licensed User

    @jmon I have done some tests and your new code make wonders, the hot spot works as expected!! :) No need to call in the heavy artillery with us around lol
     
    jmon likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice