Zooming to a User Defined Area in VirtualViewer

Snowbound’s viewing applet, VirtualViewer, includes a feature called Rubber Band Zoom, which allows the user to draw a box around a portion of the image to zoom in to. This tip shows how to implement this feature.

The following is the actionListener code associated with the Zoom Rubber Band menu item:

public void actionPerformed(ActionEvent e)

{

setMode(ZOOM, ZOOM_IDS.ZOOM_RUBBER_BAND);

imagePanel.getListenerDispatch()

.setActiveListenerById(ListenerDispatcher.POST_COMMAND);

}

The setMode call tells the system to activate the Zoom_Rubber_Band mode. The setActiveListenerById instructs the imagePanel to activate the appropriate MouseListener and MouseMotionListener when the user draws the zoom box.

Here are the key members and methods for our MouseListener/MouseMotionListener that become active when the user selects the Zoom Rubber Band menu item:

// the user defined Rectangle

java.awt.Rectangle selRect;

/**

*This creates a start point for the Rectangle.

*/

public void mousePressed(final MouseEvent e)

{

imagePanel.requestFocus();

selrect = new Rectangle();

selrect.x = e.getX();

selrect.y = e.getY();

selrect.width = 5;

selrect.height = 5;

snbd_draw_pen();

}

/**

*This defines the Rectangle according to where the user drags the mouse.

*/

public void mouseDragged(MouseEvent e)

{

if (e.getX() > selrect.x && e.getY() > selrect.y)

{

snbd_draw_pen();

selrect.width = e.getX() - selrect.x;

selrect.height = e.getY() - selrect.y;

snbd_draw_pen();

}

}

/**

*This performs the graphics calls to show the outline of the Rectangle.

*/

private void snbd_draw_pen()

{

Graphics g;

int xsize, ysize;

Insets in;

Dimension d;

if (selrect == null)

{

return;

}

g = imagePanel.getGraphics();

in = imagePanel.getInsets();

d = imagePanel.getSize();

d.width -= (in.right + in.left);

d.height -= (in.top + in.bottom);

g.setXORMode(Color.white);

xsize = selrect.width;

ysize = selrect.height;

g.drawRect(selrect.x, selrect.y, xsize, ysize);

g.setPaintMode();

g.dispose();

}

/**

*The user has defined the rectangle. The postCommand method

* associated with our Zoom_Rubber_Band Menu will be executed.

*/

public void mouseReleased(final MouseEvent e)

{

snbd_draw_pen();

activeMenu.performPostCommand(e.getX(), e.getY(), selrect);

imagePanel.getListenerDispatch()

.setActiveListenerById(ListenerDispatcher.DEFAULT);

imagePanel.setCursor(Cursor.getPredefinedCursor

(Cursor.DEFAULT_CURSOR));

mainApp.fireToolTip("");

return;

}

The following is the performPostCommand method in the Menu class that contains the Zoom_Rubber_Band menu item:

public void performPostCommand(int x, int y, Rectangle selrect)

{

Point upperLeft = new Point();

Point lowerRight = new Point();

Snowbnd Simage = imagePanel.getImage();

upperLeft.x = selrect.x;

upperLeft.y = selrect.y;

// adjust the screen coordinates to image coordinates

Simage.map_wnd_to_image(imagePanel, upperLeft);

lowerRight.x = selrect.x + selrect.width;

lowerRight.y = selrect.y + selrect.height;

// adjust screen coordinates to image coordinates

Simage.map_wnd_to_image(imagePanel, lowerRight);

// adjust

if (upperLeft.x > lowerRight.x)

{

selrect.x = upperLeft.x;

upperLeft.x = lowerRight.x;

lowerRight.x = selrect.x;

}

if (upperLeft.y > lowerRight.y)

{

selrect.y = upperLeft.y;

upperLeft.y = lowerRight.y;

lowerRight.y = selrect.y;

}

imagePanel.zoom = imagePanel

.getImage().set_croprect_scroll(imagePanel,

upperLeft.x,

upperLeft.y,

lowerRight.x - upperRight.x,

lowerRight.y - upperLeft.y,

1);

if (imagePanel.zoom < 0)

{

imagePanel.zoom = 0;

}

imagePanel.repaint();

}

Learn More

In addition to our Tech Tips section, our document and image Solutions Section provides business application and industry-specific examples, including web viewing and batch file conversion.

Category:

Online Demo