001/* 002 * $Id: GlossPainter.java 4147 2012-02-01 17:13:24Z kschaefe $ 003 * 004 * Copyright 2006 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 022package org.jdesktop.swingx.painter; 023 024import java.awt.Color; 025import java.awt.Graphics2D; 026import java.awt.Paint; 027import java.awt.Rectangle; 028import java.awt.geom.Area; 029import java.awt.geom.Ellipse2D; 030 031import org.jdesktop.beans.JavaBean; 032 033/** 034 * <p>A Painter implementation that simulates a gloss effect. The gloss can 035 * be positioned at the top or bottom of the drawing area. To fill the gloss, 036 * this painter uses a Paint instance which can be used to fill with a color 037 * (opaque or translucent), a texture, a gradient...</p> 038 * <p>The following example creates a white gloss at the top of the drawing 039 * area:</p> 040 * <pre> 041 * GlossPainter p = new GlossPainter(); 042 * p.setPaint(new Color(1.0f, 1.0f, 1.0f, 0.2f); 043 * p.setPosition(GlossPainter.GlossPosition.TOP); 044 * panel.setBackgroundPainter(p); 045 * </pre> 046 * <p>The values shown in this examples are the values used by default if 047 * they are not specified.</p> 048 * 049 * @author Romain Guy <romain.guy@mac.com> 050 */ 051@JavaBean 052@SuppressWarnings("nls") 053public class GlossPainter extends AbstractPainter<Object> { 054 /** 055 * <p>Used to define the position of the gloss on the painted area.</p> 056 */ 057 public enum GlossPosition { 058 TOP, BOTTOM 059 } 060 061 private Paint paint; 062 private GlossPosition position; 063 064 /** 065 * <p>Creates a new gloss painter positioned at the top of the painted 066 * area with a 20% translucent white color.</p> 067 */ 068 public GlossPainter() { 069 this(new Color(1.0f, 1.0f, 1.0f, 0.2f), GlossPosition.TOP); 070 } 071 072 /** 073 * <p>Creates a new gloss painter positioned at the top of the painted 074 * area with the specified paint.</p> 075 * 076 * @param paint The paint to be used when filling the gloss 077 */ 078 public GlossPainter(Paint paint) { 079 this(paint, GlossPosition.TOP); 080 } 081 082 /** 083 * <p>Creates a new gloss painter positioned at the specified position 084 * and using a white, 20% translucent paint.</p> 085 * 086 * @param position The position of the gloss on the painted area 087 */ 088 public GlossPainter(GlossPosition position) { 089 this(new Color(1.0f, 1.0f, 1.0f, 0.2f), position); 090 } 091 092 /** 093 * <p>Creates a new gloss painter positioned at the specified position 094 * and painted with the specified paint.</p> 095 * 096 * @param paint The paint to be used when filling the gloss 097 * @param position The position of the gloss on the painted area 098 */ 099 public GlossPainter(Paint paint, GlossPosition position) { 100 this.setPaint(paint); 101 this.setPosition(position); 102 } 103 104 /** 105 * {@inheritDoc} 106 */ 107 @Override 108 protected void doPaint(Graphics2D g, Object component, int width, int height) { 109 if (getPaint() != null) { 110 Ellipse2D ellipse = new Ellipse2D.Double(-width / 2.0, 111 height / 2.7, width * 2.0, 112 height * 2.0); 113 114 Area gloss = new Area(ellipse); 115 if (getPosition() == GlossPosition.TOP) { 116 Area area = new Area(new Rectangle(0, 0, 117 width, height)); 118 area.subtract(new Area(ellipse)); 119 gloss = area; 120 } 121 /* 122 if(getClip() != null) { 123 gloss.intersect(new Area(getClip())); 124 }*/ 125 g.setPaint(getPaint()); 126 g.fill(gloss); 127 } 128 } 129 130 /** 131 * <p>Returns the paint currently used by the painter to fill the gloss.</p> 132 * 133 * @return the current Paint instance used by the painter 134 */ 135 public Paint getPaint() { 136 return paint; 137 } 138 139 /** 140 * <p>Changes the paint to be used to fill the gloss. When the specified 141 * paint is null, nothing is painted. A paint can be an instance of 142 * Color.</p> 143 * 144 * @param paint The Paint instance to be used to fill the gloss 145 */ 146 public void setPaint(Paint paint) { 147 Paint old = this.paint; 148 this.paint = paint; 149 setDirty(true); 150 firePropertyChange("paint", old, getPaint()); 151 } 152 153 /** 154 * <p>Returns the position at which the gloss is painted.</p> 155 * 156 * @return the position of the gloss in the painted area 157 */ 158 public GlossPosition getPosition() { 159 return position; 160 } 161 162 /** 163 * <p>Changes the position of the gloss in the painted area. Only the 164 * values defined in the GlossPosition enum are valid.</p> 165 * 166 * @param position The position at which the gloss is painted 167 */ 168 public void setPosition(GlossPosition position) { 169 GlossPosition old = this.position; 170 this.position = position; 171 setDirty(true); 172 firePropertyChange("position", old, getPosition()); 173 } 174}