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

  1. // $Header: z:/admin/metro_examples/java/demo/ImageMap/rcs/RoundButtonFilter.java 1.1 1997/02/06 00:30:11 IPGIntel-2 Exp $ 
  2. /*
  3.  * @(#)RoundButtonFilter.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. /**
  33.  * An extensible ImageMap applet class.
  34.  * The active areas on the image are controlled by ImageArea classes
  35.  * that can be dynamically loaded over the net.
  36.  *
  37.  * @author     Jim Graham
  38.  * @version     1.6, 12/06/96
  39.  */
  40. class RoundButtonFilter extends ButtonFilter {
  41.     int Xcenter;
  42.     int Ycenter;
  43.     int Yradsq;
  44.     int innerW;
  45.     int innerH;
  46.     int Yrad2sq;
  47.  
  48.     public RoundButtonFilter(boolean press, int p, int b, int w, int h) {
  49.     super(press, p, b, w, h);
  50.     Xcenter = w/2;
  51.     Ycenter = h/2;
  52.     Yradsq = h * h / 4;
  53.     innerW = w - border * 2;
  54.     innerH = h - border * 2;
  55.     Yrad2sq = innerH * innerH / 4;
  56.     }
  57.  
  58.     public boolean inside(int x, int y) {
  59.     int yrel = Math.abs(Ycenter - y);
  60.     int xrel = (int) (Math.sqrt(Yradsq - yrel * yrel) * width / height);
  61.     return (x >= Xcenter - xrel && x < Xcenter + xrel);
  62.     }
  63.  
  64.     public void buttonRanges(int y, int ranges[]) {
  65.     int yrel = Math.abs(Ycenter - y);
  66.     int xrel = (int) (Math.sqrt(Yradsq - yrel * yrel) * width / height);
  67.     ranges[0] = 0;
  68.     ranges[1] = Xcenter - xrel;
  69.     ranges[6] = Xcenter + xrel;
  70.     ranges[7] = width;
  71.     ranges[8] = ranges[9] = y;
  72.     if (y < border) {
  73.         ranges[2] = ranges[3] = ranges[4] = Xcenter;
  74.         ranges[5] = ranges[6];
  75.     } else if (y + border >= height) {
  76.         ranges[2] = ranges[1];
  77.         ranges[3] = ranges[4] = ranges[5] = Xcenter;
  78.     } else {
  79.         int xrel2 = (int) (Math.sqrt(Yrad2sq - yrel * yrel)
  80.                    * innerW / innerH);
  81.         ranges[3] = Xcenter - xrel2;
  82.         ranges[4] = Xcenter + xrel2;
  83.         if (y < Ycenter) {
  84.         ranges[2] = ranges[3];
  85.         ranges[5] = ranges[6];
  86.         } else {
  87.         ranges[2] = ranges[1];
  88.         ranges[5] = ranges[4];
  89.         }
  90.     }
  91.     }
  92.  
  93.     public int filterRGB(int x, int y, int rgb) {
  94.     boolean brighter;
  95.     int percent;
  96.     int i;
  97.     int xrel, yrel;
  98.     int ranges[] = getRanges(y);
  99.     for (i = 0; i < 7; i++) {
  100.         if (x >= ranges[i] && x < ranges[i+1]) {
  101.         break;
  102.         }
  103.     }
  104.     switch (i) {
  105.     default:
  106.     case 0:
  107.     case 6:
  108.         return rgb & 0x00ffffff;
  109.     case 1:
  110.         brighter = !pressed;
  111.         percent = defpercent;
  112.         break;
  113.     case 5:
  114.         brighter = pressed;
  115.         percent = defpercent;
  116.         break;
  117.     case 2:
  118.         yrel = y - Ycenter;
  119.         xrel = Xcenter - x;
  120.         percent = (int) (yrel * defpercent * 2 /
  121.                  Math.sqrt(yrel * yrel + xrel * xrel))
  122.         - defpercent;
  123.         if (!pressed) {
  124.         percent = -percent;
  125.         }
  126.         if (percent == 0) {
  127.         return rgb;
  128.         } else if (percent < 0) {
  129.         percent = -percent;
  130.         brighter = false;
  131.         } else {
  132.         brighter = true;
  133.         }
  134.         break;
  135.     case 4:
  136.         yrel = Ycenter - y;
  137.         xrel = x - Xcenter;
  138.         percent = (int) (yrel * defpercent * 2 /
  139.                  Math.sqrt(yrel * yrel + xrel * xrel))
  140.         - defpercent;
  141.         if (pressed) {
  142.         percent = -percent;
  143.         }
  144.         if (percent == 0) {
  145.         return rgb;
  146.         } else if (percent < 0) {
  147.         percent = -percent;
  148.         brighter = false;
  149.         } else {
  150.         brighter = true;
  151.         }
  152.         break;
  153.     case 3:
  154.         if (!pressed) {
  155.         return rgb & 0x00ffffff;
  156.         }
  157.         brighter = false;
  158.         percent = defpercent;
  159.         break;
  160.     }
  161.     return filterRGB(rgb, brighter, percent);
  162.     }
  163. }
  164.