下面列出了java.awt.event.MouseEvent#translatePoint() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
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()
);
}
/**
* 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;
}
/**
* 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;
}
@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);
}
};
}
/**
* 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;
}
@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);
}
}
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);
}
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);
}
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);
}
}
@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);
}
}
@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);
}
}
@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();
}
}
/** 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);
}
}
}
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();
}
}
@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()));
}
@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()));
}
@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);
}
};
}
@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);
}
};
}
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();
}
}