001 /* 002 * $Id: AlphaPainter.java 3279 2009-03-01 12:01:11Z rah003 $ 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 023 package org.jdesktop.swingx.painter; 024 025 import java.awt.AlphaComposite; 026 import java.awt.Graphics2D; 027 028 /** 029 * Applies an alpha value to an entire stack of painters. 030 * 031 * @author joshy 032 */ 033 public class AlphaPainter<T> extends CompoundPainter<T> { 034 private float alpha = 1.0f; 035 036 /** 037 * {@inheritDoc} 038 */ 039 @Override 040 protected void doPaint(Graphics2D g, T component, int width, int height) { 041 Graphics2D g2 = (Graphics2D) g.create(); 042 043 try { 044 if(getTransform() != null) { 045 g2.setTransform(getTransform()); 046 } 047 if(alpha < 1) { 048 g2.setComposite(AlphaComposite.getInstance( 049 AlphaComposite.SRC_OVER, alpha)); 050 } 051 052 super.doPaint(g2, component, width, height); 053 } finally { 054 g2.dispose(); 055 } 056 } 057 /* 058 public static void main(String ... args) { 059 JXPanel panel = new JXPanel(); 060 AlphaPainter alpha = new AlphaPainter(); 061 alpha.setAlpha(1f); 062 alpha.setPainters(new PinstripePainter(new Color(255,255,255,125),45,20,20)); 063 064 panel.setBackgroundPainter(new CompoundPainter( 065 new MattePainter(Color.RED), 066 alpha 067 )); 068 069 JFrame frame = new JFrame(); 070 frame.add(panel); 071 frame.pack(); 072 frame.setSize(200,200); 073 frame.setVisible(true); 074 }*/ 075 076 /** 077 * Returns the current alpha value for this painter. This is the alpha value that will be applied 078 * to all painters set inside this painter. Alpha values will be multiplied. This means if you set an 079 * alpha of 0.5 on this painter and you nest a painter inside which uses an alpha of 0.5 then the final 080 * pixels drawn will have an alpha of 0.25. 081 * @return the current value of alpha property 082 */ 083 public float getAlpha() { 084 return alpha; 085 } 086 087 /** 088 * Sets the current alpha value for this painter. This is the alpha value that will be applied 089 * to all painters set inside this painter. Alpha values will be multiplied. This means if you set an 090 * alpha of 0.5 on this painter and you nest a painter inside which uses an alpha of 0.5 then the final 091 * pixels drawn will have an alpha of 0.25. 092 * @param alpha the new value of the alpha property 093 */ 094 public void setAlpha(float alpha) { 095 float old = getAlpha(); 096 this.alpha = alpha; 097 firePropertyChange("alpha", old, getAlpha()); 098 } 099 }