001/*
002 * $Id: AlphaPainter.java 4147 2012-02-01 17:13:24Z kschaefe $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021
022
023package org.jdesktop.swingx.painter;
024
025import java.awt.AlphaComposite;
026import java.awt.Graphics2D;
027
028import org.jdesktop.beans.JavaBean;
029
030/**
031 * Applies an alpha value to an entire stack of painters.
032 *
033 * @author joshy
034 */
035@JavaBean
036@SuppressWarnings("nls")
037public class AlphaPainter<T> extends CompoundPainter<T> {
038    private float alpha = 1.0f;
039
040    /**
041     * {@inheritDoc}
042     */
043    @Override
044    protected void doPaint(Graphics2D g, T component, int width, int height) {
045        Graphics2D g2 = (Graphics2D) g.create();
046        
047        try {
048        if(getTransform() != null) {
049            g2.setTransform(getTransform());
050        }
051        if(alpha < 1) {
052                g2.setComposite(AlphaComposite.getInstance(
053                        AlphaComposite.SRC_OVER, alpha));
054        }
055            
056            super.doPaint(g2, component, width, height);
057        } finally {
058            g2.dispose();
059        }
060    }
061    /*
062    public static void main(String ... args) {
063        JXPanel panel = new JXPanel();
064        AlphaPainter alpha = new AlphaPainter();
065        alpha.setAlpha(1f);
066        alpha.setPainters(new PinstripePainter(new Color(255,255,255,125),45,20,20));
067        
068        panel.setBackgroundPainter(new CompoundPainter(
069                new MattePainter(Color.RED),
070                alpha
071                ));
072        
073        JFrame frame = new JFrame();
074        frame.add(panel);
075        frame.pack();
076        frame.setSize(200,200);
077        frame.setVisible(true);
078    }*/
079
080    /**
081     * Returns the current alpha value for this painter. This is the alpha value that will be applied 
082     * to all painters set inside this painter. Alpha values will be multiplied. This means if you set an
083     * alpha of 0.5 on this painter and you nest a painter inside which uses an alpha of 0.5 then the final
084     * pixels drawn will have an alpha of 0.25.
085     * @return the current value of alpha property
086     */
087    public float getAlpha() {
088        return alpha;
089    }
090
091    /**
092     * Sets the current alpha value for this painter. This is the alpha value that will be applied 
093     * to all painters set inside this painter. Alpha values will be multiplied. This means if you set an
094     * alpha of 0.5 on this painter and you nest a painter inside which uses an alpha of 0.5 then the final
095     * pixels drawn will have an alpha of 0.25.
096     * @param alpha the new value of the alpha property
097     */
098    public void setAlpha(float alpha) {
099        float old = getAlpha();
100        this.alpha = alpha;
101        firePropertyChange("alpha", old, getAlpha());
102    }
103}