Blog: Tech Talk

agsdix-fas fa-home

Blog: Home

agsdix-fas fa-pen-fancy

Blog: CEO's Corner

agsdix-fas fa-code

Blog: Tech Talk

Blog: Product Releases

agsdix-fas fa-chalkboard-teacher

Blog: Viewing

Blog: Conversion

arrow left circle icon Blog: Tech Talk

Zooming to a User Defined Area in VirtualViewer

by | Apr 24, 2012

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)






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)



selrect = new Rectangle();

selrect.x = e.getX();

selrect.y = e.getY();

selrect.width = 5;

selrect.height = 5;




*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)



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

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





*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)




g = imagePanel.getGraphics();

in = imagePanel.getInsets();

d = imagePanel.getSize();

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

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


xsize = selrect.width;

ysize = selrect.height;

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





*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)



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








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




lowerRight.x – upperRight.x,

lowerRight.y – upperLeft.y,


if (imagePanel.zoom < 0)


imagePanel.zoom = 0;




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.