/*******************************************************************************
 * Copyright (c) 2010 Haifeng Li
 *   
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package smile.swing;

import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.Icon;

/**
 * An Icon wrapper that paints the contained icon with a specified transparency.
 * 

* Note: This class is not suitable for wrapping an * ImageIcon that holds an animated image. */ public class AlphaIcon implements Icon { private Icon icon; private float alpha; /** * Creates an * AlphaIcon with the specified icon and opacity. The opacity * alpha should be in the range 0.0F (fully transparent) to * 1.0F (fully opaque). * * @param icon the Icon to wrap * @param alpha the opacity */ public AlphaIcon(Icon icon, float alpha) { this.icon = icon; this.alpha = alpha; } /** * Gets this * AlphaIcon's opacity * * @return the opacity, in the range 0.0 to 1.0 */ public float getAlpha() { return alpha; } /** * Gets the icon wrapped by this * AlphaIcon * * @return the wrapped icon */ public Icon getIcon() { return icon; } /** * Paints the wrapped icon with this * AlphaIcon's transparency. * * @param c The component to which the icon is painted * @param g the graphics context * @param x the X coordinate of the icon's top-left corner * @param y the Y coordinate of the icon's top-left corner */ @Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g.create(); g2.setComposite(AlphaComposite.SrcAtop.derive(alpha)); icon.paintIcon(c, g2, x, y); g2.dispose(); } /** * Gets the width of the bounding rectangle of this * AlphaIcon. Returns the width of the wrapped icon. * * @return the width in pixels */ @Override public int getIconWidth() { return icon.getIconWidth(); } /** * Gets the height of the bounding rectangle of this * AlphaIcon. * Returns the height of the wrapped icon. * * @return the height in pixels */ @Override public int getIconHeight() { return icon.getIconHeight(); } }

Ads help maintain this website.