下面列出了java.awt.event.MouseWheelEvent#getPreciseWheelRotation() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected MouseWheelListener createMouseWheelListener() {
return new BasicScrollPaneUI.MouseWheelHandler() {
@Override
public void mouseWheelMoved( MouseWheelEvent e ) {
// Note: Getting UI value "ScrollPane.smoothScrolling" here to allow
// applications to turn smooth scrolling on or off at any time
// (e.g. in application options dialog).
if( UIManager.getBoolean( "ScrollPane.smoothScrolling" ) &&
scrollpane.isWheelScrollingEnabled() &&
e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL &&
e.getPreciseWheelRotation() != 0 &&
e.getPreciseWheelRotation() != e.getWheelRotation() )
{
mouseWheelMovedSmooth( e );
} else
super.mouseWheelMoved( e );
}
};
}
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
frame.add(panel);
MouseWheelEvent event = new MouseWheelEvent(panel,
0, 0, 0, 0, 0, 0, 0, 0, false, 0, 0,
2, // wheelRotation
PRECISE_WHEEL_ROTATION); // preciseWheelRotation
MouseWheelEvent convertedEvent = (MouseWheelEvent) SwingUtilities.
convertMouseEvent(event.getComponent(), event, null);
if (convertedEvent.getPreciseWheelRotation() != PRECISE_WHEEL_ROTATION) {
throw new RuntimeException("PreciseWheelRotation field is not copied!");
}
}
@Override public void mouseWheelMoved(MouseWheelEvent e) {
double dir = e.getPreciseWheelRotation();
int z = zoomRange.getValue();
zoomRange.setValue(z + EXTENT * (dir > 0 ? -1 : 1));
if (z != zoomRange.getValue()) {
Component c = e.getComponent();
Container p = SwingUtilities.getAncestorOfClass(JViewport.class, c);
if (p instanceof JViewport) {
JViewport vport = (JViewport) p;
Rectangle ovr = vport.getViewRect();
double s = dir > 0 ? 1d / ZOOM_MULTIPLICATION_FACTOR : ZOOM_MULTIPLICATION_FACTOR;
zoomTransform.scale(s, s);
// double s = 1d + zoomRange.getValue() * .1;
// zoomTransform.setToScale(s, s);
Rectangle nvr = AffineTransform.getScaleInstance(s, s).createTransformedShape(ovr).getBounds();
Point vp = nvr.getLocation();
vp.translate((nvr.width - ovr.width) / 2, (nvr.height - ovr.height) / 2);
vport.setViewPosition(vp);
c.revalidate();
c.repaint();
}
}
}
private void mouseWheelMoved(MouseWheelEvent e) {
/* The code in this method is taken directly from Pavel Fatin's original IntelliJ patch.
Some formatting changes have been applied. */
/* The shift modifier will be enabled for horizontal touchbar scroll events, even when the
actual shift key is not pressed, on both Windows and MacOS (though not on Java 8 on
Windows). */
JScrollBar scrollbar = e.isShiftDown()
? scrollpane.getHorizontalScrollBar() : scrollpane.getVerticalScrollBar();
int orientation = scrollbar.getOrientation();
JViewport viewport = scrollpane.getViewport();
if (viewport == null || !(viewport.getView() instanceof Scrollable)) {
return;
}
Scrollable view = (Scrollable) viewport.getView();
double rotation = e.getPreciseWheelRotation();
/* Use (0, 0) view position to obtain constant unit increment (which might otherwise be
variable on smaller-than-unit scrolling). */
Rectangle r = new Rectangle(new Point(0, 0), viewport.getViewSize());
int unitIncrement = view.getScrollableUnitIncrement(r, orientation, 1);
double delta = rotation * e.getScrollAmount() * unitIncrement;
boolean limitDelta = Math.abs(rotation) < 1.0d + EPSILON;
int blockIncrement = view.getScrollableBlockIncrement(r, orientation, 1);
double adjustedDelta = limitDelta
? Math.max(-(double) blockIncrement, Math.min(delta, (double) blockIncrement))
: delta;
int value = scrollbar.getValue();
int newValue = Math.max(scrollbar.getMinimum(),
Math.min((int) Math.round(value + adjustedDelta), scrollbar.getMaximum()));
if (newValue != value) {
scrollbar.setValue(newValue);
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
JScrollBar scrollBar = mainScrollPane.getVerticalScrollBar();
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
int totalScrollAmount = (int) (e.getPreciseWheelRotation() * e.getScrollAmount() * scrollBar.getUnitIncrement());
scrollBar.setValue(scrollBar.getValue() + totalScrollAmount);
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {// zoom mouse wheel
if (e.getPreciseWheelRotation() < 0) {
proj.getFrame().getZoomControl().spinnerModel
.setValue(proj.getFrame().getZoomControl().spinnerModel.getNextValue());
} else if (e.getPreciseWheelRotation() > 0) {
proj.getFrame().getZoomControl().spinnerModel
.setValue(proj.getFrame().getZoomControl().spinnerModel.getPreviousValue());
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent arg0) {// zoom mouse wheel
if (arg0.getPreciseWheelRotation() < 0) {
proj.getFrame().getZoomControl().spinnerModel
.setValue(proj.getFrame().getZoomControl().spinnerModel.getNextValue());
} else if (arg0.getPreciseWheelRotation() > 0) {
proj.getFrame().getZoomControl().spinnerModel
.setValue(proj.getFrame().getZoomControl().spinnerModel.getPreviousValue());
}
}
@Override
public void mouseWheelMoved(final MouseWheelEvent e) {
if (e.isControlDown()) {
final float newzoom;
if (e.getPreciseWheelRotation() > 0) {
newzoom = Math.max(1.0f, this.zoom - 0.2f);
} else {
newzoom = Math.min(5.0f, this.zoom + 0.2f);
}
if (newzoom != this.zoom) {
updateZoom(newzoom);
}
}
}
@Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
MouseWheelEvent ee = new MouseWheelEvent(
(Component) e.getSource(), e.getID(), e.getWhen(),
e.getModifiers(), e.getX(), e.getY(), e.getXOnScreen(),
e.getYOnScreen(), e.getClickCount(),
e.isPopupTrigger(), e.getScrollType(), e.getScrollAmount(),
e.getWheelRotation(), e.getPreciseWheelRotation());
super.processMouseWheelEvent(ee);
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
double value = 0.05f * e.getPreciseWheelRotation();
int xleft = x0 - width / 3;
int xrigth = x0 + width / 3;
if(value > 0 && (xrigth + xScale) - (xleft - xScale) <= 50){
return;
}
else{
xScale -= value * 100;
}
repaint();
}
private void mouseWheelMovedSmooth( MouseWheelEvent e ) {
// return if there is no viewport
JViewport viewport = scrollpane.getViewport();
if( viewport == null )
return;
// find scrollbar to scroll
JScrollBar scrollbar = scrollpane.getVerticalScrollBar();
if( scrollbar == null || !scrollbar.isVisible() || e.isShiftDown() ) {
scrollbar = scrollpane.getHorizontalScrollBar();
if( scrollbar == null || !scrollbar.isVisible() )
return;
}
// consume event
e.consume();
// get precise wheel rotation
double rotation = e.getPreciseWheelRotation();
// get unit and block increment
int unitIncrement;
int blockIncrement;
int orientation = scrollbar.getOrientation();
Component view = viewport.getView();
if( view instanceof Scrollable ) {
Scrollable scrollable = (Scrollable) view;
// Use (0, 0) view position to obtain constant unit increment of first item
// (which might otherwise be variable on smaller-than-unit scrolling).
Rectangle visibleRect = new Rectangle( viewport.getViewSize() );
unitIncrement = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
blockIncrement = scrollable.getScrollableBlockIncrement( visibleRect, orientation, 1 );
if( unitIncrement > 0 ) {
// For the case that the first item (e.g. in a list) is larger
// than the other items, get the unit increment of the second item
// and use the smaller one.
if( orientation == SwingConstants.VERTICAL ) {
visibleRect.y += unitIncrement;
visibleRect.height -= unitIncrement;
} else {
visibleRect.x += unitIncrement;
visibleRect.width -= unitIncrement;
}
int unitIncrement2 = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
if( unitIncrement2 > 0 )
unitIncrement = Math.min( unitIncrement, unitIncrement2 );
}
} else {
int direction = rotation < 0 ? -1 : 1;
unitIncrement = scrollbar.getUnitIncrement( direction );
blockIncrement = scrollbar.getBlockIncrement( direction );
}
// limit scroll amount (number of units to scroll) for small viewports
// (e.g. vertical scrolling in file chooser)
int scrollAmount = e.getScrollAmount();
int viewportWH = (orientation == SwingConstants.VERTICAL)
? viewport.getHeight()
: viewport.getWidth();
if( unitIncrement * scrollAmount > viewportWH )
scrollAmount = Math.max( viewportWH / unitIncrement, 1 );
// compute relative delta
double delta = rotation * scrollAmount * unitIncrement;
boolean adjustDelta = Math.abs( rotation ) < (1.0 + EPSILON);
double adjustedDelta = adjustDelta
? Math.max( -blockIncrement, Math.min( delta, blockIncrement ) )
: delta;
// compute new value
int value = scrollbar.getValue();
double minDelta = scrollbar.getMinimum() - value;
double maxDelta = scrollbar.getMaximum() - scrollbar.getModel().getExtent() - value;
double boundedDelta = Math.max( minDelta, Math.min( adjustedDelta, maxDelta ) );
int newValue = value + (int) Math.round( boundedDelta );
// set new value
if( newValue != value )
scrollbar.setValue( newValue );
/*debug
System.out.println( String.format( "%4d %9f / %4d %4d / %12f %5s %12f / %4d %4d %4d / %12f %12f %12f / %4d",
e.getWheelRotation(),
e.getPreciseWheelRotation(),
unitIncrement,
blockIncrement,
delta,
adjustDelta,
adjustedDelta,
value,
scrollbar.getMinimum(),
scrollbar.getMaximum(),
minDelta,
maxDelta,
boundedDelta,
newValue ) );
*/
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (mouseInHouse(e)) {
setZoomMaxX(e.getX());
setZoomMaxY(e.getY());
// https://java.com/en/download/faq/release_changes.xml
double notches = e.getPreciseWheelRotation();
if (true) {
if (notches < 0) {// zoom in
setMinX(getMinX() + getRangeX_Display() / ZOOM_FACTOR);
setMaxX(getMaxX() - getRangeX_Display() / ZOOM_FACTOR);
setMinY(getMinY() + getRangeY_Display() / ZOOM_FACTOR);
setMaxY(getMaxY() - getRangeY_Display() / ZOOM_FACTOR);
zoomCount++;
} else {// zoom out
setMinX(getMinX() - getRangeX_Display() / ZOOM_FACTOR);
setMaxX(getMaxX() + getRangeX_Display() / ZOOM_FACTOR);
setMinX(Math.max(getMinX(), 0.0));
setMinY(getMinY() - getRangeY_Display() / ZOOM_FACTOR);
setMaxY(getMaxY() + getRangeY_Display() / ZOOM_FACTOR);
setMinY(Math.max(getMinY(), 0.0));
zoomCount--;
// // stop zoom out
// if (getMinX() * getMinY() > 0.0) {
// setMaxX(getMaxX() + getRangeX_Display() / ZOOM_FACTOR);
// setMaxY(getMaxY() + getRangeY_Display() / ZOOM_FACTOR);
//
// } else {
// setMinX(0.0);
// setMaxX(getMaxX_Display());
// setMinY(0.0);
// setMaxY(getMaxY_Display());
// zoomCount = 0;
// }
}
if (getMinX() <= 0.0) {
setMinX(0.0);
setDisplayOffsetX (0.0);
}
if (getMinY() <= 0.0) {
setMinY(0.0);
setDisplayOffsetY (0.0);
}
// setZoomMinX(getZoomMaxX());
// setZoomMinY(getZoomMaxY());
// ticsYaxis = TicGeneratorForAxes.generateTics(getMinY_Display(), getMaxY_Display(), 10);
// ticsXaxis = TicGeneratorForAxes.generateTics(getMinX_Display(), getMaxX_Display(), 10);
repaint();
}
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (mouseInHouse(e)) {
zoomMaxX = e.getX();
zoomMaxY = e.getY();
// https://java.com/en/download/faq/release_changes.xml
double notches = e.getPreciseWheelRotation();
if (true) {//(notches == Math.rint(notches)) {
if (notches < 0) {// zoom in
if (zoomCount >= 0) {
minX += getRangeX_Display() / ZOOM_FACTOR;
}
maxX -= getRangeX_Display() / ZOOM_FACTOR;
if (zoomCount >= 0) {
minY += getRangeY_Display() / ZOOM_FACTOR;
}
maxY -= getRangeY_Display() / ZOOM_FACTOR;
zoomCount++;
} else {// zoom out
minX -= getRangeX_Display() / ZOOM_FACTOR;
minX = Math.max(minX, 0.0);
minY -= getRangeY_Display() / ZOOM_FACTOR;
minY = Math.max(minY, 0.0);
zoomCount--;
// stop zoom out
if (minX * minY > 0.0) {
maxX += getRangeX_Display() / ZOOM_FACTOR;
maxY += getRangeY_Display() / ZOOM_FACTOR;
zoomCount = 0;
} else {
minX = 0.0;
minY = 0.0;
maxX += getRangeX_Display() / ZOOM_FACTOR;
maxY += getRangeY_Display() / ZOOM_FACTOR;
}
}
if (minX <= 0.0) {
minX = 0.0;
displayOffsetX = 0.0;
}
if (minY <= 0.0) {
minY = 0.0;
displayOffsetY = 0.0;
}
zoomMinX = zoomMaxX;
zoomMinY = zoomMaxY;
ticsYaxis = TicGeneratorForAxes.generateTics(getMinY_Display(), getMaxY_Display(), 10);
ticsXaxis = TicGeneratorForAxes.generateTics(getMinX_Display(), getMaxX_Display(), 10);
repaint();
}
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (mouseInHouse(e)) {
zoomMaxX = e.getX();
zoomMaxY = e.getY();
// https://java.com/en/download/faq/release_changes.xml
double notches = e.getPreciseWheelRotation();
if (true) {//(notches == Math.rint(notches)) {
if (notches < 0) {// zoom in
minX += getRangeX_Display() / ZOOM_FACTOR;
maxX -= getRangeX_Display() / ZOOM_FACTOR;
minY += getRangeY_Display() / ZOOM_FACTOR;
maxY -= getRangeY_Display() / ZOOM_FACTOR;
zoomCount++;
} else {// zoom out
minX -= getRangeX_Display() / ZOOM_FACTOR;
minX = Math.max(minX, 0.0);
minY -= getRangeY_Display() / ZOOM_FACTOR;
minY = Math.max(minY, 0.0);
zoomCount--;
// stop zoom out
if (minX * minY > 0.0) {
maxX += getRangeX_Display() / ZOOM_FACTOR;
maxY += getRangeY_Display() / ZOOM_FACTOR;
} else {
minX = 0.0;
maxX = xAxisMax;
minY = 0.0;
maxY = yAxisMax;
zoomCount = 0;
}
}
if (minX <= 0.0) {
minX = 0.0;
displayOffsetX = 0.0;
}
if (minY <= 0.0) {
minY = 0.0;
displayOffsetY = 0.0;
}
zoomMinX = zoomMaxX;
zoomMinY = zoomMaxY;
buildIsochronsAndInitDelta234UContours();
generateCustomTics();
repaint();
}
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
double notches = e.getPreciseWheelRotation();
double zoomFactor = Math.abs(notches) * 2;
if (notches > 0 && zoomFactor > 0) zoomFactor = 1.0 / zoomFactor;
panel().zoom(toModel(e.getPoint()), zoomFactor);
}