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