下面列出了java.awt.event.MouseEvent#getLocationOnScreen() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void mouseDragged(MouseEvent e) {
if (vehicleModel == null) {
return;
}
Point eOnScreen = e.getLocationOnScreen();
for (OpenTCSDrawingView drawView : getDrawingViews()) {
if (drawView.isShowing()) {
if (drawView.containsPointOnScreen(eOnScreen)) {
drawView.setCursor(dragCursor);
}
}
}
fComponentsTreeManager.setCursor(dragCursor);
setCursor(dragCursor);
}
public boolean isMovingTowards(@Nonnull MouseEvent me, @Nullable Rectangle rectangleOnScreen) {
Point currentLocation = me.getLocationOnScreen();
// finding previous location
Point previousLocation = null;
for (int i = 0; i < HISTORY_SIZE; i++) {
Point p = myHistory[(myCurrentIndex - i + HISTORY_SIZE) % HISTORY_SIZE];
if (p != null && p.distance(currentLocation) >= MOVEMENT_MARGIN_PX) {
previousLocation = p;
break;
}
}
// storing current location
myCurrentIndex = (myCurrentIndex + 1) % HISTORY_SIZE;
myHistory[myCurrentIndex] = currentLocation;
return ScreenUtil.isMovementTowards(previousLocation, currentLocation, rectangleOnScreen);
}
public Element getLineAtPoint(MouseEvent me) {
Point p = me.getLocationOnScreen();
Point pp = getLocationOnScreen();
p.translate(-pp.x, -pp.y);
int pos = viewToModel(p);
Element root = getDocument().getDefaultRootElement();
int e = root.getElementIndex(pos);
if (e == -1) {
return null;
}
return root.getElement(e);
}
/**
* In this default implementation, we don't modify the current image, we
* simply tell the glassPane where to redraw the image
*
* @param e the mouse event
*/
@Override
public void mouseDragged (MouseEvent e)
{
Point absPt = e.getLocationOnScreen();
glassPane.setPoint(new ScreenPoint(absPt.x, absPt.y));
}
public void mouseMoved(MouseEvent e) {
// Do not display popup when a modifier is pressed (can't read all keys)
// if (e.getModifiers() != 0) return;
currentScreenPoint = e.getLocationOnScreen();
updatePopups(e.getPoint(), false);
}
@Override
public void onMouseRelease(@Nonnull MouseEvent event) {
Point location = event.getLocationOnScreen();
for (ArrangementUiComponent component : myComponents) {
Rectangle bounds = component.getScreenBounds();
if (bounds != null && bounds.contains(location)) {
component.onMouseRelease(event);
return;
}
}
}
/**
* 处理窗体的拖拽事件
* @param e
*/
public void mouseDrag(MouseEvent e)
{
Point point = e.getLocationOnScreen();
int offsetX = point.x - lastPoint.x;
int offsetY = point.y - lastPoint.y;
Rectangle bounds = this.getBounds();
bounds.x += offsetX;
bounds.y += offsetY;
this.setBounds(bounds);
lastPoint = point;
}
@Override
public void mouseDragged(MouseEvent e)
{
int x = e.getLocationOnScreen().x - pressOffset.x;
int y = e.getLocationOnScreen().y - pressOffset.y;
chatWindow.setLocation(x, y);
}
/**
* In this default implementation, we don't modify the current image, we
* simply tell the glassPane where to redraw the image
*
* @param e the mouse event
*/
@Override
public void mouseDragged (MouseEvent e)
{
Point absPt = e.getLocationOnScreen();
glassPane.setPoint(new ScreenPoint(absPt.x, absPt.y));
}
public void mouseMoved(MouseEvent e) {
// Do not display popup when a modifier is pressed (can't read all keys)
// if (e.getModifiers() != 0) return;
currentScreenPoint = e.getLocationOnScreen();
updatePopups(e.getPoint(), false);
}
/**
* Move the component to its new location. The dragged Point must be in
* the destination coordinates.
*/
@Override
public void mouseDragged(MouseEvent e)
{
Point dragged = e.getLocationOnScreen();
int dragX = getDragDistance(dragged.x, pressed.x, snapSize.width);
int dragY = getDragDistance(dragged.y, pressed.y, snapSize.height);
int locationX = location.x + dragX;
int locationY = location.y + dragY;
// Mouse dragged events are not generated for every pixel the mouse
// is moved. Adjust the location to make sure we are still on a
// snap value.
while (locationX < edgeInsets.left)
locationX += snapSize.width;
while (locationY < edgeInsets.top)
locationY += snapSize.height;
Dimension d = getBoundingSize( destination );
while (locationX + destination.getSize().width + edgeInsets.right > d.width)
locationX -= snapSize.width;
while (locationY + destination.getSize().height + edgeInsets.bottom > d.height)
locationY -= snapSize.height;
// Adjustments are finished, move the component
destination.setLocation(locationX, locationY);
isMousePressed = false;
}
private void setupForDragging(MouseEvent e)
{
source.addMouseMotionListener( this );
potentialDrag = true;
// Determine the component that will ultimately be moved
if (destinationComponent != null)
{
destination = destinationComponent;
}
else if (destinationClass == null)
{
destination = source;
}
else // forward events to destination component
{
destination = SwingUtilities.getAncestorOfClass(destinationClass, source);
}
pressed = e.getLocationOnScreen();
location = destination.getLocation();
if (changeCursor)
{
originalCursor = source.getCursor();
source.setCursor( Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR) );
}
// Making sure autoscrolls is false will allow for smoother dragging of
// individual components
if (destination instanceof JComponent)
{
JComponent jc = (JComponent)destination;
autoscrolls = jc.getAutoscrolls();
jc.setAutoscrolls( false );
}
}
/**
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent ev) {
Window w = (Window) ev.getSource();
Point pt = ev.getPoint();
if (isMovingWindow) {
Point eventLocationOnScreen = ev.getLocationOnScreen();
w.setLocation(eventLocationOnScreen.x - dragOffsetX, eventLocationOnScreen.y - dragOffsetY);
} else if (dragCursor != 0) {
Rectangle r = w.getBounds();
Rectangle startBounds = new Rectangle(r);
Dimension min = w.getMinimumSize();
switch (dragCursor) {
case Cursor.E_RESIZE_CURSOR:
adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
break;
case Cursor.S_RESIZE_CURSOR:
adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.N_RESIZE_CURSOR:
adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
break;
case Cursor.W_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
break;
case Cursor.NE_RESIZE_CURSOR:
adjust(r, min, 0, pt.y - dragOffsetY, pt.x + (dragWidth - dragOffsetX) - r.width, -(pt.y - dragOffsetY));
break;
case Cursor.SE_RESIZE_CURSOR:
adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.NW_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, pt.y - dragOffsetY, -(pt.x - dragOffsetX), -(pt.y - dragOffsetY));
break;
case Cursor.SW_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), pt.y + (dragHeight - dragOffsetY) - r.height);
break;
default:
break;
}
if (!r.equals(startBounds)) {
w.setBounds(r);
// Defer repaint/validate on mouseReleased unless dynamic
// layout is active.
if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
w.validate();
getRootPane().repaint();
}
}
}
}
private boolean shortFromPressed(MouseEvent myMouseEvent) {
Point myPointC = myMouseEvent.getPoint();
Point myPointS = myMouseEvent.getLocationOnScreen();
return Math.hypot(myPointC.x - myPressedC.x, myPointC.y - myPressedC.y) <= tolerance && //
Math.hypot(myPointS.x - myPressedS.x, myPointS.y - myPressedS.y) <= tolerance;
}
/**
* Move the component to its new location. The dragged Point must be in
* the destination coordinates.
*/
@Override
public void mouseDragged(MouseEvent e)
{
Point dragged = e.getLocationOnScreen();
int dragX = getDragDistance(dragged.x, pressed.x, snapSize.width);
int dragY = getDragDistance(dragged.y, pressed.y, snapSize.height);
if (destination instanceof Visual)
((Visual) destination).setActualLocation(location.x + dragX, location.y + dragY);
else
destination.setLocation(location.x + dragX, location.y + dragY);
}
public void mouseDragged(MouseEvent e) {
Window w = (Window) e.getSource();
Point pt = e.getPoint();
if (isMovingWindow) {
Point eventLocationOnScreen = e.getLocationOnScreen();
w.setLocation(eventLocationOnScreen.x - dragOffsetX, eventLocationOnScreen.y - dragOffsetY);
} else if (dragCursor != 0) {
Rectangle r = w.getBounds();
Rectangle startBounds = new Rectangle(r);
Dimension min = w.getMinimumSize();
switch (dragCursor) {
case Cursor.E_RESIZE_CURSOR:
adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
break;
case Cursor.S_RESIZE_CURSOR:
adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.N_RESIZE_CURSOR:
adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
break;
case Cursor.W_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
break;
case Cursor.NE_RESIZE_CURSOR:
adjust(
r,
min,
0,
pt.y - dragOffsetY,
pt.x + (dragWidth - dragOffsetX) - r.width,
-(pt.y - dragOffsetY));
break;
case Cursor.SE_RESIZE_CURSOR:
adjust(
r,
min,
0,
0,
pt.x + (dragWidth - dragOffsetX) - r.width,
pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.NW_RESIZE_CURSOR:
adjust(
r,
min,
pt.x - dragOffsetX,
pt.y - dragOffsetY,
-(pt.x - dragOffsetX),
-(pt.y - dragOffsetY));
break;
case Cursor.SW_RESIZE_CURSOR:
adjust(
r,
min,
pt.x - dragOffsetX,
0,
-(pt.x - dragOffsetX),
pt.y + (dragHeight - dragOffsetY) - r.height);
break;
default:
break;
}
if (!r.equals(startBounds)) {
w.setBounds(r);
// Defer repaint/validate on mouseReleased unless dynamic
// layout is active.
if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
w.validate();
getRootPane().repaint();
}
}
}
}
/**
* 处理窗体的鼠标按下事件
* @param e
*/
public void mousePress(MouseEvent e)
{
lastPoint = e.getLocationOnScreen();
}
/**
* Move the component to its new location. The dragged Point must be in
* the destination coordinates.
*/
@Override
public void mouseDragged(MouseEvent e) {
Point dragged = e.getLocationOnScreen();
int dragX = getDragDistance(dragged.x, pressed.x, snapSize.width);
int dragY = getDragDistance(dragged.y, pressed.y, snapSize.height);
// if (dragX != 0 || dragY != 0) {
// if (destination instanceof MainView) {
// final MainView gui = (MainView) destination;
// if (gui.isMa) {
// return;
// }
// }
// }
int locationX = location.x + dragX;
int locationY = location.y + dragY;
// Mouse dragged events are not generated for every pixel the mouse
// is moved. Adjust the location to make sure we are still on a
// snap value.
getBoundingSize(destination);
//If the component is a MainView and it is maximized... lets restore
while (locationX < bounds.x) {
locationX += snapSize.width;
}
while (locationY < bounds.y) {
locationY += snapSize.height;
}
while (locationX > bounds.width) {
locationX -= snapSize.width;
}
while (locationY > bounds.height) {
locationY -= snapSize.height;
}
// Adjustments are finished, move the component
destination.setLocation(locationX, locationY);
}
private void setupForDragging(MouseEvent e)
{
source.addMouseMotionListener( this );
// Determine the component that will ultimately be moved
if (destinationComponent != null)
{
destination = destinationComponent;
}
else if (destinationClass == null)
{
destination = source;
}
else // forward events to destination component
{
destination = SwingUtilities.getAncestorOfClass(destinationClass, source);
}
pressed = e.getLocationOnScreen();
if (destination instanceof Visual){
location = ((Visual) destination).getActualLocation();
}else{
location = destination.getLocation();
}
if (changeCursor)
{
originalCursor = source.getCursor();
source.setCursor( Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR) );
}
// Making sure autoscrolls is false will allow for smoother dragging of
// individual components
if (destination instanceof JComponent)
{
JComponent jc = (JComponent)destination;
autoscrolls = jc.getAutoscrolls();
jc.setAutoscrolls( false );
}
}
private void setupForDragging(MouseEvent e)
{
source.addMouseMotionListener( this );
// Determine the component that will ultimately be moved
if (destinationComponent != null)
{
destination = destinationComponent;
}
else if (destinationClass == null)
{
destination = source;
}
else // forward events to destination component
{
destination = SwingUtilities.getAncestorOfClass(destinationClass, source);
}
pressed = e.getLocationOnScreen();
if (destination instanceof Visual){
location = ((Visual) destination).getActualLocation();
}else{
location = destination.getLocation();
}
if (changeCursor)
{
originalCursor = source.getCursor();
source.setCursor( Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR) );
}
// Making sure autoscrolls is false will allow for smoother dragging of
// individual components
if (destination instanceof JComponent)
{
JComponent jc = (JComponent)destination;
autoscrolls = jc.getAutoscrolls();
jc.setAutoscrolls( false );
}
}