canProccessMouseWheelEvent = true;
if ((oldZoom != newZoom) && (getParent() instanceof JViewport) ){
// Update view position regarding mouse position
JViewport viewPort = (JViewport)getParent();
Point viewpos = viewPort.getViewPosition();
// Compute ratio to appy to view position
double ratioX = ( e.getX() - viewpos.x) / (double)( viewPort.getWidth());
double ratioY = ( e.getY() - viewpos.y) / (double)( viewPort.getHeight());
// Find mouse position in new zoom view
double mousePosX = (e.getX()/ oldZoom) * newZoom;
double mousePosY = (e.getY()/ oldZoom) * newZoom;
viewpos.x = (int) (mousePosX - viewPort.getWidth()* ratioX );
viewpos.y = (int) (mousePosY - viewPort.getHeight() * ratioY);
// Cannot exceed component dimension
if (viewpos.x < 0){
viewpos.x = 0;
}
if (viewpos.y < 0){
viewpos.y = 0;
}
Dimension viewDim = viewPort.getViewSize();
if (viewpos.x + viewPort.getWidth() > viewDim.getWidth()){
viewpos.x = (int) (viewDim.getWidth() - viewPort.getWidth());
}
if (viewpos.y + viewPort.getHeight() > viewDim.getHeight()){
viewpos.y = (int) (viewDim.getHeight() - viewPort.getHeight());
}
viewPort.setViewPosition(viewpos);
}
}
if (!canProccessMouseWheelEvent){ // dispatch event to parent
getParent().dispatchEvent(e);