Java Code Examples for java.awt.event.MouseEvent#translatePoint()

The following examples show how to use java.awt.event.MouseEvent#translatePoint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JTreeTable.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Overridden to return false, and if the event is a mouse event
 * it is forwarded to the tree.<p>
 * The behavior for this is debatable, and should really be offered
 * as a property. By returning false, all keyboard actions are
 * implemented in terms of the table. By returning true, the
 * tree would get a chance to do something with the keyboard
 * events. For the most part this is ok. But for certain keys,
 * such as left/right, the tree will expand/collapse where as
 * the table focus should really move to a different column. Page
 * up/down should also be implemented in terms of the table.
 * By returning false this also has the added benefit that clicking
 * outside of the bounds of the tree node, but still in the tree
 * column will select the row, whereas if this returned true
 * that wouldn't be the case.
 * <p>By returning false we are also enforcing the policy that
 * the tree will never be editable (at least by a key sequence).
 * @param e
 * @return true if cell editable
 */
@Override
public boolean isCellEditable(EventObject e)
{
	if (e instanceof MouseEvent)
	{
		for (int counter = getColumnCount() - 1; counter >= 0; counter--)
		{
			if (getColumnClass(counter) == TreeTableNode.class)
			{
				MouseEvent me = (MouseEvent) e;
				int column = JTreeTable.this.columnAtPoint(me.getPoint());
				Rectangle cell = JTreeTable.this.getCellRect(0, column, true);
				MouseEvent newME = new MouseEvent(tree, me.getID(), me.getWhen(), me.getModifiers(), me.getX(),
					me.getY(), me.getClickCount(), me.isPopupTrigger());
				//we translate the event into the tree's coordinate system
				newME.translatePoint(-cell.x, 0);
				tree.dispatchEvent(newME);

				break;
			}
		}
	}

	return false;
}
 
Example 2
Source File: JPanelActiveBox.java    From jclic with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void processEvent(AWTEvent e) {
  if (catchMouseEvents && e instanceof MouseEvent) {
    MouseEvent me = (MouseEvent) e;
    if (e.getID() == MouseEvent.MOUSE_PRESSED && ab != null && ps != null
        && (parentActivityPanel == null || parentActivityPanel.isPlaying())) {
      ps.stopMedia(1);
      ab.playMedia(ps);
    }
    if (mouseListener != null && (ab == null || ab.getContent().mediaContent == null
        || ab.getContent().mediaContent.catchMouseEvents == false)) {
      Point bkPt = me.getPoint();
      Point pt = Utils.mapPointTo(this, bkPt, mouseListener);
      me.translatePoint(pt.x - bkPt.x, pt.y - bkPt.y);
      mouseListener.dispatchEvent(e);
    }
    me.consume();
    return;
  }
  super.processEvent(e);
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override protected TrackListener createTrackListener(JSlider slider) {
  return new TrackListener() {
    @Override public void mouseDragged(MouseEvent e) {
      // case HORIZONTAL:
      int halfThumbWidth = thumbRect.width / 2;
      int thumbLeft = e.getX() - offset;
      int maxPos = xPositionForValue(MAXI) - halfThumbWidth;
      int minPos = xPositionForValue(MINI) - halfThumbWidth;
      if (thumbLeft > maxPos) {
        e.translatePoint(maxPos + offset - e.getX(), 0);
      } else if (thumbLeft < minPos) {
        e.translatePoint(minPos + offset - e.getX(), 0);
      }
      super.mouseDragged(e);
    }
  };
}
 
Example 4
Source File: JTreeTable.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected MouseEvent convertEvent(MouseEvent e){
  int column = 0;
  int row = rowAtPoint(e.getPoint());

  //move the event from table to tree coordinates
  Rectangle tableCellRect = getCellRect(row, column, false);
  Rectangle treeCellRect = tree.getRowBounds(row);
  int dx = 0;
  if(tableCellRect != null) dx = -tableCellRect.x;
  int dy = 0;
  if(tableCellRect !=null && treeCellRect != null)
    dy = treeCellRect.y -tableCellRect.y;
  e.translatePoint(dx, dy);


  return new MouseEvent(
    tree, e.getID(), e.getWhen(), e.getModifiers(),
    e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()
  );
}
 
Example 5
Source File: JTreeTable.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Overridden to return false, and if the event is a mouse event
 * it is forwarded to the tree.<p>
 * The behavior for this is debatable, and should really be offered
 * as a property. By returning false, all keyboard actions are
 * implemented in terms of the table. By returning true, the
 * tree would get a chance to do something with the keyboard
 * events. For the most part this is ok. But for certain keys,
 * such as left/right, the tree will expand/collapse where as
 * the table focus should really move to a different column. Page
 * up/down should also be implemented in terms of the table.
 * By returning false this also has the added benefit that clicking
 * outside of the bounds of the tree node, but still in the tree
 * column will select the row, whereas if this returned true
 * that wouldn't be the case.
 * <p>By returning false we are also enforcing the policy that
 * the tree will never be editable (at least by a key sequence).
 * @param e
 * @return true if cell editable
 */
@Override
public boolean isCellEditable(EventObject e)
{
	if (e instanceof MouseEvent)
	{
		for (int counter = getColumnCount() - 1; counter >= 0; counter--)
		{
			if (getColumnClass(counter) == TreeTableNode.class)
			{
				MouseEvent me = (MouseEvent) e;
				int column = JTreeTable.this.columnAtPoint(me.getPoint());
				Rectangle cell = JTreeTable.this.getCellRect(0, column, true);
				MouseEvent newME = new MouseEvent(tree, me.getID(), me.getWhen(), me.getModifiers(), me.getX(),
					me.getY(), me.getClickCount(), me.isPopupTrigger());
				//we translate the event into the tree's coordinate system
				newME.translatePoint(-cell.x, 0);
				tree.dispatchEvent(newME);

				break;
			}
		}
	}

	return false;
}
 
Example 6
Source File: DragReorderHandler.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the layer button for the mouse event and also translate
 * the coordinates of the argument into the layer button space
 */
private static LayerButton getLayerButtonFromEvent(MouseEvent e) {
    LayerButton layerButton;
    Component c = e.getComponent();
    // the source of the event must be either the layer button
    // or the textfield inside it
    if (c instanceof LayerNameEditor) {
        LayerNameEditor nameEditor = (LayerNameEditor) c;
        layerButton = nameEditor.getLayerButton();
        // translate into the LayerButton coordinate system
        e.translatePoint(nameEditor.getX(), nameEditor.getY());
    } else if (c instanceof JLabel) {
        layerButton = (LayerButton) c.getParent();
        e.translatePoint(c.getX(), c.getY());
    } else {
        layerButton = (LayerButton) c;
    }
    return layerButton;
}
 
Example 7
Source File: AbstractAdrComponentFrame.java    From MercuryTrade with MIT License 5 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {
    if (SwingUtilities.isLeftMouseButton(e)) {
        e.translatePoint(AbstractAdrComponentFrame.this.getLocation().x - x, AbstractAdrComponentFrame.this.getLocation().y - y);
        Point point = e.getPoint();
        AbstractAdrComponentFrame.this.setLocation(point);
    }
}
 
Example 8
Source File: AdrCaptureOutComponentFrame.java    From MercuryTrade with MIT License 5 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {
    if (SwingUtilities.isLeftMouseButton(e)) {
        e.translatePoint(AdrCaptureOutComponentFrame.this.getLocation().x - x, AdrCaptureOutComponentFrame.this.getLocation().y - y);
        Point point = e.getPoint();
        AdrCaptureOutComponentFrame.this.setLocation(point);
    }
}
 
Example 9
Source File: AppearanceCanvas.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
private void repairEvent(MouseEvent e, double zoom) {
	if (zoom != 1.0) {
		int oldx = e.getX();
		int oldy = e.getY();
		int newx = (int) Math.round(e.getX() / zoom);
		int newy = (int) Math.round(e.getY() / zoom);
		e.translatePoint(newx - oldx, newy - oldy);
	}
}
 
Example 10
Source File: Canvas.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
private void zoomEvent(MouseEvent e, double zoom) {
	int oldx = e.getX();
	int oldy = e.getY();
	int newx = (int) Math.round(e.getX() / zoom);
	int newy = (int) Math.round(e.getY() / zoom);
	e.translatePoint(newx - oldx, newy - oldy);
}
 
Example 11
Source File: Canvas.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
public static void snapToGrid(MouseEvent e) {
	int old_x = e.getX();
	int old_y = e.getY();
	int new_x = snapXToGrid(old_x);
	int new_y = snapYToGrid(old_y);
	e.translatePoint(new_x - old_x, new_y - old_y);
}
 
Example 12
Source File: SwingNativeDispatcher.java    From CrossMobile with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
    public void sendTouchEvents(MouseEvent originalEvent, NativeTouch[] touches) {
        System.out.println("original event dispatching");
        if (originalEvent != null) {
            DesktopNativeWidget component = getWidgetWrapper().getNativeWidget();
            originalEvent.setSource(component);
            originalEvent.translatePoint((int) (touches[0].x) - originalEvent.getX(), (int) (touches[0].y) - originalEvent.getY());
//            component.dispatchEvent(originalEvent);
        }
    }
 
Example 13
Source File: MultiThumbSliderUI.java    From pumpernickel with MIT License 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
	if (slider.isEnabled() == false)
		return;

	e.translatePoint(dx, dy);

	mouseMoved(e);
	if (pressedState != null && pressedState.selectedThumb != -1) {
		slider.setValueIsAdjusting(true);

		State newState = new State(pressedState);
		float v;
		boolean outside;
		if (slider.getOrientation() == MultiThumbSlider.HORIZONTAL) {
			v = ((float) (e.getX() - trackRect.x))
					/ ((float) trackRect.width);
			if (slider.isInverted())
				v = 1 - v;
			outside = (e.getY() < trackRect.y - 10)
					|| (e.getY() > trackRect.y + trackRect.height + 10);

			// don't whack the thumb off the slider if you happen to be
			// *near* the edge:
			if (e.getX() > trackRect.x - 10
					&& e.getX() < trackRect.x + trackRect.width + 10) {
				if (v < 0)
					v = 0;
				if (v > 1)
					v = 1;
			}
		} else {
			v = ((float) (e.getY() - trackRect.y))
					/ ((float) trackRect.height);
			if (slider.isInverted() == false)
				v = 1 - v;
			outside = (e.getX() < trackRect.x - 10)
					|| (e.getX() > trackRect.x + trackRect.width + 10);

			if (e.getY() > trackRect.y - 10
					&& e.getY() < trackRect.y + trackRect.height + 10) {
				if (v < 0)
					v = 0;
				if (v > 1)
					v = 1;
			}
		}
		if (newState.positions.length <= slider.getMinimumThumbnailCount()) {
			outside = false; // I don't care if you are outside: no
								// removing!
		}
		newState.setPosition(newState.selectedThumb, v);

		// because we delegate mouseReleased() to this method:
		if (outside && slider.isThumbRemovalAllowed()) {
			newState.removeThumb(newState.selectedThumb);
		}
		if (validatePositions(newState)) {
			newState.install();
		}
		e.consume();
	}
}
 
Example 14
Source File: MultiThumbSliderUI.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {
    if (!slider.isEnabled()) {
        return;
    }

    e.translatePoint(dx, dy);

    mouseMoved(e);
    if (pressedState != null && pressedState.selectedThumb != -1) {
        slider.setValueIsAdjusting(true);

        State newState = new State(pressedState);
        float v;
        boolean outside;
        if (slider.getOrientation() == MultiThumbSlider.HORIZONTAL) {
            v = ((float) (e.getX() - trackRect.x)) / ((float) trackRect.width);
            if (slider.isInverted()) {
                v = 1 - v;
            }
            outside = (e.getY() < trackRect.y - 10) || (e.getY() > trackRect.y + trackRect.height + 10);

            //don't whack the thumb off the slider if you happen to be *near* the edge:
            if (e.getX() > trackRect.x - 10 && e.getX() < trackRect.x + trackRect.width + 10) {
                if (v < 0) {
                    v = 0;
                }
                if (v > 1) {
                    v = 1;
                }
            }
        } else {
            v = ((float) (e.getY() - trackRect.y)) / ((float) trackRect.height);
            if (!slider.isInverted()) {
                v = 1 - v;
            }
            outside = (e.getX() < trackRect.x - 10) || (e.getX() > trackRect.x + trackRect.width + 10);

            if (e.getY() > trackRect.y - 10 && e.getY() < trackRect.y + trackRect.height + 10) {
                if (v < 0) {
                    v = 0;
                }
                if (v > 1) {
                    v = 1;
                }
            }
        }
        if (newState.positions.length <= 2) {
            outside = false; //I don't care if you are outside: no removing!
        }
        newState.positions[newState.selectedThumb] = v;

        //because we delegate mouseReleased() to this method:
        if (outside) {
            newState.removeThumb(newState.selectedThumb);
        }
        if (validatePositions(newState)) {
            newState.install();
        }
        e.consume();
    }
}
 
Example 15
Source File: HelpIGFrame.java    From MercuryTrade with MIT License 4 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {
    e.translatePoint(HelpIGFrame.this.getLocation().x - x, HelpIGFrame.this.getLocation().y - y);
    HelpIGFrame.this.setLocation(new Point(e.getX(), e.getY()));
}
 
Example 16
Source File: AbstractComponentFrame.java    From MercuryTrade with MIT License 4 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {
    e.translatePoint(AbstractComponentFrame.this.getLocation().x - x, AbstractComponentFrame.this.getLocation().y - y);
    onFrameDragged(new Point(e.getX(), e.getY()));
}
 
Example 17
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
@Override protected TrackListener createTrackListener(JSlider slider) {
  return new TrackListener() {
    @Override public void mouseDragged(MouseEvent e) {
      if (!slider.getSnapToTicks() || slider.getMajorTickSpacing() == 0) {
        super.mouseDragged(e);
        return;
      }
      // case HORIZONTAL:
      int halfThumbWidth = thumbRect.width / 2;
      int trackLength = trackRect.width;
      int trackLeft = trackRect.x - halfThumbWidth;
      int trackRight = trackRect.x + trackRect.width - 1 + halfThumbWidth;
      int pos = e.getX();
      int snappedPos;
      if (pos <= trackLeft) {
        snappedPos = trackLeft;
      } else if (pos >= trackRight) {
        snappedPos = trackRight;
      } else {
        // int tickSpacing = slider.getMajorTickSpacing();
        // float actualPixelsForOneTick = trackLength * tickSpacing / (float) slider.getMaximum();

        // a problem if you choose to set a negative MINIMUM for the JSlider;
        // the calculated drag-positions are wrong.
        // Fixed by bobndrew:
        int possibleTickPositions = slider.getMaximum() - slider.getMinimum();
        boolean hasMinorTickSpacing = slider.getMinorTickSpacing() > 0;
        int tickSpacing = hasMinorTickSpacing ? slider.getMinorTickSpacing() : slider.getMajorTickSpacing();
        float actualPixelsForOneTick = trackLength * tickSpacing / (float) possibleTickPositions;
        pos -= trackLeft;
        // snappedPos = (int) (Math.round(pos / actualPixelsForOneTick) * actualPixelsForOneTick + .5) + trackLeft;
        snappedPos = Math.round(Math.round(pos / actualPixelsForOneTick) * actualPixelsForOneTick) + trackLeft;
        offset = 0;
        // System.out.println(snappedPos);
      }
      e.translatePoint(snappedPos - e.getX(), 0);
      super.mouseDragged(e);
      // MouseEvent me = new MouseEvent(
      //   e.getComponent(), e.getID(), e.getWhen(), e.getModifiers() | e.getModifiersEx(),
      //   snappedPos, e.getY(),
      //   e.getXOnScreen(), e.getYOnScreen(),
      //   e.getClickCount(), e.isPopupTrigger(), e.getButton());
      // e.consume();
      // super.mouseDragged(me);
    }
  };
}
 
Example 18
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
@Override protected TrackListener createTrackListener(JSlider slider) {
  return new TrackListener() {
    @Override public void mouseDragged(MouseEvent e) {
      if (!slider.getSnapToTicks() || slider.getMajorTickSpacing() == 0) {
        super.mouseDragged(e);
        return;
      }
      // case HORIZONTAL:
      int halfThumbWidth = thumbRect.width / 2;
      int trackLength = trackRect.width;
      int trackLeft = trackRect.x - halfThumbWidth;
      int trackRight = trackRect.x + trackRect.width - 1 + halfThumbWidth;
      int pos = e.getX();
      int snappedPos;
      if (pos <= trackLeft) {
        snappedPos = trackLeft;
      } else if (pos >= trackRight) {
        snappedPos = trackRight;
      } else {
        // int tickSpacing = slider.getMajorTickSpacing();
        // float actualPixelsForOneTick = trackLength * tickSpacing / (float) slider.getMaximum();

        // a problem if you choose to set a negative MINIMUM for the JSlider;
        // the calculated drag-positions are wrong.
        // Fixed by bobndrew:
        int possibleTickPositions = slider.getMaximum() - slider.getMinimum();
        boolean hasMinorTickSpacing = slider.getMinorTickSpacing() > 0;
        int tickSpacing = hasMinorTickSpacing ? slider.getMinorTickSpacing() : slider.getMajorTickSpacing();
        float actualPixelsForOneTick = trackLength * tickSpacing / (float) possibleTickPositions;
        pos -= trackLeft;
        // snappedPos = (int) (Math.round(pos / actualPixelsForOneTick) * actualPixelsForOneTick + .5) + trackLeft;
        snappedPos = Math.round(Math.round(pos / actualPixelsForOneTick) * actualPixelsForOneTick) + trackLeft;
        offset = 0;
        // System.out.println(snappedPos);
      }
      e.translatePoint(snappedPos - e.getX(), 0);
      super.mouseDragged(e);
      // MouseEvent me = new MouseEvent(
      //   e.getComponent(), e.getID(), e.getWhen(), e.getModifiers() | e.getModifiersEx(),
      //   snappedPos, e.getY(),
      //   e.getXOnScreen(), e.getYOnScreen(),
      //   e.getClickCount(), e.isPopupTrigger(), e.getButton());
      // super.mouseDragged(me);
    }
  };
}
 
Example 19
Source File: MultiThumbSliderUI.java    From PyramidShader with GNU General Public License v3.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
	if(slider.isEnabled()==false) return;

	e.translatePoint(dx, dy);

	mouseMoved(e);
	if(pressedState!=null && pressedState.selectedThumb!=-1) {
		slider.setValueIsAdjusting(true);

		State newState = new State(pressedState);
		float v;
		boolean outside;
		if(slider.getOrientation()==MultiThumbSlider.HORIZONTAL) {
			v = ((float)(e.getX()-trackRect.x))/((float)trackRect.width);
			if(slider.isInverted())
				v = 1-v;
			outside = (e.getY()<trackRect.y-10) || (e.getY()>trackRect.y+trackRect.height+10);

			//don't whack the thumb off the slider if you happen to be *near* the edge:
			if(e.getX()>trackRect.x-10 && e.getX()<trackRect.x+trackRect.width+10) {
				if(v<0) v = 0;
				if(v>1) v = 1;
			}
		} else {
			v = ((float)(e.getY()-trackRect.y))/((float)trackRect.height);
			if(slider.isInverted()==false)
				v = 1-v;
			outside = (e.getX()<trackRect.x-10) || (e.getX()>trackRect.x+trackRect.width+10);

			if(e.getY()>trackRect.y-10 && e.getY()<trackRect.y+trackRect.height+10) {
				if(v<0) v = 0;
				if(v>1) v = 1;
			}
		}
		if(newState.positions.length<=slider.getMinimumThumbnailCount()) {
			outside = false; //I don't care if you are outside: no removing!
		}
		newState.setPosition(newState.selectedThumb, v);

		//because we delegate mouseReleased() to this method:
		if(outside && slider.isThumbRemovalAllowed()) {
			newState.removeThumb(newState.selectedThumb);
		}
		if(validatePositions(newState)) {
			newState.install();
		}
		e.consume();
	}
}
 
Example 20
Source File: SplittedPanel.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** A method implemented from the MouseMotionListener interface to handle the splitter dragging */
public void mouseDragged(MouseEvent e) {
    if (continuousLayout == true) {
        Dimension d = getSize();
        Point splitterPos = splitter.getLocation();
        e.translatePoint(splitterPos.x, splitterPos.y);

        if (splitType == VERTICAL) {
            dragPos = e.getY();

            if (dragPos > d.height) {
                dragPos = d.height;
            }
        } else {
            dragPos = e.getX();

            if (dragPos > d.width) {
                dragPos = d.width;
            }
        }

        if (dragPos < 0) {
            dragPos = 0;
        }

        if (continuousLayout) {
            if (dragPos == -1) {
                return;
            }

            int newDragPos = dragPos;

            if (!absolute) {
                if (splitType == VERTICAL) {
                    newDragPos = (100 * dragPos) / d.height;
                } else {
                    newDragPos = (100 * dragPos) / d.width;
                }
            }

            setSplitPosition(newDragPos);
        }
    }
}