home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / ImageMapArea.java < prev    next >
Text File  |  1997-07-30  |  12KB  |  342 lines

  1. // $Header: z:/admin/metro_examples/java/demo/ImageMap/rcs/ImageMapArea.java 1.1 1997/02/06 00:30:10 IPGIntel-2 Exp $ 
  2. /*
  3.  * @(#)ImageMapArea.java    1.6 96/12/06
  4.  *
  5.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  8.  * modify and redistribute this software in source and binary code form,
  9.  * provided that i) this copyright notice and license appear on all copies of
  10.  * the software; and ii) Licensee does not utilize the software in a manner
  11.  * which is disparaging to Sun.
  12.  *
  13.  * This software is provided "AS IS," without a warranty of any kind. ALL
  14.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  15.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  16.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  17.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  18.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  19.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  20.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  21.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  22.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  23.  * POSSIBILITY OF SUCH DAMAGES.
  24.  *
  25.  * This software is not designed or intended for use in on-line control of
  26.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  27.  * the design, construction, operation or maintenance of any nuclear
  28.  * facility. Licensee represents and warrants that it will not use or
  29.  * redistribute the Software for such purposes.
  30.  */
  31.  
  32. import java.awt.Graphics;
  33. import java.awt.Image;
  34. import java.awt.image.*;
  35. import java.util.StringTokenizer;
  36. import java.net.URL;
  37. import java.net.MalformedURLException;
  38.  
  39. /**
  40.  * The base ImageArea class.
  41.  * This class performs the basic functions that most ImageArea
  42.  * classes will need and delegates specific actions to the subclasses.
  43.  *
  44.  * @author     Jim Graham
  45.  * @version     1.6, 12/06/96
  46.  */
  47. class ImageMapArea implements ImageObserver {
  48.     /** The applet parent that contains this ImageArea. */
  49.     ImageMap parent;
  50.     /** The X location of the area (if rectangular). */
  51.     int X;
  52.     /** The Y location of the area (if rectangular). */
  53.     int Y;
  54.     /** The size().width of the area (if rectangular). */
  55.     int W;
  56.     /** The size().height of the area (if rectangular). */
  57.     int H;
  58.     /**
  59.      * This flag indicates whether the user was in this area during the
  60.      * last scan of mouse locations.
  61.      */
  62.     boolean entered = false;
  63.     /** This flag indicates whether the area is currently highlighted. */
  64.     boolean active = false;
  65.  
  66.     /**
  67.      * This is the default highlight image if no special effects are
  68.      * needed to draw the highlighted image.  It is created by the
  69.      * default "makeImages()" method.
  70.      */
  71.     Image hlImage;
  72.  
  73.     /**
  74.      * This is the status string requested by this area.  Only the
  75.      * status string from the topmost area which has requested one
  76.      * will be displayed.
  77.      */
  78.     String status;
  79.  
  80.     /**
  81.      * Initialize this ImageArea as called from the applet.
  82.      * If the subclass does not override this initializer, then it
  83.      * will perform the basic functions of setting the parent applet
  84.      * and parsing out 4 numbers from the argument string which specify
  85.      * a rectangular region for the ImageArea to act on.
  86.      * The remainder of the argument string is passed to the handleArg()
  87.      * method for more specific handling by the subclass.
  88.      */
  89.     public void init(ImageMap parent, String args) {
  90.     this.parent = parent;
  91.     StringTokenizer st = new StringTokenizer(args, ", ");
  92.     X = Integer.parseInt(st.nextToken());
  93.     Y = Integer.parseInt(st.nextToken());
  94.     W = Integer.parseInt(st.nextToken());
  95.     H = Integer.parseInt(st.nextToken());
  96.     if (st.hasMoreTokens()) {
  97.         // hasMoreTokens() Skips the trailing comma
  98.         handleArg(st.nextToken(""));
  99.     } else {
  100.         handleArg(null);
  101.     }
  102.     makeImages();
  103.     }
  104.  
  105.     /**
  106.      * This method handles the remainder of the argument string after
  107.      * the standard initializer has parsed off the 4 rectangular
  108.      * parameters.  If the subclass does not override this method,
  109.      * the remainder will be ignored.
  110.      */
  111.     public void handleArg(String s) {
  112.     }
  113.  
  114.     /**
  115.      * This method loads any additional media that the ImageMapArea
  116.      * may need for its animations.
  117.      */
  118.     public void getMedia() {
  119.     }
  120.  
  121.     /**
  122.      * This method is called every animation cycle if there are any
  123.      * active animating areas.
  124.      * @return true if this area requires further animation notifications
  125.      */
  126.     public boolean animate() {
  127.     return false;
  128.     }
  129.  
  130.     /**
  131.      * This method sets the image to be used to render the ImageArea
  132.      * when it is highlighted.
  133.      */
  134.     public void setHighlight(Image img) {
  135.     hlImage = img;
  136.     }
  137.  
  138.     /**
  139.      * This method handles the construction of the various images
  140.      * used to highlight this particular ImageArea when the user
  141.      * interacts with it.
  142.      */
  143.     public void makeImages() {
  144.     setHighlight(parent.getHighlight(X, Y, W, H));
  145.     }
  146.  
  147.     /**
  148.      * The repaint method causes the area to be repainted at the next
  149.      * opportunity.
  150.      */
  151.     public void repaint() {
  152.     parent.repaint(0, X, Y, W, H);
  153.     }
  154.  
  155.     /**
  156.      * This method tests to see if a point is inside this ImageArea.
  157.      * The standard method assumes a rectangular area as parsed by
  158.      * the standard initializer.  If a more complex area is required
  159.      * then this method will have to be overridden by the subclass.
  160.      */
  161.     public boolean inside(int x, int y) {
  162.     return (x >= X && x < (X + W) && y >= Y && y < (Y + H));
  163.     }
  164.  
  165.     /**
  166.      * This utility method draws a rectangular subset of a highlight
  167.      * image.
  168.      */
  169.     public void drawImage(Graphics g, Image img, int imgx, int imgy,
  170.               int x, int y, int w, int h) {
  171.     Graphics ng = g.create();
  172.     ng.clipRect(x, y, w, h);
  173.     ng.drawImage(img, imgx, imgy, this);
  174.     }
  175.  
  176.     /**
  177.      * This method handles the updates from drawing the images.
  178.      */
  179.     public boolean imageUpdate(Image img, int infoflags,
  180.                    int x, int y, int width, int height) {
  181.     if (img == hlImage) {
  182.         return parent.imageUpdate(img, infoflags, x + X, y + Y,
  183.                       width, height);
  184.     } else {
  185.         return (infoflags & (ALLBITS | ERROR)) == 0;
  186.     }
  187.     }
  188.  
  189.     /**
  190.      * This utility method records a string to be shown in the status bar.
  191.      */
  192.     public void showStatus(String msg) {
  193.     status = msg;
  194.     parent.newStatus();
  195.     }
  196.  
  197.     /**
  198.      * This utility method returns the status string this area wants to
  199.      * put into the status bar.  If no previous area (higher in the
  200.      * stacking order) has yet returned a status message, prevmsg will
  201.      * be null and this area will then return its own message, otherwise
  202.      * it will leave the present message alone.
  203.      */
  204.     public String getStatus(String prevmsg) {
  205.     return (prevmsg == null) ? status : prevmsg;
  206.     }
  207.  
  208.     /**
  209.      * This utility method tells the browser to visit a URL.
  210.      */
  211.     public void showDocument(URL u) {
  212.     parent.getAppletContext().showDocument(u);
  213.     }
  214.  
  215.     /**
  216.      * This method highlights the specified area when the user enters
  217.      * it with his mouse.  The standard highlight method is to replace
  218.      * the indicated rectangular area of the image with the primary
  219.      * highlighted image.
  220.      */
  221.     public void highlight(Graphics g) {
  222.     }
  223.  
  224.     /**
  225.      * The checkEnter method is called when the mouse is inside the
  226.      * region to see if the area needs to have its enter method called.
  227.      * The default implementation simply checks if the entered flag is
  228.      * set and only calls enter if it is false.
  229.      */
  230.     public boolean checkEnter(int x, int y) {
  231.     if (!entered) {
  232.         entered = true;
  233.         enter(x, y);
  234.     }
  235.     return isTerminal();
  236.     }
  237.  
  238.     /**
  239.      * The checkExit method is called when the mouse is outside the
  240.      * region to see if the area needs to have its exit method called.
  241.      * The default implementation simply checks if the entered flag is
  242.      * set and only calls exit if it is true.
  243.      */
  244.     public void checkExit() {
  245.     if (entered) {
  246.         entered = false;
  247.         exit();
  248.     }
  249.     }
  250.  
  251.     /**
  252.      * The isTerminal method controls whether events propagate to the
  253.      * areas which lie beneath this one.
  254.      * @return true if the events should be propagated to the underlying
  255.      * areas.
  256.      */
  257.     public boolean isTerminal() {
  258.     return false;
  259.     }
  260.  
  261.     /**
  262.      * The enter method is called when the mouse enters the region.
  263.      * The location is supplied, but the standard implementation is
  264.      * to call the overloaded method with no arguments.
  265.      */
  266.     public void enter(int x, int y) {
  267.     enter();
  268.     }
  269.  
  270.     /**
  271.      * The overloaded enter method is called when the mouse enters
  272.      * the region.  This method can be overridden if the ImageArea
  273.      * does not need to know where the mouse entered.
  274.      */
  275.     public void enter() {
  276.     }
  277.  
  278.     /**
  279.      * The exit method is called when the mouse leaves the region.
  280.      */
  281.     public void exit() {
  282.     }
  283.  
  284.     /**
  285.      * The press method is called when the user presses the mouse
  286.      * button inside the ImageArea.  The location is supplied, but
  287.      * the standard implementation is to call the overloaded method
  288.      * with no arguments.
  289.      * @return true if this ImageMapArea wants to prevent any underlying
  290.      * areas from seeing the press
  291.      */
  292.     public boolean press(int x, int y) {
  293.     return press();
  294.     }
  295.  
  296.     /**
  297.      * The overloaded press method is called when the user presses the
  298.      * mouse button inside the ImageArea.  This method can be overridden
  299.      * if the ImageArea does not need to know the location of the press.
  300.      * @return true if this ImageMapArea wants to prevent any underlying
  301.      * areas from seeing the press
  302.      */
  303.     public boolean press() {
  304.     return isTerminal();
  305.     }
  306.  
  307.     /**
  308.      * The lift method is called when the user releases the mouse button.
  309.      * The location is supplied, but the standard implementation is to
  310.      * call the overloaded method with no arguments.  Only those ImageAreas
  311.      * that were informed of a press will be informed of the corresponding
  312.      * release.
  313.      * @return true if this ImageMapArea wants to prevent any underlying
  314.      * areas from seeing the lift
  315.      */
  316.     public boolean lift(int x, int y) {
  317.     return lift();
  318.     }
  319.  
  320.     /**
  321.      * The overloaded lift method is called when the user releases the
  322.      * mouse button.  This method can be overridden if the ImageArea
  323.      * does not need to know the location of the release.
  324.      * @return true if this ImageMapArea wants to prevent any underlying
  325.      * areas from seeing the lift
  326.      */
  327.     public boolean lift() {
  328.     return isTerminal();
  329.     }
  330.  
  331.     /**
  332.      * The drag method is called when the user moves the mouse while
  333.      * the button is pressed.  Only those ImageAreas that were informed
  334.      * of a press will be informed of the corresponding mouse movements.
  335.      * @return true if this ImageMapArea wants to prevent any underlying
  336.      * areas from seeing the drag
  337.      */
  338.     public boolean drag(int x, int y) {
  339.     return isTerminal();
  340.     }
  341. }
  342.