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

View File

@ -66,6 +66,10 @@ public class ScaleScrollPane extends JScrollPane
// is the subject *currently* being fitting
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.
*
@ -99,38 +103,37 @@ public class ScaleScrollPane extends JScrollPane
this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
getHorizontalScrollBar().setUnitIncrement(50);
//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().setBlockIncrement(viewport.getHeight()); // the default value is good
viewport.addMouseListener(this);
viewport.addMouseMotionListener(this);
figure.addChangeListener(new StateChangeListener() {
@Override
public void stateChanged(EventObject e) {
horizontalRuler.updateSize();
verticalRuler.updateSize();
if(fit) {
figure.scaleTo(viewport.getExtentSize());
}
figure.addChangeListener( e -> {
horizontalRuler.updateSize();
verticalRuler.updateSize();
if(fit) {
final java.awt.Dimension calculatedViewSize = new java.awt.Dimension(getWidth() - viewportMarginPx.width, getHeight() - viewportMarginPx.height);
figure.scaleTo(calculatedViewSize);
}
});
viewport.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if(fit) {
figure.scaleTo(viewport.getExtentSize());
}
figure.updateFigure();
if(fit) {
final Dimension calculatedViewSize = new Dimension(getWidth() - viewportMarginPx.width, getHeight() - viewportMarginPx.height);
figure.scaleTo(calculatedViewSize);
}
figure.updateFigure();
horizontalRuler.updateSize();
verticalRuler.updateSize();
horizontalRuler.updateSize();
verticalRuler.updateSize();
}
});
@ -160,10 +163,8 @@ public class ScaleScrollPane extends JScrollPane
final Point zoomPoint = figure.getAutoZoomPoint();
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.invalidate();
revalidate();
}
}