KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RoundButtonFilter


1 /*
2  * @(#)RoundButtonFilter.java 1.15 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)RoundButtonFilter.java 1.15 05/11/17
39  */

40
41 /**
42  * An extensible ImageMap applet class.
43  * The active areas on the image are controlled by ImageArea classes
44  * that can be dynamically loaded over the net.
45  *
46  * @author Jim Graham
47  * @version 1.15, 11/17/05
48  */

49 class RoundButtonFilter extends ButtonFilter {
50     int Xcenter;
51     int Ycenter;
52     int Yradsq;
53     int innerW;
54     int innerH;
55     int Yrad2sq;
56
57     public RoundButtonFilter(boolean press, int p, int b, int w, int h) {
58     super(press, p, b, w, h);
59     Xcenter = w/2;
60     Ycenter = h/2;
61     Yradsq = h * h / 4;
62     innerW = w - border * 2;
63     innerH = h - border * 2;
64     Yrad2sq = innerH * innerH / 4;
65     }
66
67     public boolean inside(int x, int y) {
68     int yrel = Math.abs(Ycenter - y);
69     int xrel = (int) (Math.sqrt(Yradsq - yrel * yrel) * width / height);
70     return (x >= Xcenter - xrel && x < Xcenter + xrel);
71     }
72
73     public void buttonRanges(int y, int ranges[]) {
74     int yrel = Math.abs(Ycenter - y);
75     int xrel = (int) (Math.sqrt(Yradsq - yrel * yrel) * width / height);
76     ranges[0] = 0;
77     ranges[1] = Xcenter - xrel;
78     ranges[6] = Xcenter + xrel;
79     ranges[7] = width;
80     ranges[8] = ranges[9] = y;
81     if (y < border) {
82         ranges[2] = ranges[3] = ranges[4] = Xcenter;
83         ranges[5] = ranges[6];
84     } else if (y + border >= height) {
85         ranges[2] = ranges[1];
86         ranges[3] = ranges[4] = ranges[5] = Xcenter;
87     } else {
88         int xrel2 = (int) (Math.sqrt(Yrad2sq - yrel * yrel)
89                    * innerW / innerH);
90         ranges[3] = Xcenter - xrel2;
91         ranges[4] = Xcenter + xrel2;
92         if (y < Ycenter) {
93         ranges[2] = ranges[3];
94         ranges[5] = ranges[6];
95         } else {
96         ranges[2] = ranges[1];
97         ranges[5] = ranges[4];
98         }
99     }
100     }
101
102     public int filterRGB(int x, int y, int rgb) {
103     boolean brighter;
104     int percent;
105     int i;
106     int xrel, yrel;
107     int ranges[] = getRanges(y);
108     for (i = 0; i < 7; i++) {
109         if (x >= ranges[i] && x < ranges[i+1]) {
110         break;
111         }
112     }
113     switch (i) {
114     default:
115     case 0:
116     case 6:
117         return rgb & 0x00ffffff;
118     case 1:
119         brighter = !pressed;
120         percent = defpercent;
121         break;
122     case 5:
123         brighter = pressed;
124         percent = defpercent;
125         break;
126     case 2:
127         yrel = y - Ycenter;
128         xrel = Xcenter - x;
129         percent = (int) (yrel * defpercent * 2 /
130                  Math.sqrt(yrel * yrel + xrel * xrel))
131         - defpercent;
132         if (!pressed) {
133         percent = -percent;
134         }
135         if (percent == 0) {
136         return rgb;
137         } else if (percent < 0) {
138         percent = -percent;
139         brighter = false;
140         } else {
141         brighter = true;
142         }
143         break;
144     case 4:
145         yrel = Ycenter - y;
146         xrel = x - Xcenter;
147         percent = (int) (yrel * defpercent * 2 /
148                  Math.sqrt(yrel * yrel + xrel * xrel))
149         - defpercent;
150         if (pressed) {
151         percent = -percent;
152         }
153         if (percent == 0) {
154         return rgb;
155         } else if (percent < 0) {
156         percent = -percent;
157         brighter = false;
158         } else {
159         brighter = true;
160         }
161         break;
162     case 3:
163         if (!pressed) {
164         return rgb & 0x00ffffff;
165         }
166         brighter = false;
167         percent = defpercent;
168         break;
169     }
170     return filterRGB(rgb, brighter, percent);
171     }
172 }
173
Popular Tags