Merge pull request #707 from teyrana/fix/696-zoom-jitter

[fixes #696] Fixes occassional zitter when auto-zooming at some sizes
This commit is contained in:
Daniel Williams 2020-07-26 10:06:06 -04:00 committed by GitHub
commit bbc22e19b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 31 deletions

View File

@ -109,7 +109,6 @@ public abstract class AbstractScaleFigure extends JPanel {
* @param visibleBounds the visible bounds upon the Figure * @param visibleBounds the visible bounds upon the Figure
*/ */
public void scaleTo(final double newScaleRequest, final Dimension visibleBounds) { public void scaleTo(final double newScaleRequest, final Dimension visibleBounds) {
// System.err.println(String.format(" ::scaleTo: %6.4f ==>> %6.4f, %d x %d", userScale, newScaleRequest, visibleBounds.width, visibleBounds.height));
if (MathUtil.equals(this.userScale, newScaleRequest, 0.01) && if (MathUtil.equals(this.userScale, newScaleRequest, 0.01) &&
(visibleBounds_px.width == visibleBounds.width) && (visibleBounds_px.width == visibleBounds.width) &&
(visibleBounds_px.height == visibleBounds.height) ) (visibleBounds_px.height == visibleBounds.height) )
@ -117,7 +116,6 @@ public abstract class AbstractScaleFigure extends JPanel {
return;} return;}
if (Double.isInfinite(newScaleRequest) || Double.isNaN(newScaleRequest) || 0 > newScaleRequest) { if (Double.isInfinite(newScaleRequest) || Double.isNaN(newScaleRequest) || 0 > newScaleRequest) {
return;} return;}
// System.err.println(String.format(" => continue"));
this.userScale = MathUtil.clamp( newScaleRequest, MINIMUM_ZOOM, MAXIMUM_ZOOM); this.userScale = MathUtil.clamp( newScaleRequest, MINIMUM_ZOOM, MAXIMUM_ZOOM);
this.scale = baseScale * userScale; this.scale = baseScale * userScale;
@ -133,7 +131,6 @@ public abstract class AbstractScaleFigure extends JPanel {
* @param visibleBounds the visible bounds to scale this figure to. * @param visibleBounds the visible bounds to scale this figure to.
*/ */
public void scaleTo(Dimension visibleBounds) { public void scaleTo(Dimension visibleBounds) {
// System.err.println(String.format(" ::scaleTo: %d x %d", visibleBounds.width, visibleBounds.height));
if( 0 >= visibleBounds.getWidth() || 0 >= visibleBounds.getHeight()) if( 0 >= visibleBounds.getWidth() || 0 >= visibleBounds.getHeight())
return; return;
@ -141,8 +138,8 @@ public abstract class AbstractScaleFigure extends JPanel {
updateCanvasSize(); updateCanvasSize();
updateCanvasOrigin(); updateCanvasOrigin();
final double width_scale = (visibleBounds.width) / ((subjectBounds_m.getWidth() * baseScale) + 2 * borderThickness_px.width); final double width_scale = (visibleBounds.width - 2 * borderThickness_px.width) / (subjectBounds_m.getWidth() * baseScale);
final double height_scale = (visibleBounds.height) / ((subjectBounds_m.getHeight() * baseScale) + 2 * borderThickness_px.height); final double height_scale = (visibleBounds.height - 2 * borderThickness_px.height) / (subjectBounds_m.getHeight() * baseScale);
final double newScale = Math.min(height_scale, width_scale); final double newScale = Math.min(height_scale, width_scale);
scaleTo(newScale, visibleBounds); scaleTo(newScale, visibleBounds);
@ -169,9 +166,11 @@ public abstract class AbstractScaleFigure extends JPanel {
Dimension preferredFigureSize_px = new Dimension(desiredWidth, desiredHeight); Dimension preferredFigureSize_px = new Dimension(desiredWidth, desiredHeight);
if( (getWidth() != preferredFigureSize_px.width) || (getHeight() != preferredFigureSize_px.height)) {
setPreferredSize(preferredFigureSize_px); setPreferredSize(preferredFigureSize_px);
setMinimumSize(preferredFigureSize_px); setMinimumSize(preferredFigureSize_px);
} }
}
protected void updateTransform(){ protected void updateTransform(){
// Calculate and store the transformation used // Calculate and store the transformation used

View File

@ -66,6 +66,10 @@ public class ScaleScrollPane extends JScrollPane
// is the subject *currently* being fitting // is the subject *currently* being fitting
private boolean fit = false; private boolean fit = false;
// magic number. I don't know why this number works, but this nudges the figures to zoom correctly.
// n.b. it is slightly large than the ruler.width + scrollbar.width
final Dimension viewportMarginPx = new Dimension( 40, 40);
/** /**
* Create a scale scroll pane. * Create a scale scroll pane.
* *
@ -99,25 +103,23 @@ public class ScaleScrollPane extends JScrollPane
this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
getHorizontalScrollBar().setUnitIncrement(50); getHorizontalScrollBar().setUnitIncrement(50);
//getHorizontalScrollBar().setBlockIncrement(viewport.getWidth()); // the default value is good //getHorizontalScrollBar().setBlockIncrement(viewport.getWidth()); // the default value is good
setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
getVerticalScrollBar().setUnitIncrement(50); getVerticalScrollBar().setUnitIncrement(50);
//getVerticalScrollBar().setBlockIncrement(viewport.getHeight()); // the default value is good //getVerticalScrollBar().setBlockIncrement(viewport.getHeight()); // the default value is good
viewport.addMouseListener(this); viewport.addMouseListener(this);
viewport.addMouseMotionListener(this); viewport.addMouseMotionListener(this);
figure.addChangeListener(new StateChangeListener() { figure.addChangeListener( e -> {
@Override
public void stateChanged(EventObject e) {
horizontalRuler.updateSize(); horizontalRuler.updateSize();
verticalRuler.updateSize(); verticalRuler.updateSize();
if(fit) { if(fit) {
figure.scaleTo(viewport.getExtentSize()); final java.awt.Dimension calculatedViewSize = new java.awt.Dimension(getWidth() - viewportMarginPx.width, getHeight() - viewportMarginPx.height);
} figure.scaleTo(calculatedViewSize);
} }
}); });
@ -125,7 +127,8 @@ public class ScaleScrollPane extends JScrollPane
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
if(fit) { if(fit) {
figure.scaleTo(viewport.getExtentSize()); final Dimension calculatedViewSize = new Dimension(getWidth() - viewportMarginPx.width, getHeight() - viewportMarginPx.height);
figure.scaleTo(calculatedViewSize);
} }
figure.updateFigure(); figure.updateFigure();
@ -160,10 +163,8 @@ public class ScaleScrollPane extends JScrollPane
final Point zoomPoint = figure.getAutoZoomPoint(); final Point zoomPoint = figure.getAutoZoomPoint();
final Rectangle zoomRectangle = new Rectangle(zoomPoint.x, zoomPoint.y, (int)(view.getWidth()), (int)(view.getHeight())); final Rectangle zoomRectangle = new Rectangle(zoomPoint.x, zoomPoint.y, (int)(view.getWidth()), (int)(view.getHeight()));
// System.err.println(String.format("::zoom: @ %d, %d [ %d x %d ]", zoomRectangle.x, zoomRectangle.y, zoomRectangle.width, zoomRectangle.height));
figure.scrollRectToVisible(zoomRectangle); figure.scrollRectToVisible(zoomRectangle);
figure.invalidate();
revalidate(); revalidate();
} }
} }