From f3af4018c313315cc0226189708c6f02f5bb5bf4 Mon Sep 17 00:00:00 2001 From: ray Date: Fri, 23 Apr 2004 16:46:37 +0000 Subject: [PATCH] A silly little class to draw icons with an alpha level. I was going to make this an anonymous inner class for something else, but I figured it could be useful somewhere else in the future... git-svn-id: https://samskivert.googlecode.com/svn/trunk@1416 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/swing/DimmedIcon.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/swing/DimmedIcon.java diff --git a/projects/samskivert/src/java/com/samskivert/swing/DimmedIcon.java b/projects/samskivert/src/java/com/samskivert/swing/DimmedIcon.java new file mode 100644 index 00000000..0defcc1c --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/swing/DimmedIcon.java @@ -0,0 +1,63 @@ +// +// $Id: DimmedIcon.java,v 1.1 2004/04/23 16:46:37 ray Exp $ + +package com.samskivert.swing; + +import java.awt.AlphaComposite; +import java.awt.Component; +import java.awt.Composite; +import java.awt.Graphics; +import java.awt.Graphics2D; + +import javax.swing.Icon; + +/** + * Draws an icon with a specified alpha level. + */ +public class DimmedIcon implements Icon +{ + /** + * Construct a dimmed icon that is drawn at 50% normal. + */ + public DimmedIcon (Icon icon) + { + this(icon, .5f); + } + + /** + * Construct a dimmed icon that is drawn at the specified alpha level. + */ + public DimmedIcon (Icon icon, float alpha) + { + _alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha); + _icon = icon; + } + + // documentation inherited from interface Icon + public int getIconWidth () + { + return _icon.getIconWidth(); + } + + // documentation inherited from interface Icon + public int getIconHeight () + { + return _icon.getIconHeight(); + } + + // documentation inherited from interface Icon + public void paintIcon (Component c, Graphics g, int x, int y) + { + Graphics2D gfx = (Graphics2D) g; + Composite ocomp = gfx.getComposite(); + gfx.setComposite(_alpha); + _icon.paintIcon(c, gfx, x, y); + gfx.setComposite(ocomp); + } + + /** The icon we're actually drawing. */ + protected Icon _icon; + + /** Our alpha composite. */ + protected Composite _alpha; +}