public synchronized void paint(Graphics gin) { Graphics2D g = (Graphics2D) gin; if (im == null) return; int height = getHeight(); int width = getWidth(); if (fit) { t = new AffineTransform(); double scale = Math.min(((double) width) / im.getWidth(), ((double) height) / im.getHeight()); // we'll re-center the transform in a moment. t.scale(scale, scale); } // if the image (in either X or Y) is smaller than the view port, then center // the image with respect to that axis. double mwidth = im.getWidth() * t.getScaleX(); double mheight = im.getHeight() * t.getScaleY(); if (mwidth < width) t.preConcatenate( AffineTransform.getTranslateInstance((width - mwidth) / 2.0 - t.getTranslateX(), 0)); if (mheight < height) t.preConcatenate( AffineTransform.getTranslateInstance(0, (height - mheight) / 2.0 - t.getTranslateY())); // if we're allowing panning (because only a portion of the image is visible), // don't allow translations that show less information that is possible. Point2D topleft = t.transform(new Point2D.Double(0, 0), null); Point2D bottomright = t.transform(new Point2D.Double(im.getWidth(), im.getHeight()), null); if (mwidth > width) { if (topleft.getX() > 0) t.preConcatenate(AffineTransform.getTranslateInstance(-topleft.getX(), 0)); if (bottomright.getX() < width) t.preConcatenate(AffineTransform.getTranslateInstance(width - bottomright.getX(), 0)); // t.translate(width-bottomright.getX(), 0); } if (mheight > height) { if (topleft.getY() > 0) t.preConcatenate(AffineTransform.getTranslateInstance(0, -topleft.getY())); if (bottomright.getY() < height) t.preConcatenate(AffineTransform.getTranslateInstance(0, height - bottomright.getY())); } g.drawImage(im, t, null); }
public void mouseDragged(MouseEvent e) { if (e.getModifiers() == modifiers) { Point2D pEndScale = e.getPoint(); double scale = pEndScale.distance(viewOrigin) / pStartScale.distance(viewOrigin); AffineTransform transform = super.plot.getTransform(); transform.setTransform( transform.getScaleX() * scale, transform.getShearY(), transform.getShearX(), transform.getScaleY() * scale, transform.getTranslateX() * scale, transform.getTranslateY() * scale); pStartScale = pEndScale; super.plot.setTransform(transform); } }
// zoom out a notch void zoomOut(Point2D p) { double nz = t.getScaleX() * .5; nz = Math.max(nz, 1.0 / 32.0); adjustZoom(nz, p); }
// zoom in a notch void zoomIn(Point2D p) { double nz = t.getScaleX() * 2.0; nz = Math.min(nz, 64); adjustZoom(nz, p); }