001: /*
002: * @(#)ColorDemo.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis.demos;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import basis.Builder;
031:
032: public class ColorDemo extends Demo implements MouseListener,
033: MouseMotionListener {
034: private static Color[] rgb;
035: private static Color[] bw;
036: private static Color[] colors;
037: private static Color[] shades;
038: private static Color[] alphas;
039: private static Color[] systemColors;
040: private static String[] systemColorNames;
041: private String oldStatus;
042:
043: public ColorDemo() {
044: addMouseListener(this );
045: addMouseMotionListener(this );
046: }
047:
048: public void paint(Graphics g) {
049: Dimension d = getSize();
050: g.setColor(Color.black);
051: for (int y = 2; y < d.height - 3; y += 4) {
052: g.drawLine(0, y, d.width, y);
053: }
054: draw(rgb, 0);
055: draw(bw, 1);
056: draw(colors, 2);
057: draw(shades, 3);
058: draw(alphas, 4);
059: draw(systemColors, 5);
060: }
061:
062: private void draw(Color[] array, int offset) {
063: Graphics g = getGraphics();
064: Dimension d = getSize();
065: int h = d.height / 6;
066: int x = 0;
067: int y = offset * h;
068: if (d.width < array.length) {
069: for (x = 0; x < d.width; x++) {
070: int index = x * array.length / d.width;
071: g.setColor(array[index]);
072: g.drawLine(x, y, x, y + h);
073: }
074: } else {
075: int w = d.width / array.length;
076: int r = d.width % array.length;
077: for (int i = 0; i < array.length; i++) {
078: int fill = r-- > 0 ? 1 : 0;
079: g.setColor(array[i]);
080: g.fillRect(x, y, w + fill, h);
081: x += w + fill;
082: }
083: }
084: }
085:
086: public void mouseClicked(MouseEvent e) {
087: showSystemColorName(e.getX(), e.getY());
088: }
089:
090: public void mouseEntered(MouseEvent e) {
091: if (oldStatus == null) {
092: oldStatus = getStatus();
093: }
094: }
095:
096: public void mouseExited(MouseEvent e) {
097: setStatus(oldStatus);
098: }
099:
100: public void mousePressed(MouseEvent e) {
101: showSystemColorName(e.getX(), e.getY());
102: }
103:
104: public void mouseReleased(MouseEvent e) {
105: }
106:
107: public void mouseDragged(MouseEvent e) {
108: showSystemColorName(e.getX(), e.getY());
109: }
110:
111: public void mouseMoved(MouseEvent e) {
112: showSystemColorName(e.getX(), e.getY());
113: }
114:
115: private void showSystemColorName(int x, int y) {
116: Dimension d = getSize();
117: int w = d.width - 1;
118: int h = d.height - 1;
119: if (x >= 0 && x < w && y > (5 * h) / 6 && y < h) {
120: int i = x * systemColorNames.length / w;
121: setStatus(systemColorNames[i]);
122: } else {
123: setStatus(oldStatus);
124: }
125: }
126:
127: static {
128: rgb = new Color[1000];
129: int[] array = new int[rgb.length];
130: int sixth = rgb.length / 6;
131: for (int i = 0; i < array.length; i++) {
132: if (i <= 2 * sixth) {
133: array[i] = 0;
134: } else if (i > 2 * sixth && i < 3 * sixth) {
135: array[i] = (i - 2 * sixth) * 255 / sixth;
136: } else if (i >= 3 * sixth && i <= 5 * sixth) {
137: array[i] = 255;
138: } else if (i > 5 * sixth && i < 6 * sixth) {
139: array[i] = 255 - (i - 5 * sixth) * 255 / sixth;
140: } else if (i >= 6 * sixth) {
141: array[i] = 0;
142: }
143: }
144: for (int i = 0; i < rgb.length; i++) {
145: int r = array[(i + 4 * sixth) % rgb.length];
146: int g = array[(i + 2 * sixth) % rgb.length];
147: int b = array[i];
148: rgb[i] = new Color(r, g, b);
149: }
150: bw = new Color[256];
151: for (int i = 0; i < bw.length; i++) {
152: int x = i * 255 / bw.length;
153: bw[i] = new Color(x, x, x);
154: }
155: colors = new Color[] { Color.red, Color.green, Color.blue,
156: Color.cyan, Color.magenta, Color.yellow, Color.orange,
157: Color.pink, Color.black, Color.darkGray, Color.gray,
158: Color.lightGray, Color.white };
159: shades = new Color[] { new Color(0xAA0000).darker(),
160: new Color(0xAA0000), new Color(0xAA0000).brighter(),
161: new Color(0x00AA00).darker(), new Color(0x00AA00),
162: new Color(0x00AA00).brighter(),
163: new Color(0x0000AA).darker(), new Color(0x0000AA),
164: new Color(0x0000AA).brighter(),
165: new Color(0xAAAAAA).darker(), new Color(0xAAAAAA),
166: new Color(0xAAAAAA).brighter() };
167: alphas = new Color[] { new Color(1.0f, 0.0f, 0.0f, 0.0f),
168: new Color(1.0f, 0.0f, 0.0f, 0.1f),
169: new Color(1.0f, 0.0f, 0.0f, 0.2f),
170: new Color(1.0f, 0.0f, 0.0f, 0.3f),
171: new Color(1.0f, 0.0f, 0.0f, 0.4f),
172: new Color(1.0f, 0.0f, 0.0f, 0.5f),
173: new Color(1.0f, 0.0f, 0.0f, 0.6f),
174: new Color(1.0f, 0.0f, 0.0f, 0.7f),
175: new Color(1.0f, 0.0f, 0.0f, 0.8f),
176: new Color(1.0f, 0.0f, 0.0f, 0.9f),
177: new Color(1.0f, 0.0f, 0.0f, 1.0f),
178: new Color(0.0f, 1.0f, 0.0f, 0.0f),
179: new Color(0.0f, 1.0f, 0.0f, 0.1f),
180: new Color(0.0f, 1.0f, 0.0f, 0.2f),
181: new Color(0.0f, 1.0f, 0.0f, 0.3f),
182: new Color(0.0f, 1.0f, 0.0f, 0.4f),
183: new Color(0.0f, 1.0f, 0.0f, 0.5f),
184: new Color(0.0f, 1.0f, 0.0f, 0.6f),
185: new Color(0.0f, 1.0f, 0.0f, 0.7f),
186: new Color(0.0f, 1.0f, 0.0f, 0.8f),
187: new Color(0.0f, 1.0f, 0.0f, 0.9f),
188: new Color(0.0f, 1.0f, 0.0f, 1.0f),
189: new Color(0.0f, 0.0f, 1.0f, 0.0f),
190: new Color(0.0f, 0.0f, 1.0f, 0.1f),
191: new Color(0.0f, 0.0f, 1.0f, 0.2f),
192: new Color(0.0f, 0.0f, 1.0f, 0.3f),
193: new Color(0.0f, 0.0f, 1.0f, 0.4f),
194: new Color(0.0f, 0.0f, 1.0f, 0.5f),
195: new Color(0.0f, 0.0f, 1.0f, 0.6f),
196: new Color(0.0f, 0.0f, 1.0f, 0.7f),
197: new Color(0.0f, 0.0f, 1.0f, 0.8f),
198: new Color(0.0f, 0.0f, 1.0f, 0.9f),
199: new Color(0.0f, 0.0f, 1.0f, 1.0f),
200: new Color(0.0f, 0.0f, 0.0f, 0.0f),
201: new Color(0.0f, 0.0f, 0.0f, 0.1f),
202: new Color(0.0f, 0.0f, 0.0f, 0.2f),
203: new Color(0.0f, 0.0f, 0.0f, 0.3f),
204: new Color(0.0f, 0.0f, 0.0f, 0.4f),
205: new Color(0.0f, 0.0f, 0.0f, 0.5f),
206: new Color(0.0f, 0.0f, 0.0f, 0.6f),
207: new Color(0.0f, 0.0f, 0.0f, 0.7f),
208: new Color(0.0f, 0.0f, 0.0f, 0.8f),
209: new Color(0.0f, 0.0f, 0.0f, 0.9f),
210: new Color(0.0f, 0.0f, 0.0f, 1.0f),
211: new Color(0.5f, 0.5f, 0.5f, 0.0f),
212: new Color(0.5f, 0.5f, 0.5f, 0.1f),
213: new Color(0.5f, 0.5f, 0.5f, 0.2f),
214: new Color(0.5f, 0.5f, 0.5f, 0.3f),
215: new Color(0.5f, 0.5f, 0.5f, 0.4f),
216: new Color(0.5f, 0.5f, 0.5f, 0.5f),
217: new Color(0.5f, 0.5f, 0.5f, 0.6f),
218: new Color(0.5f, 0.5f, 0.5f, 0.7f),
219: new Color(0.5f, 0.5f, 0.5f, 0.8f),
220: new Color(0.5f, 0.5f, 0.5f, 0.9f),
221: new Color(0.5f, 0.5f, 0.5f, 1.0f),
222: new Color(1.0f, 1.0f, 1.0f, 0.0f),
223: new Color(1.0f, 1.0f, 1.0f, 0.1f),
224: new Color(1.0f, 1.0f, 1.0f, 0.2f),
225: new Color(1.0f, 1.0f, 1.0f, 0.3f),
226: new Color(1.0f, 1.0f, 1.0f, 0.4f),
227: new Color(1.0f, 1.0f, 1.0f, 0.5f),
228: new Color(1.0f, 1.0f, 1.0f, 0.6f),
229: new Color(1.0f, 1.0f, 1.0f, 0.7f),
230: new Color(1.0f, 1.0f, 1.0f, 0.8f),
231: new Color(1.0f, 1.0f, 1.0f, 0.9f),
232: new Color(1.0f, 1.0f, 1.0f, 1.0f) };
233:
234: systemColors = new Color[] { SystemColor.activeCaption,
235: SystemColor.activeCaptionBorder,
236: SystemColor.activeCaptionText, SystemColor.control,
237: SystemColor.controlDkShadow,
238: SystemColor.controlHighlight,
239: SystemColor.controlShadow, SystemColor.controlText,
240: SystemColor.desktop, SystemColor.inactiveCaption,
241: SystemColor.inactiveCaptionBorder,
242: SystemColor.inactiveCaptionText, SystemColor.info,
243: SystemColor.infoText, SystemColor.menu,
244: SystemColor.menuText, SystemColor.scrollbar,
245: SystemColor.text, SystemColor.textHighlight,
246: SystemColor.textHighlightText,
247: SystemColor.textInactiveText, SystemColor.textText,
248: SystemColor.window, SystemColor.windowBorder,
249: SystemColor.windowText };
250: systemColorNames = new String[] { "activeCaption",
251: "activeCaptionBorder", "activeCaptionText", "control",
252: "controlDkShadow", "controlHighlight", "controlShadow",
253: "controlText", "desktop", "inactiveCaption",
254: "inactiveCaptionBorder", "inactiveCaptionText", "info",
255: "infoText", "menu", "menuText", "scrollbar", "text",
256: "textHighlight", "textHighlightText",
257: "textInactiveText", "textText", "window",
258: "windowBorder", "windowText" };
259: }
260: }
|