001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.border;
031:
032: import java.awt.*;
033:
034: import org.jvnet.substance.color.ColorScheme;
035: import org.jvnet.substance.scroll.SubstanceScrollBarButton;
036: import org.jvnet.substance.utils.SubstanceColorUtilities;
037: import org.jvnet.substance.utils.SubstanceSizeUtils;
038:
039: /**
040: * The default border painter. This class is part of officially supported API.
041: *
042: * @author Kirill Grouchnikov
043: */
044: public class StandardBorderPainter implements SubstanceBorderPainter {
045: /*
046: * (non-Javadoc)
047: *
048: * @see org.jvnet.substance.painter.SubstanceGradientPainter#getDisplayName()
049: */
050: public String getDisplayName() {
051: return "Standard";
052: }
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see org.jvnet.substance.border.SubstanceBorderPainter#paintBorder(java.awt.Graphics,
058: * java.awt.Component, int, int, java.awt.Shape, java.awt.Shape,
059: * org.jvnet.substance.color.ColorScheme,
060: * org.jvnet.substance.color.ColorScheme, float, boolean)
061: */
062: public void paintBorder(Graphics g, Component c, int width,
063: int height, Shape contour, Shape innerContour,
064: ColorScheme colorScheme1, ColorScheme colorScheme2,
065: float cyclePos, boolean useCyclePosAsInterpolation) {
066: Graphics2D graphics = (Graphics2D) g.create();
067: graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
068: RenderingHints.VALUE_ANTIALIAS_ON);
069: graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
070: RenderingHints.VALUE_STROKE_NORMALIZE);
071:
072: ColorScheme interpolationScheme1 = colorScheme1;
073: ColorScheme interpolationScheme2 = useCyclePosAsInterpolation ? colorScheme2
074: : colorScheme1;
075:
076: double cycleCoef = 1.0 - cyclePos / 10.0;
077:
078: Color topBorderColor = getTopBorderColor(interpolationScheme1,
079: interpolationScheme2, cycleCoef,
080: useCyclePosAsInterpolation);
081: Color midBorderColor = getMidBorderColor(interpolationScheme1,
082: interpolationScheme2, cycleCoef,
083: useCyclePosAsInterpolation);
084: Color bottomBorderColor = getBottomBorderColor(
085: interpolationScheme1, interpolationScheme2, cycleCoef,
086: useCyclePosAsInterpolation);
087:
088: // Shape currClip = graphics.getClip();
089: float strokeWidth = SubstanceSizeUtils
090: .getBorderStrokeWidth(SubstanceSizeUtils
091: .getComponentFontSize(c));
092: int joinKind = (c instanceof SubstanceScrollBarButton) ? BasicStroke.JOIN_BEVEL
093: : BasicStroke.JOIN_ROUND;
094: int capKind = (c instanceof SubstanceScrollBarButton) ? BasicStroke.CAP_BUTT
095: : BasicStroke.CAP_BUTT;
096: graphics.setStroke(new BasicStroke(strokeWidth, capKind,
097: joinKind));
098:
099: // Draw border
100: if ((topBorderColor != null) && (midBorderColor != null)) {
101: GradientPaint gradientBorderTop = new GradientPaint(0, 0,
102: topBorderColor, 0, height / 2, midBorderColor);
103: Graphics2D topGr = (Graphics2D) graphics.create();
104: topGr.setPaint(gradientBorderTop);
105: topGr.clip(new Rectangle(0, 0, width, height / 2));
106: topGr.draw(contour);
107: topGr.dispose();
108: // graphics.setClip(currClip);
109: }
110:
111: if ((bottomBorderColor != null) && (midBorderColor != null)) {
112: Graphics2D bottomGr = (Graphics2D) graphics.create();
113: GradientPaint gradientBorderBottom = new GradientPaint(0,
114: height / 2, midBorderColor, 0, height,
115: bottomBorderColor);
116: bottomGr.setPaint(gradientBorderBottom);
117: bottomGr.clip(new Rectangle(0, height / 2, width,
118: 1 + height / 2));
119: bottomGr.draw(contour);
120: bottomGr.dispose();
121: }
122:
123: graphics.dispose();
124: }
125:
126: /**
127: * Computes the color of the top portion of the border. Override to provide
128: * different visual.
129: *
130: * @param interpolationScheme1
131: * The first interpolation scheme.
132: * @param interpolationScheme2
133: * The second interpolation scheme.
134: * @param cycleCoef
135: * Cycle coefficient.
136: * @param useCyclePosAsInterpolation
137: * Indicates whether the cycle coefficient should be used as the
138: * interpolation parameter.
139: * @return The color of the top portion of the border.
140: */
141: public Color getTopBorderColor(ColorScheme interpolationScheme1,
142: ColorScheme interpolationScheme2, double cycleCoef,
143: boolean useCyclePosAsInterpolation) {
144: return SubstanceColorUtilities.getTopBorderColor(
145: interpolationScheme1, interpolationScheme2, cycleCoef);
146: }
147:
148: /**
149: * Computes the color of the middle portion of the border. Override to
150: * provide different visual.
151: *
152: * @param interpolationScheme1
153: * The first interpolation scheme.
154: * @param interpolationScheme2
155: * The second interpolation scheme.
156: * @param cycleCoef
157: * Cycle coefficient.
158: * @param useCyclePosAsInterpolation
159: * Indicates whether the cycle coefficient should be used as the
160: * interpolation parameter.
161: * @return The color of the middle portion of the border.
162: */
163: public Color getMidBorderColor(ColorScheme interpolationScheme1,
164: ColorScheme interpolationScheme2, double cycleCoef,
165: boolean useCyclePosAsInterpolation) {
166: return SubstanceColorUtilities.getMidBorderColor(
167: interpolationScheme1, interpolationScheme2, cycleCoef);
168: }
169:
170: /**
171: * Computes the color of the bottom portion of the border. Override to
172: * provide different visual.
173: *
174: * @param interpolationScheme1
175: * The first interpolation scheme.
176: * @param interpolationScheme2
177: * The second interpolation scheme.
178: * @param cycleCoef
179: * Cycle coefficient.
180: * @param useCyclePosAsInterpolation
181: * Indicates whether the cycle coefficient should be used as the
182: * interpolation parameter.
183: * @return The color of the bottom portion of the border.
184: */
185: public Color getBottomBorderColor(ColorScheme interpolationScheme1,
186: ColorScheme interpolationScheme2, double cycleCoef,
187: boolean useCyclePosAsInterpolation) {
188: return SubstanceColorUtilities.getBottomBorderColor(
189: interpolationScheme1, interpolationScheme2, cycleCoef);
190: }
191:
192: }
|