下面列出了怎么用java.awt.event.MouseWheelEvent的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Handles a mouse wheel event from the underlying chart panel.
*
* @param e the event.
*/
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
JFreeChart chart = this.chartPanel.getChart();
if (chart == null) {
return;
}
Plot plot = chart.getPlot();
if (plot instanceof Zoomable) {
Zoomable zoomable = (Zoomable) plot;
handleZoomable(zoomable, e);
}
else if (plot instanceof PiePlot) {
PiePlot pp = (PiePlot) plot;
pp.handleMouseWheelRotation(e.getWheelRotation());
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
JScrollPane parent = getParentScrollPane();
if (parent != null) {
/*
* Only dispatch if we have reached top/bottom on previous scroll
*/
if (e.getWheelRotation() < 0) {
if (bar.getValue() == 0 && previousValue == 0) {
parent.dispatchEvent(cloneEvent(e));
}
} else {
if (bar.getValue() == getMax() && previousValue == getMax()) {
parent.dispatchEvent(cloneEvent(e));
}
}
previousValue = bar.getValue();
}
/*
* If parent scrollpane doesn't exist, remove this as a listener. We have to
* defer this till now (vs doing it in constructor) because in the constructor
* this item has no parent yet.
*/
else {
PDControlScrollPane.this.removeMouseWheelListener(this);
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.isControlDown()) {
int zoomLevel = zoomComboBox.getSelectedIndex();
int notches = e.getWheelRotation();
if (zoomLevel != -1) {
if (notches < 0) {
if (zoomLevel > 0) {
zoomLevel--;
zoomComboBox.setSelectedIndex(zoomLevel);
}
}
else {
if (zoomLevel < zoomComboBox.getItemCount() - 1) {
zoomLevel++;
zoomComboBox.setSelectedIndex(zoomLevel);
}
}
}
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int notches = e.getWheelRotation();
Point mouse = e.getPoint(); // Don't use getMousePosition() because when computer is swapping/grinding, mouse may have moved out of window before execution reaches here.
for (Widget widget : widgets) {
if ((widget.isVisible()) &&
(mouse.x > widget.getX()) &&
(mouse.y > widget.getY()) &&
(mouse.x < widget.getX() + widget.getWidth()) &&
(mouse.y < widget.getY() + widget.getHeight())) {
if (widget.onMouseWheelMoved(mouse.x - widget.getX(), mouse.y - widget.getY(), notches))
return;
}
}
adjustZoom(getMousePosition(), notches);
}
public JScrollPopupMenu(String label) {
super(label);
setLayout(new ScrollPopupMenuLayout());
super.add(getScrollBar());
addMouseWheelListener(new MouseWheelListener() {
@Override public void mouseWheelMoved(MouseWheelEvent event) {
JScrollBar scrollBar = getScrollBar();
int amount = (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
? event.getUnitsToScroll() * scrollBar.getUnitIncrement()
: (event.getWheelRotation() < 0 ? -1 : 1) * scrollBar.getBlockIncrement();
scrollBar.setValue(scrollBar.getValue() + amount);
event.consume();
}
});
}
public CgSpinner(int start, int min, int max, int step) {
super();
this.min = min;
this.max = max;
this.step = step;
model = new SpinnerNumberModel(start, // initial value
min, // min
max, // max
step); // step
setModel(model);
addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent mwe) {
MouseWheelAction(mwe.getWheelRotation());
}
});
// Center
JSpinner.DefaultEditor spinnerEditor = (JSpinner.DefaultEditor) this.getEditor();
spinnerEditor.getTextField().setHorizontalAlignment(JTextField.CENTER);
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e)
{
int sign = e.getWheelRotation();
if(sign < 0)
{
this.cam_zoom_increase();
return;
}
if(sign > 0)
{
this.cam_zoom_decrease();
return;
}
}
/**
* Creates a new {@link MapCanvas}.
*
* @param repProc replay processor
* @param zoomComboBox combo box which tells how to zoom the map image
*/
public MapCanvas( final RepProcessor repProc, final XComboBox< Zoom > zoomComboBox ) {
this.repProc = repProc;
this.zoomComboBox = zoomComboBox;
ricon = MapImageCache.getMapImage( repProc );
GuiUtils.makeComponentDragScrollable( this );
// Zoom in and out with CTRL+wheel scroll:
addMouseWheelListener( new MouseWheelListener() {
@Override
public void mouseWheelMoved( final MouseWheelEvent event ) {
if ( event.isControlDown() ) {
final int newZoomIdx = zoomComboBox.getSelectedIndex() - event.getWheelRotation();
zoomComboBox.setSelectedIndex( Math.max( 0, Math.min( zoomComboBox.getItemCount() - 1, newZoomIdx ) ) );
// An event will be fired which will cause reconfigureZoom() to be called...
}
}
} );
}
public static void main(final String[] args) {
final int mask = InputEvent.META_DOWN_MASK | InputEvent.CTRL_MASK;
Frame f = new Frame();
MouseEvent mwe = new MouseWheelEvent(f, 1, 1, mask, 1, 1, 1, 1, 1, true,
1, 1, 1);
MouseEvent mdme = new MenuDragMouseEvent(f, 1, 1, mask, 1, 1, 1, 1, 1,
true, null, null);
MouseEvent me = new MouseEvent(f, 1, 1, mask, 1, 1, 1, 1, 1, true,
MouseEvent.NOBUTTON);
test(f, mwe);
test(f, mdme);
test(f, me);
if (FAILED) {
throw new RuntimeException("Wrong mouse event");
}
}
public static void main(final String[] args) {
final int mask = InputEvent.META_DOWN_MASK | InputEvent.CTRL_MASK;
Frame f = new Frame();
MouseEvent mwe = new MouseWheelEvent(f, 1, 1, mask, 1, 1, 1, 1, 1, true,
1, 1, 1);
MouseEvent mdme = new MenuDragMouseEvent(f, 1, 1, mask, 1, 1, 1, 1, 1,
true, null, null);
MouseEvent me = new MouseEvent(f, 1, 1, mask, 1, 1, 1, 1, 1, true,
MouseEvent.NOBUTTON);
test(f, mwe);
test(f, mdme);
test(f, me);
if (FAILED) {
throw new RuntimeException("Wrong mouse event");
}
}
public CFGComponent(mxGraph g) {
super(g);
this.getViewport().setBackground(Color.WHITE);
this.setEnabled(false);
this.setBorder(new EmptyBorder(0, 0, 0, 0));
this.setZoomFactor(1.1);
this.getGraphControl().addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.isControlDown()) {
if (e.getWheelRotation() < 0) {
zoomIn();
} else {
zoomOut();
}
repaint();
revalidate();
} else if (scp != null) {
// do we need this on linux too?
scp.getVerticalScrollBar().setValue(scp.getVerticalScrollBar().getValue()
+ e.getUnitsToScroll() * scp.getVerticalScrollBar().getUnitIncrement());
}
}
});
}
/**
* Handles a mouse wheel event from the underlying chart panel.
*
* @param e the event.
*/
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
JFreeChart chart = this.chartPanel.getChart();
if (chart == null) {
return;
}
Plot plot = chart.getPlot();
if (plot instanceof Zoomable) {
Zoomable zoomable = (Zoomable) plot;
handleZoomable(zoomable, e);
}
else if (plot instanceof PiePlot) {
PiePlot pp = (PiePlot) plot;
pp.handleMouseWheelRotation(e.getWheelRotation());
}
}
public static void main(final String[] args) {
final int mask = InputEvent.META_DOWN_MASK | InputEvent.CTRL_MASK;
Frame f = new Frame();
MouseEvent mwe = new MouseWheelEvent(f, 1, 1, mask, 1, 1, 1, 1, 1, true,
1, 1, 1);
MouseEvent mdme = new MenuDragMouseEvent(f, 1, 1, mask, 1, 1, 1, 1, 1,
true, null, null);
MouseEvent me = new MouseEvent(f, 1, 1, mask, 1, 1, 1, 1, 1, true,
MouseEvent.NOBUTTON);
test(f, mwe);
test(f, mdme);
test(f, me);
if (FAILED) {
throw new RuntimeException("Wrong mouse event");
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static int getIncrementFromAdjustable(Adjustable adj,
MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
}
int increment = 0;
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
increment = e.getUnitsToScroll() * adj.getUnitIncrement();
}
else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
increment = adj.getBlockIncrement() * e.getWheelRotation();
}
return increment;
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (gestureHandlers == null || gestureHandlers.isEmpty() || !listensFor(Event.MOUSE_WHEEL))
return;
if (e.getComponent() instanceof ChartPanel) {
ChartPanel chartPanel = (ChartPanel) e.getComponent();
ChartEntity entity = findChartEntity(chartPanel, e);
ChartGesture.Entity gestureEntity = ChartGesture.getGestureEntity(entity);
Button button = Button.getButton(e.getButton());
// handle event
handleEvent(new ChartGestureEvent(chartPanel, e, entity,
new ChartGesture(gestureEntity, Event.MOUSE_WHEEL, button)));
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
Point p = e.getPoint();
boolean zoomIn = (e.getWheelRotation() < 0);
if (isInNavigationImage(p)) {
if (zoomIn) {
navZoomFactor = 1.0 + zoomIncrement;
} else {
navZoomFactor = 1.0 - zoomIncrement;
}
zoomNavigationImage();
} else if (isInImage(p)) {
if (zoomIn) {
zoomFactor = 1.0 + zoomIncrement;
} else {
zoomFactor = 1.0 - zoomIncrement;
}
zoomImage();
}
}
/**
* When event received will spin the carousel to select the next object.
* Because the wheel can be spun quicker than the carousel animates it keeps
* track of the target (so the user may have selected something three
* components away, althought the animation has not yet finished moving past
* the first comopnent)
*
* @param mouseWheelEvent
* The event object
*/
public void mouseWheelMoved(MouseWheelEvent mouseWheelEvent) {
if (mouseWheelEvent.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
int amount = mouseWheelEvent.getWheelRotation();
if (lastWheeledTo == null) {
lastWheeledTo = getFrontmost();
}
int lastPosition = layout.getComponentIndex(lastWheeledTo);
int frontMostPosition = layout.getComponentIndex(getComponent(0));
// Don't over spin
if (Math.abs(lastPosition - frontMostPosition) > layout
.getComponentCount() / 4) {
return;
}
if (amount > 0) {
lastWheeledTo = layout.getPreviousComponent(lastWheeledTo);
} else {
lastWheeledTo = layout.getNextComponent(lastWheeledTo);
}
bringToFront(lastWheeledTo);
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
double distanceX = maxX - minX;
double distanceY = maxY - minY;
double cursorX = minX + distanceX / 2.0;
double cursorY = minY + distanceY / 2.0;
int rotation = e.getWheelRotation();
if (rotation < 0) {
distanceX /= COEFF_ZOOM;
distanceY /= COEFF_ZOOM;
} else {
distanceX *= COEFF_ZOOM;
distanceY *= COEFF_ZOOM;
}
minX = cursorX - distanceX / 2.0;
maxX = cursorX + distanceX / 2.0;
minY = cursorY - distanceY / 2.0;
maxY = cursorY + distanceY / 2.0;
repaint();
}
private void dispatchScrollEvent(final int x, final int y,
final int modifiers, final double delta) {
final long when = System.currentTimeMillis();
final int scrollType = MouseWheelEvent.WHEEL_UNIT_SCROLL;
final int scrollAmount = 1;
int wheelRotation = (int) delta;
int signum = (int) Math.signum(delta);
if (signum * delta < 1) {
wheelRotation = signum;
}
// invert the wheelRotation for the peer
eventNotifier.notifyMouseWheelEvent(when, x, y, modifiers, scrollType,
scrollAmount, -wheelRotation, -delta, null);
}
@Override
public void mouseWheelMoved(MouseWheelEvent event) {
if (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
int amt = event.getUnitsToScroll();
JScrollBar bar = event.isShiftDown() ? mHSB : mVSB;
bar.setValue(bar.getValue() + amt * bar.getUnitIncrement());
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (User.isNull()) {
return;
}
/*
* Turning with mouse wheel. Ignore all but the first to avoid flooding
* the server with turn commands.
*/
logger.debug(e.getClickCount() + " click count and " + e.getScrollType() + " scroll type and wheel rotation " + e.getWheelRotation());
if (e.getClickCount() <= 1) {
final User user = User.get();
Direction currentDirection = user.getDirection();
Direction newDirection = null;
if (e.getUnitsToScroll() > 0) {
// Turn right
newDirection = currentDirection.nextDirection();
} else {
// Turn left
newDirection =
currentDirection.nextDirection().oppositeDirection();
}
if (newDirection != null && newDirection != currentDirection) {
final RPAction turnAction = new RPAction();
turnAction.put(TYPE, FACE);
turnAction.put(DIR, newDirection.get());
client.send(turnAction);
}
}
}
@Override
public void handleEvent(AWTEvent e) {
if (e instanceof MouseWheelEvent) {
MouseWheelEvent wheelEvent = (MouseWheelEvent) e;
//java.awt.ScrollPane consumes the event
// in case isWheelScrollingEnabled() is true,
// forcibly send the consumed event to the delegate
if (getTarget().isWheelScrollingEnabled() && wheelEvent.isConsumed()) {
sendEventToDelegate(wheelEvent);
}
} else {
super.handleEvent(e);
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
final JViewport scrollpane = (JViewport) getParent();
final Point pos = scrollpane.getViewPosition();
final int y;
final int SCROLL_HEIGHT = 50;
if (e.getWheelRotation() < 0) {
y = pos.y - (e.getScrollAmount() * SCROLL_HEIGHT);
} else {
y = pos.y + (e.getScrollAmount() * SCROLL_HEIGHT);
}
scrollpane.setViewPosition(new Point(0, Math.max(0, y)));
}
/**
* Handle the case where a plot implements the {@link Zoomable} interface.
*
* @param zoomable the zoomable plot.
* @param e the mouse wheel event.
*/
private void handleZoomable(Zoomable zoomable, MouseWheelEvent e) {
// don't zoom unless the mouse pointer is in the plot's data area
ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo();
PlotRenderingInfo pinfo = info.getPlotInfo();
Point2D p = this.chartPanel.translateScreenToJava2D(e.getPoint());
if (!pinfo.getDataArea().contains(p)) {
return;
}
Plot plot = (Plot) zoomable;
// do not notify while zooming each axis
boolean notifyState = plot.isNotify();
plot.setNotify(false);
int clicks = e.getWheelRotation();
double zf = 1.0 + this.zoomFactor;
if (clicks < 0) {
zf = 1.0 / zf;
}
if (chartPanel.isDomainZoomable()) {
zoomable.zoomDomainAxes(zf, pinfo, p, true);
}
if (chartPanel.isRangeZoomable()) {
zoomable.zoomRangeAxes(zf, pinfo, p, true);
}
plot.setNotify(notifyState); // this generates the change event too
}
@SuppressWarnings("fallthrough")
public void handleEvent(AWTEvent e) {
int id = e.getID();
if ((e instanceof InputEvent) && !((InputEvent)e).isConsumed() &&
((Component)target).isEnabled())
{
if (e instanceof MouseEvent && !(e instanceof MouseWheelEvent)) {
handleJavaMouseEvent((MouseEvent) e);
} else if (e instanceof KeyEvent) {
if (handleJavaKeyEvent((KeyEvent)e)) {
return;
}
}
}
switch(id) {
case PaintEvent.PAINT:
// Got native painting
paintPending = false;
// Fallthrough to next statement
case PaintEvent.UPDATE:
// Skip all painting while layouting and all UPDATEs
// while waiting for native paint
if (!isLayouting && ! paintPending) {
paintArea.paint(target,shouldClearRectBeforePaint());
}
return;
case FocusEvent.FOCUS_LOST:
case FocusEvent.FOCUS_GAINED:
handleJavaFocusEvent((FocusEvent)e);
default:
break;
}
// Call the native code
nativeHandleEvent(e);
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() < 0) {
getZoomInAction().actionPerformed(
new ActionEvent(ContentButton.this, 0,
ACTION_MAP_KEY_ZOOM_IN));
} else if (e.getWheelRotation() > 0) {
getZoomOutAction().actionPerformed(
new ActionEvent(ContentButton.this, 0,
ACTION_MAP_KEY_ZOOM_OUT));
}
}
/**
* Handle the case where a plot implements the {@link Zoomable} interface.
*
* @param zoomable the zoomable plot.
* @param e the mouse wheel event.
*/
private void handleZoomable(Zoomable zoomable, MouseWheelEvent e) {
// don't zoom unless the mouse pointer is in the plot's data area
ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo();
PlotRenderingInfo pinfo = info.getPlotInfo();
Point2D p = this.chartPanel.translateScreenToJava2D(e.getPoint());
if (!pinfo.getDataArea().contains(p)) {
return;
}
Plot plot = (Plot) zoomable;
// do not notify while zooming each axis
boolean notifyState = plot.isNotify();
plot.setNotify(false);
int clicks = e.getWheelRotation();
double zf = 1.0 + this.zoomFactor;
if (clicks < 0) {
zf = 1.0 / zf;
}
if (chartPanel.isDomainZoomable()) {
zoomable.zoomDomainAxes(zf, pinfo, p, true);
}
if (chartPanel.isRangeZoomable()) {
zoomable.zoomRangeAxes(zf, pinfo, p, true);
}
plot.setNotify(notifyState); // this generates the change event too
}
private void mainEditorPanelMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
if (this.selectAreaMode) {
this.selectAreaMode = false;
this.mainEditor.resetSelectArea();
} else if (evt.getModifiersEx() == MouseWheelEvent.CTRL_DOWN_MASK) {
if (evt.getWheelRotation() < 0) {
this.mainEditor.zoomIn();
} else {
this.mainEditor.zoomOut();
}
}
updateBottomBar();
}