Custom Cursor in Java

I am working on an application in java , where I want to create a
cursor of CrossHair type in which the horizontal and the vertical
hairs/lines should run across the entire screen/panel , and not limited to
32x32 pixel size. With the method Toolkit.createCustomCursor(Image ,
Hotspot , String) one can create cursors with images of one's choice , BUT
ONLY OF 32X32 PIXEL SIZE. How can I overcome this restriction ? Is there
any other way to go about it ?
I shall be obliged if could please help me solve this problem.
Yours sincerely ,
Karunesh Tripathi

Your idea is worth appreciatoin and holds promise of providing a solution indirectly. I would elaborate my problem further. I want to show this big cursor over a JPanel , occupying a major part of the screen , which is contained in a JFrame alongwith the usual JMenuBar and JToolBar.
In fact , presently , I am already drawing a cross hair across the panel by g2D.drawLine(int , int , int , int) method in paintComponent(Graphics g) of the JPanel. But it does not move as fast as a cursor , especially when the painting load on the panel increases , because the painting of the cross hair at new location can take place only after the method repaint() as well as the event handler returns.
Therefore, the solution may have to be searched in the OS (Windows 98 , in this case) and the basic code of java.awt.Toolkit.createCustomCursor(Image , HotSpot , String).
Your idea may, however , improve the situation significantly , provided the applet is made to float over the JPanel with a transparent background and without borders etc. Can this be done ? I shall be obliged , if you could extend your expertise further.

Similar Messages

  • Custom Cursor and Drag Image in 1.4 DnD

    What is the proper way to provide a custom cursor and drag image in 1.4 DnD? Say we initiate the drag on a component that supports data transfer (such as a JTree).

    Well, this is strange. My app was locking up (100% CPU) when using DND from windows Explorer.
    I noticed that another part of the same appliction was working perfictly. The difference: JFrame .vs. JDialog. I switched the offending JDialog to a JFrame and all works perfictly now!
    java version "1.4.0"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
    Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
    I would like to switch back at some point, but my g.setXORMode() problem is a much bigger issue for us. (That is, once you enter the XOR mode, you can not go back!)

  • Adding a custom cursor to a panel

    HI, I am making a game where i want clicking a radio button to change the cursor image. I have figured out how to create a custom cursor, and even change the current cursor to it. The problem im having is i want to change it while the program is running. I can say somthing like frame.addCursor(cursor) but that sets it permenantly. What i would like is to add it to a panel during run-time. If anyone knows how i would go abou this i would be gratefull.
    Thanks

    Do u mean, upon selecting the radio button, the cursor must be changed to the customized one?
    If it is so,
    u add the snippet in the itemStateChanged() event....of the radiobutton
    i.e.,
    public void itemStateChanged(ItemEvent ie)
    if(ie.getStateChange() == ItemEvent.SELECTED)
    myPanel.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(CursorImg, new Point
    (0,0), "MyCursorImg"));
    CursorImg - the image to display when the cursor is actived
    new Point(0,0) - hotSpot
    MyCursorImg - a localized description of the cursor, for Java Accessibility use
    else
    myPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }

  • My own Cursor in Java

    Hi,
    I'm working on some kinda Drag N Drop program, and I want to to draw the object as cursor while dragging.
    I found out how to change the cursor to another default cursor, but of course I need to create a cursor myself.
    My question is: how can I use one of my own drawings as Cursor? The API doesn't give me anything about custom cursors.
    I'd really be helped out here, thanks in advance!!

    Found something with java.awt.Toolkit.createCustomCursor(..,..)
    However the problem is that this method wants an Image as argument, while I want to draw the cursur myself.
    I need to display the objects name in the cursor. Anybody knows how to do that?

  • How to make a custom cursor

    How do you make a custom Cursor with the Cursor class, so that I can use it in my program.
    And also, how do you make a LookAndFeel??

    To make a cursor use:
    createCustomCursor public Cursor createCustomCursor(Image cursor,
                                     Point hotSpot,
                                     String name)
                              throws IndexOutOfBoundsException, HeadlessException
    Creates a new custom cursor object. If the image to display is invalid, the cursor will be hidden
    (made completely transparent), and the hotspot will be set to (0, 0). Parameters:hotSpot - the X
    and Y of the large cursor's hot spot. The hotSpot values must be less than the Dimension returned
    by getBestCursorSize().name - a localized description of the cursor, for Java Accessibility use.
    Throws: IndexOutOfBoundsException - if the hotSpot values are outside the bounds of the cursor.

  • Custom Cursor bigger than 32x32

    Hi,
    I am looking for a way to make a custom cursor bigger than 32x32 pixels of getBestCursorSize() ?
    This is a urgent problem, pls help me.
    Hqtrung

    This is another way
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class GUIa extends JFrame 
    public GUIa()
         super("Grafritare");
         setBounds(0,0,700,400);
         addWindowListener(new WindowAdapter()
         {      public void windowClosing(WindowEvent ev)
                   dispose();
                   System.exit(0);
         EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
         eq.push(new MyEventQueue(this));
         setVisible(true);
    public class MyEventQueue extends EventQueue
         JWindow   w1;
         JFrame    f1;
    public MyEventQueue(JFrame f)
         super();
         f1 = f;
         w1 = new JWindow(f);
         w1.setBounds(-99,0,32,32);
         w1.getContentPane().setBackground(Color.red);     
         w1.setVisible(true);
         Toolkit      tk = Toolkit.getDefaultToolkit();
         Cursor myCursor = tk.createCustomCursor(tk.createImage(""),new Point(0,0),"");
         f1.setCursor(myCursor);
         w1.setCursor(myCursor);
    protected void dispatchEvent(AWTEvent event)
         if (event.getID() == MouseEvent.MOUSE_EXITED)
              MouseEvent m = (MouseEvent)event;
              if (m.getSource() == f1)
                   if (m.getX() > f1.getWidth()-7)  w1.setVisible(false);
                   if (m.getY() > f1.getHeight()-6) w1.setVisible(false);
                   if (m.getX() < 4)  w1.setVisible(false);
                   if (m.getY() < 23) w1.setVisible(false);
         if (event.getID() == MouseEvent.MOUSE_MOVED)
              MouseEvent m = (MouseEvent)event;
              if (m.getSource() == f1) w1.setLocation(f1.getX()+m.getX()+1,f1.getY()+m.getY()+1);     
              if (m.getSource() == w1) w1.setLocation(w1.getX()+m.getX(),w1.getY()+m.getY());     
              if (!f1.getBounds().contains(w1.getLocation()))
                   if (w1.isVisible()) w1.setVisible(false);
              else if (!w1.isVisible()) w1.setVisible(true);
         super.dispatchEvent(event);
    public static void main( String[] args)
         new GUIa();
    }Noah

  • Cross-platform custom cursor: please assist to solve!!!

    Hi all,
    I'm looking for solution for problem of cursor size when creating custom cursor. Please take a moment and run the code below on platform you are working on, after this please post the results here.
    Thanks in advance for help!
    public class CursorTest {
        public static void main(String[] args) {
            for ( int cnt = 0; cnt < 100; cnt++ )
                System.out.println("Size " + cnt + ": " +
                        java.awt.Toolkit.getDefaultToolkit().getBestCursorSize(cnt, cnt));
    }

    Sample output on Win2000:
    Size 0: java.awt.Dimension[width=32,height=32]
    Size 1: java.awt.Dimension[width=32,height=32]
    Size 2: java.awt.Dimension[width=32,height=32]
    Size 3: java.awt.Dimension[width=32,height=32]
    Size 4: java.awt.Dimension[width=32,height=32]
    Size 5: java.awt.Dimension[width=32,height=32]
    Size 6: java.awt.Dimension[width=32,height=32]
    Size 7: java.awt.Dimension[width=32,height=32]
    Size 8: java.awt.Dimension[width=32,height=32]
    Size 9: java.awt.Dimension[width=32,height=32]
    Size 10: java.awt.Dimension[width=32,height=32]
    Size 11: java.awt.Dimension[width=32,height=32]
    Size 12: java.awt.Dimension[width=32,height=32]
    Size 13: java.awt.Dimension[width=32,height=32]
    Size 14: java.awt.Dimension[width=32,height=32]
    Size 15: java.awt.Dimension[width=32,height=32]
    Size 16: java.awt.Dimension[width=32,height=32]
    Size 17: java.awt.Dimension[width=32,height=32]
    Size 18: java.awt.Dimension[width=32,height=32]
    Size 19: java.awt.Dimension[width=32,height=32]
    Size 20: java.awt.Dimension[width=32,height=32]
    Size 21: java.awt.Dimension[width=32,height=32]
    Size 22: java.awt.Dimension[width=32,height=32]
    Size 23: java.awt.Dimension[width=32,height=32]
    Size 24: java.awt.Dimension[width=32,height=32]
    Size 25: java.awt.Dimension[width=32,height=32]
    Size 26: java.awt.Dimension[width=32,height=32]
    Size 27: java.awt.Dimension[width=32,height=32]
    Size 28: java.awt.Dimension[width=32,height=32]
    Size 29: java.awt.Dimension[width=32,height=32]
    Size 30: java.awt.Dimension[width=32,height=32]
    Size 31: java.awt.Dimension[width=32,height=32]
    Size 32: java.awt.Dimension[width=32,height=32]
    Size 33: java.awt.Dimension[width=32,height=32]
    Size 34: java.awt.Dimension[width=32,height=32]
    Size 35: java.awt.Dimension[width=32,height=32]
    Size 36: java.awt.Dimension[width=32,height=32]
    Size 37: java.awt.Dimension[width=32,height=32]
    Size 38: java.awt.Dimension[width=32,height=32]
    Size 39: java.awt.Dimension[width=32,height=32]
    Size 40: java.awt.Dimension[width=32,height=32]
    Size 41: java.awt.Dimension[width=32,height=32]
    Size 42: java.awt.Dimension[width=32,height=32]
    Size 43: java.awt.Dimension[width=32,height=32]
    Size 44: java.awt.Dimension[width=32,height=32]
    Size 45: java.awt.Dimension[width=32,height=32]
    Size 46: java.awt.Dimension[width=32,height=32]
    Size 47: java.awt.Dimension[width=32,height=32]
    Size 48: java.awt.Dimension[width=32,height=32]
    Size 49: java.awt.Dimension[width=32,height=32]
    Size 50: java.awt.Dimension[width=32,height=32]
    Size 51: java.awt.Dimension[width=32,height=32]
    Size 52: java.awt.Dimension[width=32,height=32]
    Size 53: java.awt.Dimension[width=32,height=32]
    Size 54: java.awt.Dimension[width=32,height=32]
    Size 55: java.awt.Dimension[width=32,height=32]
    Size 56: java.awt.Dimension[width=32,height=32]
    Size 57: java.awt.Dimension[width=32,height=32]
    Size 58: java.awt.Dimension[width=32,height=32]
    Size 59: java.awt.Dimension[width=32,height=32]
    Size 60: java.awt.Dimension[width=32,height=32]
    Size 61: java.awt.Dimension[width=32,height=32]
    Size 62: java.awt.Dimension[width=32,height=32]
    Size 63: java.awt.Dimension[width=32,height=32]
    Size 64: java.awt.Dimension[width=32,height=32]
    Size 65: java.awt.Dimension[width=32,height=32]
    Size 66: java.awt.Dimension[width=32,height=32]
    Size 67: java.awt.Dimension[width=32,height=32]
    Size 68: java.awt.Dimension[width=32,height=32]
    Size 69: java.awt.Dimension[width=32,height=32]
    Size 70: java.awt.Dimension[width=32,height=32]
    Size 71: java.awt.Dimension[width=32,height=32]
    Size 72: java.awt.Dimension[width=32,height=32]
    Size 73: java.awt.Dimension[width=32,height=32]
    Size 74: java.awt.Dimension[width=32,height=32]
    Size 75: java.awt.Dimension[width=32,height=32]
    Size 76: java.awt.Dimension[width=32,height=32]
    Size 77: java.awt.Dimension[width=32,height=32]
    Size 78: java.awt.Dimension[width=32,height=32]
    Size 79: java.awt.Dimension[width=32,height=32]
    Size 80: java.awt.Dimension[width=32,height=32]
    Size 81: java.awt.Dimension[width=32,height=32]
    Size 82: java.awt.Dimension[width=32,height=32]
    Size 83: java.awt.Dimension[width=32,height=32]
    Size 84: java.awt.Dimension[width=32,height=32]
    Size 85: java.awt.Dimension[width=32,height=32]
    Size 86: java.awt.Dimension[width=32,height=32]
    Size 87: java.awt.Dimension[width=32,height=32]
    Size 88: java.awt.Dimension[width=32,height=32]
    Size 89: java.awt.Dimension[width=32,height=32]
    Size 90: java.awt.Dimension[width=32,height=32]
    Size 91: java.awt.Dimension[width=32,height=32]
    Size 92: java.awt.Dimension[width=32,height=32]
    Size 93: java.awt.Dimension[width=32,height=32]
    Size 94: java.awt.Dimension[width=32,height=32]
    Size 95: java.awt.Dimension[width=32,height=32]
    Size 96: java.awt.Dimension[width=32,height=32]
    Size 97: java.awt.Dimension[width=32,height=32]
    Size 98: java.awt.Dimension[width=32,height=32]
    Size 99: java.awt.Dimension[width=32,height=32]

  • Linux - The custom cursor is looking bad

    Creating a custom cursor using png 32x32 images. The images are displaying perfect as a cursor on Windows and Mac but, on Linux the image is bad. Below I am pasting code to test the things by using some png image.
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.net.URL;
    import javax.swing.*;
    public class UserCursor {
         private void initComponents() {
              JFrame jf = new JFrame("Cursor Test");
              jf.setLayout(new BorderLayout());
              final JButton jb = new JButton("Change Cursor");
              jf.getContentPane().add(jb,BorderLayout.NORTH);
              jf.setSize(200, 200);
              jf.setVisible(true);
              final Cursor cur = createCursor();
              jb.addMouseListener(new MouseAdapter(){
                   public void mouseEntered(MouseEvent arg0) {
                        jb.setCursor(cur);
         private Cursor createCursor() {
              Cursor userCursor=null;
              Toolkit toolkit = Toolkit.getDefaultToolkit();
              URL icon = getClass().getResource("images/someImage.png");
              Image image = new ImageIcon(icon).getImage();
              userCursor = toolkit.createCustomCursor(image, new Point(0, 0), "My Cursor");
              return userCursor;
         public static void main(String args[])
              UserCursor uc= new UserCursor();
              uc.initComponents();
    }

    Kanni wrote:
    Toolkit.getBestCursorSize(....)
    I tried using above suggestion to solve my cursor problem. It didn't work for me.
    ThanksSo how did you try this. Did you use the result of this function to choose a suitable cursor glyph? Did you use it to scale the original using an Affine Transformation?
    How can anyone give any further help if the only feedback you provide is "it didn't work" .

  • Change custom cursor size

    does anybody know how to change a custom cursor's size? we have created a custom cursor using the toolkit, but we can't seem to resize it so that the cursor is bigger.

    Read the API on java.awt.Toolkit
    createCustomCursor() creates a cursor of whatever size the system can handle. Generally, this is 32x32 for Windoze systems. toolkit.getBestCursorSize() will return a closest supported size for some (w,h) suggestion. You have to draw your image on a BufferedImage using the graphics object, then submit the BufferedImage object to toolkit.createCustomCursor()
    Jason

  • Custom cursor using image gets resized bigger

    I have created a custom cursor using the following code:
    ClassLoader cl = this.getClass().getClassLoader();     
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image im = tk.getImage( cl.getResource("images/wallcursor.gif"));
    Cursor Custom_Cursor = tk.createCustomCursor(im,new Point(9,9),"Drawing wall");
    draftGrid.setCursor( Custom_Cursor );The cursor is loaded and it works, however - the image I use for the cursor is resized so it gets too big. Any clues on why this happens?
    btw; draftGrid is one of my own classes that extends JComponent.
    - bjorn

    Thanks for your replies guys.
    I tried your code KPSeal, and it works in resizing the cursor image back to its original size. However, a gray square of size 32,32 (which is returned by getBestCursorSize()) is shown round the cursor.
    Here is the updated code (yours hade some minor bugs)
    Image im = tk.getImage( cl.getResource("images/wallcursor.gif"));
    try {
           tracker.addImage(im, 0 );
           tracker.waitForID(0);
    } catch( InterruptedException ie ) {
           ie.printStackTrace();
    int w = im.getWidth(this);
    int h = im.getHeight(this);
    int pw = Toolkit.getDefaultToolkit().getBestCursorSize(w, h).width;
    int ph = Toolkit.getDefaultToolkit().getBestCursorSize(w, h).height;
    System.out.println("w="+w+", h="+h+" - pw="+pw+", ph="+ph);
    Image cim = createImage(pw, ph);
    cim.getGraphics().drawImage(im,0,0, this);                     
    Cursor Custom_Cursor = tk.createCustomCursor(cim,new Point(0,0),"Drawing wall");
    draftGrid.setCursor( Custom_Cursor );The System.out.println statement prints: w=18, h=13 - pw=32, ph=32
    If I try to do the createImage with w and h instead of pw and ph the same gray square (32,32) is created, and in addition the cursor's size is too big which was the probem to begin with ...
    - bjorn

  • Custom cursor using startDrag

    Hello everyone,
    I have a movieclip following my mouse to act as a custom cursor.
    I am using startDrag on the movie clip and mouse.hide()
    What I am trying to resolve is the following:
    I have several hidden simple button objects in the background of my flash movie. ( I am using these to detect when the user has the mouse in certain areas of the flash movie)
    When i go to move over these simple buttons, my custom cursor movieclip stops following the mouse cursor.  Normally my cursor would also then change to the Hand cursor, but i disabled that.
    Can anyone think of any method or hack that would make this work for me?  I would like my custom cursor following the mouse pointer at all times even when hovering over a simple button.
    thanks in advance.

    The cursor changes to hand cursor yet?
    When you say "single button Several hidden objects", the number is?

  • How to load and increase size of a custom cursor on a certain monitor (1st or 2nd)?

    In my application I need to load a custom cursor, make it big and display it on the my second monitor in a dual monitor setup to let user click with an additional mouse. I found this thread able to solve the first 2 questions, however the cursor only changes within the VI panel. I had tried but couldn't get a handle to the second monitor, in face, not my primary one as well since I'm not familiar with winapi. I also want to restrict when the user can use the second mouse and only in the second monitor if possible. By the way I am using windows 7 64 bit with Labview 9 32 bit.
    Thanks,

    Ishi,
    More than likely, you are going to have to use Windows OS APIs for this. It sounds like you would like to integrate your program at the operating system level, and will have to make calls to Windows to accomplish this in the proper manner.  You can make calls to Windows in LabVIEW through several methods. Check out this example that sets the position of the cursor via a Call Library Function Node to the Windows SDK.  
    http://zone.ni.com/devzone/cda/epd/p/id/2315
    Cheers,
    Aaron
    National Instruments

  • Custom cursor is too slow in FULL_SCREEN_INTERACTIVE

    My custom cursor is fast in window mode (even maximized) but not in fullscreen where it is really slow.
    Is there a solution to this?

    Dear Rob,
    Thanks for your answer.
    I have put together a simple test case (see attached zip file) and the custom cursor is still "too slow to be usable" in fullscreen mode. Run it and see that the cursor is fast in window mode. Then hit ctrl+enter to go into fullscreen and see how slow the cursor becomes.
    Code:
    package
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    import flash.ui.Mouse;
    import flash.ui.Keyboard;
    import flash.display.StageDisplayState;
    import flash.display.MovieClip;
    public class Main extends MovieClip
    private var _mousePointer:MousePointer;
    public function Main()
    super();
    Mouse.hide();
    _mousePointer = new MousePointer();
    addChild(_mousePointer);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoved);
    private function goFullscreen(fullscreen:Boolean)
    if(fullscreen)
    stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
    else
    stage.displayState = StageDisplayState.NORMAL;
    private function onMouseMoved(evt:MouseEvent)
    _mousePointer.x = mouseX;
    _mousePointer.y = mouseY;
    evt.updateAfterEvent();
    private function onKeyboardDown(evt:KeyboardEvent)
    if(evt.controlKey && evt.keyCode == Keyboard.ENTER)
    goFullscreen(stage.displayState == StageDisplayState.NORMAL);

  • CS6 - Custom Cursor & Button's icon

    I am trying to display a custom cursor when mouse is moving on a button which has icon.
    But the button's icon is always in front of my cursor image.
    How to make my cursor image on top?
    Belows is my code. Please tell me what's wrong.
    Thank you!
    <mx:Application... creationComplete = "init()>
    private var MyCursor:Shape = new Shape();
    private function init():void{
        // draw custom cursor
        MyCursor.graphics.clear();
        MyCursor.graphics.lineStyle(2, 0xFF0000, 1,true);
        MyCursor.graphics.drawCircle(0,0,6);
        MyCursor.graphics.moveTo(-3,-3);
        MyCursor.graphics.lineTo(4,4);
        MyCursor.visible = false;
        // add to the layer
        CursorLayer.addChild(MyCursor);
    private function ShowCustomCursor(e: MouseEvent):void
        Mouse.hide();
        MyCursor.x = e.stageX;
        MyCursor.y = e.stageY;
        MyCursor.visible = true;
    private funtion HideCustomCursor(e:MouseEvent):void
        Mouse.show();
        MyCursor.visible = false;
    <mx:UIComponent id = "CursorLayer" x ="0' y="0" width ="1000" height = "1000" blendMode="layer">
    <mx:Button id="btnTest" x="50" y="5" width="28" height="20" buttonMode="true"
    icon="@Embed(source='assets/btnTest.png')"
    mouseFocusEnabled="true"
    mouseMove="ShowCustomCursor(event)"
    mouseOut="HideCustomCursor(event)"
    tabEnabled="false"
    useHandCursor="true"/>

    that's because your cursor is sporadically moving over your interactive objects interfering with their mouseevents.
    to remedy, disable the mouseEnabled property of your cursor.

  • Custom Cursors with AS3

    I used the code snippets to create a custom cursor and it works well.  What if I want that cursor to change when it's clicked?  Can I do that easy enough?  Thanks.

    you can use a stage mousedown listener to make it appear your cursor was clicked.

Maybe you are looking for