001 /* 002 * $Id: GlossPainter.java,v 1.6 2006/04/11 01:46:00 joshy Exp $ 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.RenderingHints; 029 import java.awt.Shape; 030 import java.awt.geom.Area; 031 import java.awt.geom.Ellipse2D; 032 import javax.swing.JComponent; 033 034 /** 035 * <p>A Painter implementation that simulates a gloss effect. The gloss can 036 * be positionned at the top or bottom of the drawing area. To fill the gloss, 037 * this painter uses a Paint instance which can be used to fill with a color 038 * (opaque or translucent), a texture, a gradient...</p> 039 * <p>The following example creates a white gloss at the top of the drawing 040 * area:</p> 041 * <pre> 042 * GlossPainter p = new GlossPainter(); 043 * p.setPaint(new Color(1.0f, 1.0f, 1.0f, 0.2f); 044 * p.setPosition(GlossPainter.GlossPosition.TOP); 045 * panel.setBackgroundPainter(p); 046 * </pre> 047 * <p>The values shown in this examples are the values used by default if 048 * they are not specified.</p> 049 * 050 * @author Romain Guy <romain.guy@mac.com> 051 */ 052 public class GlossPainter extends AbstractPainter { 053 /** 054 * <p>Used to define the position of the gloss on the painted area.</p> 055 */ 056 public enum GlossPosition { 057 TOP, BOTTOM 058 } 059 060 private Paint paint; 061 private GlossPosition position; 062 063 /** 064 * <p>Creates a new gloss painter positionned at the top of the painted 065 * area with a 20% translucent white color.</p> 066 */ 067 public GlossPainter() { 068 this(new Color(1.0f, 1.0f, 1.0f, 0.2f), GlossPosition.TOP); 069 } 070 071 /** 072 * <p>Creates a new gloss painter positionned at the top of the painted 073 * area with the specified paint.</p> 074 * 075 * @param paint The paint to be used when filling the gloss 076 */ 077 public GlossPainter(Paint paint) { 078 this(paint, GlossPosition.TOP); 079 } 080 081 /** 082 * <p>Creates a new gloss painter positionned at the specified position 083 * and using a white, 20% translucent paint.</p> 084 * 085 * @param position The position of the gloss on the painted area 086 */ 087 public GlossPainter(GlossPosition position) { 088 this(new Color(1.0f, 1.0f, 1.0f, 0.2f), position); 089 } 090 091 /** 092 * <p>Creates a new gloss painter positionned at the specified position 093 * and painted with the specified paint.</p> 094 * 095 * @param paint The paint to be used when filling the gloss 096 * @param position The position of the gloss on the painted area 097 */ 098 public GlossPainter(Paint paint, GlossPosition position) { 099 this.setPaint(paint); 100 this.setPosition(position); 101 102 setRenderingHint(RenderingHints.KEY_ANTIALIASING, 103 RenderingHints.VALUE_ANTIALIAS_ON); 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 protected void paintBackground(Graphics2D g, JComponent component) { 110 if (getPaint() != null) { 111 Ellipse2D ellipse = new Ellipse2D.Double(-component.getWidth() / 2.0, 112 component.getHeight() / 2.7, component.getWidth() * 2.0, 113 component.getHeight() * 2.0); 114 115 Area gloss = new Area(ellipse); 116 if (getPosition() == GlossPosition.TOP) { 117 Area area = new Area(new Rectangle(0, 0, 118 component.getWidth(), component.getHeight())); 119 area.subtract(new Area(ellipse)); 120 gloss = area; 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 firePropertyChange("paint", old, getPaint()); 150 } 151 152 /** 153 * <p>Returns the position at which the gloss is painted.</p> 154 * 155 * @return the position of the gloss in the painted area 156 */ 157 public GlossPosition getPosition() { 158 return position; 159 } 160 161 /** 162 * <p>Changes the position of the gloss in the painted area. Only the 163 * values defined in the GlossPosition enum are valid.</p> 164 * 165 * @param position The position at which the gloss is painted 166 */ 167 public void setPosition(GlossPosition position) { 168 GlossPosition old = this.position; 169 this.position = position; 170 firePropertyChange("position", old, getPosition()); 171 } 172 }