/*
#************************************************************
#** This Java example is provided "as is". This code is **
#** not supported, but I will try to answer questions as **
#** time allows. Email: wizjd@panix.com **
#** Visit: for updated, **
#** and new examples. **
#************************************************************
*/
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import java.util.Vector;
/**
*
* Pan is a mostly stolen applet from Gamelan used to pan around in
* a small reference image, exposing a high resolution view into that
* image. I am just checking in a very raw version with sample images
* so we have an example for future work.
*
* To run the applet, you need the class file, the html file, and the
* two gif images specified in the html file.
*
* @author Tommy Jasmin
*
*/
public class Pan extends Applet
{
private scroll_box window_canvas;
Image small_map_img, big_map_img;
MediaTracker mt;
int xPos, yPos;
Vector imagemap_rect_vector;
//*************************** init *************************************
public void init()
{
this.resize(550,280);
super.init();
String imagemap_file_name = getParameter("imagemap_file_name");
String index_file_name = getParameter("index_file_name");
int scale = (Integer.valueOf(getParameter("scale"))).intValue();
big_map_img = getPicImage(imagemap_file_name);
small_map_img = getPicImage(index_file_name);
window_canvas = new scroll_box(big_map_img, small_map_img, scale, Color.yellow, Color.black);
setLayout (new BorderLayout() );
add(window_canvas);
}
//*************************** update *************************************
//Overide the default update method to avoid it blanking the screen and causing flickering
public void update(Graphics g) //Overide the default update method to avoid it
{ // blanking the screen and causing flickering
paint(g);
}
//*************************** handleEvent *************************************
//If the user clicked on the scrolling Canavs we will catch it here and
//do our html style image-map stuff
public boolean handleEvent(Event evt)
{
if ( evt.target == window_canvas && evt.id == Event.MOUSE_DOWN )
{
// translate these back to canvas context
evt.x = (evt.x - window_canvas.bounds().x);
evt.y = (evt.y - window_canvas.bounds().y);
process_imagemap_click(evt.x, evt.y);
}
return true; //Dont allow event to continue up
}
//********************** process_imagemap_click ****************************
public void process_imagemap_click( int x, int y)
{
for ( int i=0; i < imagemap_rect_vector.size(); i++)
{
if (try_url_match( x, y,
((Imagemap_rect)imagemap_rect_vector.elementAt(i)).upper_left_corner.x,
((Imagemap_rect)imagemap_rect_vector.elementAt(i)).upper_left_corner.y,
((Imagemap_rect)imagemap_rect_vector.elementAt(i)).lower_right_corner.x,
((Imagemap_rect)imagemap_rect_vector.elementAt(i)).lower_right_corner.y,
((Imagemap_rect)imagemap_rect_vector.elementAt(i)).the_url )
) { break; }
}
}
//*************************** try_url_match ****************************
public boolean try_url_match( int xPos, int yPos, int up_left_x, int up_left_y, int bot_right_x, int bot_right_y, String url_in)
{
if ( ( xPos >= up_left_x) && (xPos <= bot_right_x) &&
( yPos >= up_left_y) && (yPos <= bot_right_y) )
{
jump_to_url(url_in);
return true;
}
return false;
}
//*************************** jump_to_url ****************************
public void jump_to_url(String the_url)
{
try
{
URL url_to_goto = new URL(getCodeBase(), the_url);
getAppletContext().showDocument(url_to_goto);
//System.out.println("jumping to:" + url_to_goto + ":");
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
}
//*************************** getPicImage ****************************
// Use MediaTracker to insure the images are fully loaded before
// we try to use them
private Image getPicImage(String image_file_name)
{
System.out.println(" - getPicImage in, filename: " + image_file_name);
Image img_work = null;
mt = new MediaTracker(this);
try
{
img_work = getImage(getDocumentBase(),image_file_name);
}
catch(Exception e1)
{
System.out.println(e1);
}
mt.addImage(img_work, 0);
try
{
showStatus("Loading image " + image_file_name );
mt.checkID(0, true);
mt.waitForID(0);
}
catch(InterruptedException e2)
{
System.out.println(e2);
}
System.out.println(" - getPicImage out ok");
return img_work;
}
}
//*********************************** end of "Pan" applet **********
//*******************************************************************************
//** A Class for holding the corners of a image-map rectangle and the URL **
//** that clicks inside that rectangle point to. **
//*******************************************************************************
class Imagemap_rect
{
Point upper_left_corner, lower_right_corner;
String the_url;
Imagemap_rect(int x1_in, int y1_in, int x2_in, int y2_in, String the_url_in)
{
upper_left_corner = new Point(x1_in, y1_in);
lower_right_corner = new Point(x2_in, y2_in);
the_url = the_url_in;
}
}
/*
#************************************************************
#** **
#** Scroll Box Class **
#** Ver: 1.1 **
#** Date: 01/11/97 **
#** Author: John Donohue (wizjd@panix.com) **
#** **
#** This Java example is provided "as is". This code is **
#** not supported, but I will try to answer questions as **
#** time allows. Email: wizjd@panix.com **
#** Visit: for updated, **
#** and new examples. **
#************************************************************
*/
//*******************************************************************************
//** This canvas is used for the "peek window" which displays a portion of **
//** of the big picture. Which portion shown is controlled by dragging the **
//** zooming rectangle across the minature index version of the picture in the **
//** lower right corner **
//*******************************************************************************
class scroll_box extends Canvas
{
Image big_pic_img; //Ptr to the big picture
Image index_pic_img; //Ptr to the small index picture
int scale_factor; //How much of the big pic we show at once ( 4 = 1/4)
Color zoom_rect_color; //Color of the zooming rectangle and border of the peek window
Color zoom_border_color; //Color of the border around th index image
Image img_buffer = null; //Work area to build our image in
Image peek_img_buffer = null; //Work area to build our image in
Graphics g_buffer; //Graphics context for our work area
int bt = 5;
Point index_pos; //The displacement of the small index picture in the canvas
Point rect_pos; //The current positon of the zooming rectangle in the index pic
//The following variables are constants for each image, we calculate and save them
//so that we dont have to recalculate them every time we need them.
int big_pic_width; //Save the width of the big picture
int big_pic_height; //Save the height of the big picture
int peek_window_width; //Save the width of the window we peek at the big picture through
int peek_window_height; //Save the height of the window we peek at the big pic through
int index_pic_width; //Save the width of the small index pic that we zoom around on
int index_pic_height; //Save the height of the small index pic that we zoom around on
int img_buffer_width;
int img_buffer_height;
int rect_width; //Save the width of the zooming rectangle
int rect_height; //Save the height of the zooming rectangle
int disp_rect_center_x; //X displacement of index pic in the canvas + the 1/2 rect width
int disp_rect_center_y; //Y displacement of index pic in the canvas + the 1/2 rect height
int max_rect_x; //Maximum X of rect in index pic(keeps us from shooting past edge)
int max_rect_y; //Maximum Y of rect in index pic(keeps us from shooting past edge)
int index_to_big_multiple; //Ratio of index pic to big pic * -1
//Constructor:
scroll_box( Image big_pic_in_img, Image index_pic_img_in, int scale_in,
Color zoom_rect_color_in, Color zoom_border_color_in)
{
big_pic_img = big_pic_in_img;
index_pic_img = index_pic_img_in;
scale_factor = scale_in;
zoom_rect_color = zoom_rect_color_in;
zoom_border_color = zoom_border_color_in;
big_pic_width = big_pic_img.getWidth(null);
big_pic_height = big_pic_img.getHeight(null);
index_pic_width = index_pic_img.getWidth(null);
index_pic_height = index_pic_img.getHeight(null);
//The scaling factor determines the fraction of the big pic we display at once,
//by setting the size of the peek window and the zooming rectangle in the index pic.
peek_window_width = big_pic_width/scale_factor;
peek_window_height = big_pic_height/scale_factor;
System.out.println(" canvas size: " + peek_window_width + ", " +
peek_window_height);
img_buffer_width = peek_window_width + (2 * bt);
img_buffer_height = peek_window_height + (2 * bt);
rect_width = index_pic_width/scale_factor;
rect_height =index_pic_height/scale_factor;
//The ratio of the index picture to the big picture. We use this when translating
//the X/Y of the zooming rectangle in the index image to the "position" of the
//peek window in the big picture. This is a negative number so that we start
//drawing above and to the left of our peek window's 0,0 origin. The further
//"up and to the left" we begin drawing the big pic the more "down and to the
//right" will be the portion displayed in the peek window.
index_to_big_multiple = (-1 * (big_pic_width / index_pic_width));
//Put the index image at the lower right corner of the canvas
index_pos = new Point( peek_window_width - index_pic_width, peek_window_height - index_pic_height);
//Calc the dispalcement of the center of the zoom rectangle in the canvas context
disp_rect_center_x = index_pos.x + (rect_width/2);
disp_rect_center_y = index_pos.y + (rect_height/2);
//Calc the limits of the zoom rect in the index image
max_rect_x = (index_pic_width - rect_width) -2;
max_rect_y = (index_pic_height - rect_height) -2;
//The zooming rectangle starts at the upper left corner in the index picture
rect_pos = new Point(0,0);
peek_img_buffer = createImage( peek_window_width, peek_window_height);
img_buffer = createImage( img_buffer_width, img_buffer_height);
this.resize(img_buffer_width, img_buffer_height);
}
//Overide the default update method to avoid it blanking the screen and causing flickering
public void update(Graphics g)
{
paint(g);
}
public void paint (Graphics g)
{
if ( peek_img_buffer == null) peek_img_buffer = createImage( peek_window_width, peek_window_height);
if ( img_buffer == null) img_buffer = createImage( img_buffer_width, img_buffer_height);
g_buffer = peek_img_buffer.getGraphics();
g_buffer.drawImage(big_pic_img, rect_pos.x * index_to_big_multiple, rect_pos.y * index_to_big_multiple, this);
g_buffer.drawImage(index_pic_img, index_pos.x, index_pos.y, this);
g_buffer.setColor(zoom_border_color);
g_buffer.drawRect(index_pos.x -1, index_pos.y -1, index_pic_width, index_pic_height);
g_buffer.setColor(zoom_rect_color);
g_buffer.drawRect(rect_pos.x + index_pos.x, rect_pos.y + index_pos.y, rect_width, rect_height);
g_buffer = img_buffer.getGraphics();
g_buffer.setColor(zoom_rect_color);
g_buffer.fillRect(0,0,img_buffer_width, img_buffer_height);
g_buffer.drawImage(peek_img_buffer, bt, bt, null);
g.drawImage(img_buffer, 0, 0, this);
}
// If the user clicks or drags the mouse on the Canvas we will look at if they
// did it inside the index picture. If they did we will process this event here.
// If it was outside the index pic we will allow the event to continue up the
// hierarchy to the applet ( or whatever instantiated this canvas)- after
// changing the x/y to show where the click was in terms of the big picture
// not this canvas.
// This is so that the mouse clicks can be used in the applet to do HTML image-map
// type functions.
public boolean handleEvent(Event evt)
{
int click_x, click_y;
//Was it a click on our canvas ?
if ( (evt.target == this) &&
( (evt.id == Event.MOUSE_DOWN) || (evt.id == Event.MOUSE_DRAG) ) )
{
click_x = evt.x - bt;
click_y = evt.y - bt;
// It was in the canvas but was it on our index picture ?
if ( ( click_x > index_pos.x) && ( click_y > index_pos.y) )
{
process_scrollbox_click(click_x, click_y);
return true;
}
else
{
// Change this to be the x,y on our big pic so that caller can do imagemap stuff
evt.x += ( rect_pos.x * (-1 * index_to_big_multiple));
evt.y += ( rect_pos.y * (-1 * index_to_big_multiple));
return super.handleEvent(evt); //This passess the mouse click back up to Applet
}
}
else
{
return super.handleEvent(evt); //This passess the mouse click back up to Applet
}
}
public void process_scrollbox_click( int x, int y)
{
//Subtract half the zoom rectangles dimensions to place the rectangle at
//the center of the users click
rect_pos.x = x - disp_rect_center_x;
rect_pos.y = y - disp_rect_center_y;
if (rect_pos.x > max_rect_x) rect_pos.x = max_rect_x;
if (rect_pos.y > max_rect_y) rect_pos.y = max_rect_y;;
if (rect_pos.x < 0) rect_pos.x = 0;
if (rect_pos.y < 0) rect_pos.y = 0;
repaint();
}
}
//*******************************************************************************