jdk/src/share/classes/javax/swing/GrayFilter.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-1998 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * An image filter that "disables" an image by turning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * it into a grayscale image, and brightening the pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * in the image. Used by buttons to create an image for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a disabled button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author      Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author      Tom Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class GrayFilter extends RGBImageFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private boolean brighter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int percent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Creates a disabled image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static Image createDisabledImage (Image i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        GrayFilter filter = new GrayFilter(true, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return grayImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Constructs a GrayFilter object that filters a color image to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * grayscale image. Used by buttons to create disabled ("grayed out")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * button images.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param b  a boolean -- true if the pixels should be brightened
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param p  an int in the range 0..100 that determines the percentage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *           of gray, where 100 is the darkest gray, and 0 is the lightest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public GrayFilter(boolean b, int p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        brighter = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        percent = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // canFilterIndexColorModel indicates whether or not it is acceptable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // to apply the color filtering of the filterRGB method to the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // table entries of an IndexColorModel object in lieu of pixel by pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        canFilterIndexColorModel = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Overrides <code>RGBImageFilter.filterRGB</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public int filterRGB(int x, int y, int rgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // Use NTSC conversion formula.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int gray = (int)((0.30 * ((rgb >> 16) & 0xff) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                         0.59 * ((rgb >> 8) & 0xff) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                         0.11 * (rgb & 0xff)) / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (brighter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            gray = (255 - ((255 - gray) * (100 - percent) / 100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            gray = (gray * (100 - percent) / 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (gray < 0) gray = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (gray > 255) gray = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | (gray << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
}