You likely don't care about this, it's all greek to you (rare, likely imaginary, reader of this blog). But, mark my words. Somebody is going to google and find this and love the hell out of it. It will make their day.
You can ensure it zooms to a specific location by giving it a rectangle similar to the view field.
protected void zoomrange(Rectangle rectangle) { Rectangle viewBounds = getView().getVisibleRect(); double zoomw = (float) viewBounds.width / rectangle.width; double zoomh = (float) viewBounds.height / rectangle.height; double zoom = min(zoomw, zoomh); Rectangle normaled = new Rectangle(rectangle.x, rectangle.y, (int)(viewBounds.width / zoom), (int)(viewBounds.height / zoom)); getSceneAnimator().animateZoomFactor(zoom); getSceneAnimator().getZoomAnimator().addAnimatorListener(new FocusZoomAnimatorListener(this, normaled)); }
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JComponent;
import org.netbeans.api.visual.animator.AnimatorEvent;
import org.netbeans.api.visual.animator.AnimatorListener;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
/**
* @author Tat
*/
public class FocusZoomAnimatorListener implements AnimatorListener {
private Point point;
private Rectangle rectangle,viewRect;
private Point center;
private Point mouseLocation;
private JComponent view;
private Scene scene;
private Widget widget;
public FocusZoomAnimatorListener(Widget widget, Point point) {
this.widget = widget;
this.point = point;
scene = widget.getScene();
view = scene.getView();
}
public FocusZoomAnimatorListener(Widget widget, Rectangle rectangle) {
this.widget = widget;
this.rectangle = rectangle;
scene = widget.getScene();
view = scene.getView();
}
@Override
public void animatorStarted(AnimatorEvent ae) {
}
@Override
public void animatorReset(AnimatorEvent ae) {
}
@Override
public void animatorFinished(AnimatorEvent ae) {
}
@Override
public void animatorPreTick(AnimatorEvent ae) {
if (point != null) {
viewRect = view.getVisibleRect();
center = widget.convertLocalToScene(point);
mouseLocation = scene.convertSceneToView(center);
}
}
@Override
public void animatorPostTick(AnimatorEvent ae) {
if (point != null) {
center = scene.convertSceneToView(center);
view.scrollRectToVisible(new Rectangle(
center.x - (mouseLocation.x - viewRect.x),
center.y - (mouseLocation.y - viewRect.y),
viewRect.width,
viewRect.height));
} else {
viewRect = scene.convertSceneToView(rectangle);
view.scrollRectToVisible(viewRect);
}
}
}
2 comments:
Thank you. You made my day ;o)
lol
Post a Comment