/* * @file: TranslateTest.java * @source: David Geary [Graphic Java, p.57] * * @purpose: * This class TranslateTest is an applet that reads an image, * and display the image in its natural size in a limited size * window. Using the mouse, you can "pan" the image, i.e., * by dragging the mouse, the image translates in the window. * * Several exercises for extending this example are given below. * * @history: * Adapted by Chee Yap for * Visualization Class, Spring 2003 * */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class TranslateTest extends Applet { Image image; Point pressed = new Point(), lastTranslate = new Point(); public void init() { //////////////////////////////////////////////////////////////// // Load Image: //////////////////////////////////////////////////////////////// image = getImage(getCodeBase(), "saint.gif"); // getCodeBase() is current path try { MediaTracker mt = new MediaTracker(this); // make *this* a MediaTracker mt.addImage(image, 0); // image has ID=0 mt.waitForID(0); // wait till ID 0 load } catch(InterruptedException e) { e.printStackTrace(); } //////////////////////////////////////////////////////////////// // Mouse Listener: //////////////////////////////////////////////////////////////// addMouseListener(new MouseAdapter() { // get new (anonymous) MouseAdapter & public void mousePressed(MouseEvent e) { // register it with "addMouseListener" Point loc = e.getPoint(); pressed.x = loc.x - lastTranslate.x; // adjust absolute position of "loc" pressed.y = loc.y - lastTranslate.y; // using the "lastTranslate" point. } }); //////////////////////////////////////////////////////////////// // Mouse Motion Listener: //////////////////////////////////////////////////////////////// addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Point loc = e.getPoint(); Point translate = new Point(loc.x - pressed.x, // NOTE: pressed point is unchanged loc.y - pressed.y); // during dragging Graphics g = getGraphics(); try { g.clearRect(0,0, getSize().width,getSize().height); g.translate(translate.x, translate.y); showStatus("Translating Graphics: " + translate); g.drawImage(image, 0, 0, TranslateTest.this); // *this* is the ImageObserver } finally { // The third clause in "try-catch-finally". g.dispose(); // It is executed whether or not the exception } // is caught. lastTranslate = translate; } }); } //////////////////////////////////////////////////////////////// // paint method //////////////////////////////////////////////////////////////// public void paint(Graphics g) { g.drawImage(image, 0, 0, this); // image is the loaded image // *this* (i.e., TranslateTest) is // the ImageObserver argument. } } // TranslateTest class /************************************************************ EXERCISE 1: Merge the present program (TranslateTest.java) with the previous program (PanelTest2.java). Call the result ZoomPan.java. In particular, the current image translation demo should be placed in the centerPanel of a workPanel. EXERCISE 2: Introduce into ZoomPan.java the ability to zoom. Add two buttons named "+" and "-" into the controlPanel. Clicking these will double or half the image size. EXERCISE 3: It is easy to get lost when you pan and zoom. Please introduce a "navigator" component. This can be a Canvas component that has a fixed size (about 70x70 may be good). In this navigator, you draw a PINK rectangle, representing a scaled outline of the full extent of the image. Inside this pink rectangle, you draw a BLUE rectangle, representing a scaled outline of your current centerPanel. EXERCISE 4: In good GUI design, we know that it is important to provide feedback to the user. We want the following visual feedback: when the mouse button is pressed, the cursor changes into an "open hand" image. This cursor returns to the default ("arrow") when the mouse is released. ************************************************************/