home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / ImageMap / ImageMapArea.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  11.4 KB  |  358 lines

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