Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Change The Mouse Cursor In Java

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Change The Mouse Cursor In Java

    Hello! I'm doing a little play and among the many things I decided to change the mouse cursor based on the theme of the game.
    This is the code that I wrote for the declaration of the cursor:

             ImageIcon imageIcon = new ImageIcon ("cursor / cursor.gif"); 
             Image img = imageIcon.getImage (); 
             Toolkit t = Toolkit.getDefaultToolkit (); 
             Cursor cursor = t.createCustomCursor (img, new Point (0,0), "cur"); 
             this.setCursor (cursor);

    Having to click on different JButton in the menu of the game, a few screens later, I have to manage something in particular, such as determining the point of clicks that allows the action to take place?

    --- Update ---

    In particular I would like to change the point of hotspots. The size of 136x163 is cursor.gif

    Image - TinyPic - Servizio di hosting d'immagini, condivisione immagini & hosting di video gratuito

    As you can see in the image I want to create the point of hotspots in the red dot of the image. How should I do to pass the correct coordinates in Point?


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Change The Mouse Cursor In Java

    Quote Originally Posted by Nemesis89 View Post
    In particular I would like to change the point of hotspots. The size of 136x163 is cursor.gif
    One important note: the cursor cannot have any dimension you like! The real effective size is determined by S.O./hardware settings and/or limits.
    Toolkit has the getBestCursorSize method to find the supported cursor dimension.
    If you create a Cursor with a larger image, this image is resized to fit that dimension. And this would also require a recalculation of the hotspot.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Change The Mouse Cursor In Java

    Thanks anyway for the clarification, but I don't want the cursor has this dimension, I wrote for information, in fact, writing code on the back has been adapted to the classical size of the cursor. My question is another: make sure that the point of hotspot is at a given point (as seen in the image) and not (0,0)

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Change The Mouse Cursor In Java

    Quote Originally Posted by Nemesis89 View Post
    as seen in the image
    Sorry, image access at tinypic is forbidden ... at least for me at this moment.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Change The Mouse Cursor In Java

    Now it is visible?

    cursor.gif

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Change The Mouse Cursor In Java

    Quote Originally Posted by Nemesis89 View Post
    Now it is visible?
    Yes but ... I still don't understand your necessity/problem. I see a 136x163 pixels image. Is this the cursor image you load? As I told before, you cannot have a 136x163 cursor.
    If you create a correct cursor image (typical/common is 32x32), you can assign the hotspot at any location in (0,0)...(31,31). But this is an implicit knowledge that you must have on the cursor image. If you pass e.g. new Point (10,13) it's because you know that the cursor hotspot is at that point. Nor the image file, nor the java.awt.Image object, nor AWT/Swing in general can have/discover this knowledge in some way.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Change The Mouse Cursor In Java

    I agree with what you said.
    This image is loaded and displayed as a normal cursor.
    If I had this same image of size 32x32 I could have the point of hotspots at the point indicated by me in the picture? Is a possible thing that I would do?

  8. #8
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Change The Mouse Cursor In Java

    Quote Originally Posted by Nemesis89 View Post
    I agree with what you said.
    This image is loaded and displayed as a normal cursor.
    If I had this same image of size 32x32 I could have the point of hotspots at the point indicated by me in the picture? Is a possible thing that I would do?
    See this for example:


    This image has no alpha channel, so it's not good as cursor. But just for example.

    The top of the index finger is not at (0,0) but at (12,5) .... so in the point object you know to put new Point(12,5)
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Change The Mouse Cursor In Java

    The example is very clear, that's fine and I understand, but do you think I can / is correct that I want my object point in the red dot in the image?
    And if I can, how do I determine the point (for example, the (12,5) said it yourself)?

Similar Threads

  1. How do I change the color of my cursor?
    By Chokladmunken in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2014, 10:54 AM
  2. Display an image after mouse cursor point on a text area
    By samseah123 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 19th, 2013, 02:24 PM
  3. [SOLVED] trying to change image by mouse click
    By leonne in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 5th, 2013, 08:16 PM
  4. How to change color of JMenuItem when mouse is over it
    By Bagzli in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2012, 02:09 PM
  5. Replies: 7
    Last Post: June 26th, 2011, 11:16 AM