Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Zoom

As with pan, different applications provide different types of user interfaces for zooming images. Some applications allow you to zoom in or zoom out by clicking on the image. When the image is magnified or reduced, the point on the image where you clicked remains at the same position on the viewport. In some applications you are asked to mark a rectangular region over the image. The image within this rectangular region is then zoomed onto the viewport.

As we did with ScrollController to implement the pan operation, let's create an interface called ZoomController for implementing GUI-independent zoom logic and then build a GUI-specific class for a zoom feature. The ZoomController interface will have a property called magFactor whose set and get methods are

  • public void setMagFactor(double magFactor);

  • public double getMagFactor();

We'll design two types of magnification methods:

  1. A method for magnifying the original image by a specified factor

  2. A method for magnifying a displayed image by a specified factor and then setting the magnified image as the new displayed image

Both types of methods are required in practice.

Magnifying the Original Image

Let's specify the following method for magnifying the original image:

  • public boolean magnify(int magCenterX, int magCenterY, double mag);

This method will reset any other transformation that has been performed on the image, except when the image is transformed to set the displayMode and flipMode properties. It magnifies the image with (magCenterX, magCenterY) as the coordinates of the center of magnification. This means that if you click at position P(magCenterX,magCenterY) on the viewport, the corresponding point on the magnified image will be located at the same position (see Figure 7.8).

FIGURE 7.8 Zooming an image

Let's define one more flavor of the magnify() method for implementing the zoom feature:

  • public boolean magnify(int magCenterX, int magCenterY);

This method is same as the preceding magnify() method, except that it gets the magnification factor from the magFactor property.

Magnifying the Displayed Image

It may be convenient in some cases to magnify the displayed image itself. To meet this requirement, let's define one more paintImage() method:

  • public void paintImage(int magCenterX, int magCenterY, double mag);

When this method is executed, the magnified image becomes the new displayImage object.

The ZoomController interface, shown in Listing 7.9, contains all the methods defined here.

LISTING 7.9 The ZoomController interface

public interface ZoomController {
  public void setMagFactor(double magFactor);
  public double getMagFactor();
  public void magnify(int magCenterX, int magCenterY, double mag);
  public void magnify(int magCenterX, int magCenterY);
  public void paintImage(int magCenterX, int magCenterY, double mag);
 }

Implementing Zoom

The ZoomController interface specifies several methods for magnification as well. The design of the zoom feature is similar to the scroll feature. We'll define two classes: one for controlling zoom, and the other for the GUI:

  1. Zoom. This class will control zoom by implementing the ZoomController interface.

  2. ZoomGUI. This class will implement the zoom feature. It will contain the mouse user interface.

The zoom feature follows the same design pattern as scroll (see Figure 7.9). Listing 7.10 shows the code for the Zoom class.

FIGURE 7.9 The data flow for zoom

LISTING 7.10 The Zoom class

package com.vistech.imageviewer;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
public class Zoom implements ZoomController {
  protected AffineTransform atx = new AffineTransform();
  protected boolean magOn = true;
  protected double magFactor = 1.0;
  protected int magCenterX = 0;
  protected int magCenterY =0;
  protected Point zoomOffset = new Point(0,0);
  protected ImageManipulator imageCanvas;

  public Zoom(){}
  public Zoom(ImageManipulator imageCanvas){ this.imageCanvas = imageCanvas;}

  public void setImageManipulator(ImageManipulator imageCanvas){
   this.imageCanvas = imageCanvas;
  }

  public void setMagOn(boolean onOff){ magOn = onOff;}
  public boolean getMagOn(){ return magOn; }

  public void setMagFactor(double magFactor){
   this.magFactor = magFactor;
   imageCanvas.setMagFactor(magFactor);
  }

  public double getMagFactor(){ return magFactor;}

  public void magnify(int magCenterX, int magCenterY){
    magnify(magCenterX, magCenterY, magFactor);
  }

  public void magnify(int magCenterX, int magCenterY, double magFac){
   setMagFactor(magFac);
   this.magCenterX = magCenterX;
   this.magCenterY = magCenterY;
   Point panOffset = imageCanvas.getPanOffset();
   int x = (int)((magCenterX-panOffset.x)*magFactor)-magCenterX;
   int y = (int)((magCenterY-panOffset.y)*magFactor)-magCenterY;
   atx = imageCanvas.getTransform();
   atx.setToTranslation(-x, -y);
   atx.scale(magFactor, magFactor);
   applyTransform(atx);
  }

  public void paintImage(int magCenterX, int magCenterY, double mag){
   setMagFactor(this.magFactor *mag);
   int dx = this.magCenterX -magCenterX;
   int dy = this.magCenterY-magCenterY;
   this.magCenterX = magCenterX;
   this.magCenterY = magCenterY;
   try {
    Point2D mgp = null;
    atx = imageCanvas.getTransform();
    mgp =
    atx.inverseTransform((Point2D)(new Point(magCenterX, magCenterY)),                               
                                                        (Point2D)mgp);
    double x = (mgp.getX()*mag)-mgp.getX();
    double y = (mgp.getY()*mag)-mgp.getY();
    scale(-x,-y, mag);
   }catch (Exception e) {System.out.println(e); }
  }

  public void scale(double magOffsetX, double magOffsetY, double mag){
   atx.translate(magOffsetX,magOffsetY);
   atx.scale(mag,mag);
   applyTransform(atx);
  }

  public void resetAndScale(double magOffsetX, double magOffsetY, double mag){
   atx.setToTranslation(magOffsetX,magOffsetY);
   atx.scale(mag,mag);
   applyTransform(atx);
 }

  public void applyTransform(AffineTransform atx) {
   imageCanvas.applyTransform(atx);
  }

  public void reset() {
   magCenterX = 0;
   magCenterY = 0;
   magFactor = 1.0;
  }
}

The paintImage() method is the key method for implementation of the zoom feature. The mag input parameter holds the zoom increment or decrement value. Just as with the scroll() method in the scroll feature, paintImage() converts the point at which the mouse is clicked to the image space by using the inverse transformation. Once that point has been obtained, paintImage() computes the amount of translation that needs to be applied. As Figure 7.8 shows, translations in the x and y directions are calculated as follows:

Tx 5 (mag 3 x) – x
Ty 5 (mag 3 y) – y

The scale() method takes these translations and the mag increment value as inputs.

Scaling

The scale() method of the Zoom class does the uniform scaling. The scaling is performed at a reference point (the magnification center) whose coordinates are specified by magCenterX and magCenterY. The scale() method of the AffineTransform class performs scaling with the origin as the reference point. In most applications, however, the reference point is somewhere in the viewport. This can be a point where the mouse is clicked, or it can be the center of a rectangle that was drawn over the image. To achieve scaling at a reference point, the image is translated by a certain amount. So the scale() method of the Zoom class first translates the image to the reference point specified by (magOffsetX, magOffsetY) and then scales it by mag.

The resetAndScale() method performs absolute scaling. It resets atx and translates the images to (magOffsetX, magOffsetY) and then applies scaling.

User Interface for Zoom

Let's design a GUI interface that zooms an image in or out at a position where the mouse is clicked. The zoom operator has two modes: zoom in and zoom out. If the zoom-in mode is selected, the magnification of the image increases every time the user clicks the mouse. If the zoom-out mode is selected, the opposite happens.

The ZoomGUI class implements the MouseListener interface. That means it captures only mouse events, and not mouseMotion events. Listing 7.11 shows the code for ZoomGUI.

LISTING 7.11 The ZoomGUI class

package com.vistech.imageviewer;
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class ZoomGUI implements MouseListener{
  protected ZoomController zoomController;
  protected final static double baseZoomFactor = 1.0;
  protected boolean zoomOn = false;
  protected double zoomFactor = 1.0;
  protected double increment = 0.1;
  protected boolean zoomOut = false;
  protected boolean mousePressed = false;

  public ZoomGUI(ZoomController c){ zoomController = c;}

  public void setZoomOn(boolean onOff){zoomOn = onOff;}
  public boolean getZoomOn(){ return zoomOn;}
  public void setZoomOut(boolean outIn){zoomOut = outIn;}
  public boolean getZoomOut(){ return zoomOut; }
  public void setZoomfactor(double mag){ zoomFactor = mag; }
  public double getZoomFactor(){ return zoomFactor; }
  public void setZoomIncrement(double incr){ increment = incr; }
  public double getZoomIncrement(){ return increment;}

  public void zoom(int x, int y, double zoomfact){
    if(zoomOut) {
     zoomController.paintImage(x,y,baseZoomFactor-increment);
     zoomFactor *= baseZoomFactor-increment;
    }
    else {
     zoomController.paintImage(x,y,baseZoomFactor+increment);
     zoomFactor *= baseZoomFactor+increment;
    }
  }
  public void reset() {
    setZoomOn(false);
    zoomFactor = 1.0;
  }
  public void mousePressed(MouseEvent e) {
    if(mousePressed) return;
    mousePressed = true;
    if(!zoomOn) return;
    if(SwingUtilities.isLeftMouseButton(e)){
     zoom(e.getX(), e.getY(), zoomFactor);
    }
  }
  public void mouseReleased(MouseEvent e){ mousePressed = false;}
  public void mouseClicked(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
}

The properties of the zoom operator include zoomOn, zoomOut, zoomFactor, and zoomIncrement. When you reset the zoom feature, the zoomFactor property starts with 1.0—that is, no magnification. When the mouse is clicked, the mousePressed() method calls zoom(), which increments or decrements zoomFactor depending on the current zoom mode. The zoom() method then calls the paintImage() method of ZoomController and passes the current position of the mouse and magFactor.

The following code fragment shows how to use the zoom-related classes:

ImageManipulator imageCanvas = new ImageCanvas2D();

ZoomController zoom = new Zoom(imageCanvas);
ZoomGUI zoomUI = new ZoomGUI(zoom);

imageCanvas.addMouseListener(zoomUI);
  • + Share This
  • 🔖 Save To Your Account