0

I have to update a legacy java applet by adding a custom cursor which appears when moving the mouse over certain regions of a Panel. I've done this and it works, except that if the cursor position moves over the left or bottom of my Panel (displaying a resize arrow cursor) and then moves into the custom cursor region, the custom cursor displays if the cursor is moved fast, but the default cursor is displayed if moving slowly.

If I use the predefined cursors the problem goes away, but unfortunately I need a move cursor, and this is running on a Mac, where the usual move cursor is not available.

Any ideas? Example code below, though you'll need to grab your own cursor png file - something like this one would do https://i.stack.imgur.com/gJmeJ.png (Thanks @Andrew Thompson)

Runner.java:

import java.applet.Applet;
import java.awt.Frame;
import java.awt.Panel;

public class Runner extends Applet
{

    public void init()
    {
        //Create and set up the window.
        Panel testpanel = new TestPanel();

        Frame frame = new Frame();
        frame.add(testpanel);
        frame.pack();
        frame.setVisible(true);
    }
}

TestPanel.java:

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class TestPanel extends Panel
    implements MouseMotionListener, MouseListener 
{
  private Cursor dragCursor;

  public TestPanel()
  {
    setPreferredSize(new Dimension(500, 200));

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("dragcursor.png");
    dragCursor = toolkit.createCustomCursor(image ,new Point(16, 16), "img");

    addMouseMotionListener(this);

    addMouseListener(this);

  }

  @Override
  public void mouseEntered(MouseEvent evt)
  {
  }

  @Override
  public void mouseExited(MouseEvent evt)
  {
  }

  @Override
  public void mouseClicked(MouseEvent evt)
  {
    if (evt.getX() > 0 && evt.getX() < 300 && evt.getY() > 0
        && evt.getY() < 300)
    {
      // display drag cursor at mouse position
      this.getParent().setCursor(dragCursor);
    }
    else
    {
      // reset cursor
      this.getParent()
          .setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }

  @Override
  public void mouseMoved(MouseEvent evt)
  {
    if (evt.getX() > 0 && evt.getX() < 300 && evt.getY() > 0
        && evt.getY() < 300)
    {
      // display drag cursor at mouse position
      this.getParent().setCursor(dragCursor);
    }
    else
    {
      // reset cursor
      this.getParent()
          .setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }

  @Override
  public void mousePressed(MouseEvent evt)
  {
  }

  @Override
  public void mouseReleased(MouseEvent evt)
  {
  }

  @Override
  public void mouseDragged(MouseEvent evt)
  {
  }

  @Override
  public void paint(Graphics g)
  {
    g.setColor(Color.red);
    g.drawRect(0, 0, 300, 300);
  }
}
kmt
  • 773
  • 12
  • 30
  • 1
    1) "you'll need to grab your own cursor png file"* One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 2) *"I have to update a legacy java applet"* See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). It'll quickly become lest costly to redevelop this using another technology than trying to make the applet workable. 3) Why does the applet immediately create .. – Andrew Thompson Jan 12 '18 at 03:54
  • .. a free floating `Frame`? Before Java Web Start was (also) deprecated, I'd have recommended dumping the applet completely and launching the frame from a link using JWS. – Andrew Thompson Jan 12 '18 at 03:56
  • Thanks for this: (1) have added link to cursor (2) not my decision, but I think it's in the works (3) I was trying to make an MWE and my applet knowledge is limited; the real thing does a load of other stuff – kmt Jan 12 '18 at 09:13

0 Answers0