Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unlimited mouse moves/drags



Here's my implementation using Robot. It was easier than I expected, and works great. To use it, just create an InfiniteDragListener, then listen to it as your source of mouse press, drag, and release events. It prevents the cursor from moving more than a few pixels from the click location, while generating events as if the cursor were moving freely over an unbounded screen. This uses Buoy (http://buoy.sourceforge.net), but it would be easy to create a Swing version.

Peter


import buoy.event.*; import buoy.widget.*;

import javax.swing.*;
import java.awt.*;

public class InfiniteDragListener extends EventSource
{
  private Widget source;
  private Robot robot;
  private Point startPoint, startScreenPoint, lastPoint;
  private int dx, dy;
  private boolean expectingReset;

  public InfiniteDragListener(Widget source)
  {
    this.source = source;
    source.addEventLink(MousePressedEvent.class, this, "mousePressed");
    source.addEventLink(MouseReleasedEvent.class, this, "mouseReleased");
    source.addEventLink(MouseDraggedEvent.class, this, "mouseDragged");
  }

private void mousePressed(MousePressedEvent ev)
{
startPoint = lastPoint = ev.getPoint();
startScreenPoint = new Point(startPoint);
SwingUtilities.convertPointToScreen(startScreenPoint, source.getComponent());
dx = 0;
dy = 0;
try
{
robot = new Robot(source.getComponent().getGraphicsConfiguration().getDevice());
}
catch (Exception ex)
{
robot = null;
}
dispatchEvent(ev);
}


private void mouseDragged(MouseDraggedEvent ev)
{
if (expectingReset && ev.getPoint().equals(startPoint))
{
dx += lastPoint.x-startPoint.x;
dy += lastPoint.y-startPoint.y;
expectingReset = false;
}
if ((Math.abs(ev.getX()-startPoint.x) > 5 || Math.abs(ev.getY()-startPoint.y) > 5) && robot != null)
{
expectingReset = true;
robot.mouseMove(startScreenPoint.x, startScreenPoint.y);
}
lastPoint = ev.getPoint();
dispatchEvent(new MouseDraggedEvent(source, ev.getWhen(), ev.getModifiersEx(), ev.getX()+dx, ev.getY()+dy));
}


private void mouseReleased(MouseReleasedEvent ev)
{
robot = null;
dispatchEvent(new MouseReleasedEvent(source, ev.getWhen(), ev.getModifiersEx(), ev.getX()+dx, ev.getY()+dy, ev.getClickCount(), ev.isPopupTrigger(), ev.getButton()));
}
}


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Unlimited mouse moves/drags (From: email@hidden)
 >Re: Unlimited mouse moves/drags (From: Rob Cope <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.