001: /*
002: * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. 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 JGoodies Karsten Lentzsch 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:
031: package com.jgoodies.looks.plastic;
032:
033: import java.awt.Color;
034: import java.awt.Graphics;
035:
036: import javax.swing.plaf.metal.MetalLookAndFeel;
037:
038: import com.jgoodies.looks.LookUtils;
039:
040: /**
041: * Consists exclusively of static methods that provide convenience behavior.
042: *
043: * @author Karsten Lentzsch
044: * @version $Revision: 1.1 $
045: */
046:
047: public final class PlasticXPUtils {
048:
049: private PlasticXPUtils() {
050: // Overrides default constructor; prevents instantiation.
051: }
052:
053: /**
054: * Draws a plain border for an xp button.
055: */
056: static void drawPlainButtonBorder(Graphics g, int x, int y, int w,
057: int h) {
058: drawButtonBorder(g, x, y, w, h,
059: PlasticLookAndFeel.getControl(), PlasticLookAndFeel
060: .getControlDarkShadow(), LookUtils
061: .getSlightlyBrighter(PlasticLookAndFeel
062: .getControlDarkShadow(), 1.25f));
063: }
064:
065: /**
066: * Draws a border for a pressed xp button.
067: */
068: static void drawPressedButtonBorder(Graphics g, int x, int y,
069: int w, int h) {
070: drawPlainButtonBorder(g, x, y, w, h);
071: Color darkColor = translucentColor(PlasticLookAndFeel
072: .getControlDarkShadow(), 128);
073: Color lightColor = translucentColor(PlasticLookAndFeel
074: .getControlHighlight(), 80);
075: g.translate(x, y);
076: g.setColor(darkColor);
077: g.fillRect(2, 1, w - 4, 1);
078:
079: g.setColor(lightColor);
080: g.fillRect(2, h - 2, w - 4, 1);
081: g.translate(-x, -y);
082: }
083:
084: /**
085: * Draws a border for a default xp button.
086: */
087: static void drawDefaultButtonBorder(Graphics g, int x, int y,
088: int w, int h) {
089: drawPlainButtonBorder(g, x, y, w, h);
090: drawInnerButtonDecoration(g, x, y, w, h, PlasticLookAndFeel
091: .getPrimaryControlDarkShadow());
092: }
093:
094: /**
095: * Draws a border for a focused xp button.
096: */
097: static void drawFocusedButtonBorder(Graphics g, int x, int y,
098: int w, int h) {
099: drawPlainButtonBorder(g, x, y, w, h);
100: drawInnerButtonDecoration(g, x, y, w, h, PlasticLookAndFeel
101: .getFocusColor());
102: }
103:
104: /**
105: * Draws a border for a disabled xp button.
106: */
107: static void drawDisabledButtonBorder(Graphics g, int x, int y,
108: int w, int h) {
109: drawButtonBorder(g, x, y, w, h,
110: PlasticLookAndFeel.getControl(), MetalLookAndFeel
111: .getControlShadow(), LookUtils
112: .getSlightlyBrighter(MetalLookAndFeel
113: .getControlShadow()));
114: }
115:
116: /**
117: * Draws a button border for an xp button with the given colors.
118: */
119: public static void drawButtonBorder(Graphics g, int x, int y,
120: int w, int h, Color backgroundColor, Color edgeColor,
121: Color cornerColor) {
122:
123: g.translate(x, y);
124: // Edges
125: g.setColor(edgeColor);
126: drawRect(g, 0, 0, w - 1, h - 1);
127:
128: // Near corners
129: g.setColor(cornerColor);
130: g.fillRect(0, 0, 2, 2);
131: g.fillRect(0, h - 2, 2, 2);
132: g.fillRect(w - 2, 0, 2, 2);
133: g.fillRect(w - 2, h - 2, 2, 2);
134:
135: // Corners
136: g.setColor(backgroundColor);
137: g.fillRect(0, 0, 1, 1);
138: g.fillRect(0, h - 1, 1, 1);
139: g.fillRect(w - 1, 0, 1, 1);
140: g.fillRect(w - 1, h - 1, 1, 1);
141:
142: g.translate(-x, -y);
143: }
144:
145: /**
146: * Draws a button border for an xp button with the given colors.
147: */
148: private static void drawInnerButtonDecoration(Graphics g, int x,
149: int y, int w, int h, Color baseColor) {
150:
151: Color lightColor = translucentColor(baseColor, 90);
152: Color mediumColor = translucentColor(baseColor, 120);
153: Color darkColor = translucentColor(baseColor, 200);
154:
155: g.translate(x, y);
156: g.setColor(lightColor);
157: g.fillRect(2, 1, w - 4, 1);
158:
159: g.setColor(mediumColor);
160: g.fillRect(1, 2, 1, h - 4);
161: g.fillRect(w - 2, 2, 1, h - 4);
162: drawRect(g, 2, 2, w - 5, h - 5);
163:
164: g.setColor(darkColor);
165: g.fillRect(2, h - 2, w - 4, 1);
166: g.translate(-x, -y);
167: }
168:
169: /**
170: * An optimized version of Graphics.drawRect.
171: */
172: static void drawRect(Graphics g, int x, int y, int w, int h) {
173: g.fillRect(x, y, w + 1, 1);
174: g.fillRect(x, y + 1, 1, h);
175: g.fillRect(x + 1, y + h, w, 1);
176: g.fillRect(x + w, y + 1, 1, h);
177: }
178:
179: /**
180: * Returns a color that is a translucent copy of the given color.
181: *
182: * @param baseColor the base color
183: * @param alpha the alpha value
184: * @return the translucent color with specified alpha
185: */
186: private static Color translucentColor(Color baseColor, int alpha) {
187: return new Color(baseColor.getRed(), baseColor.getGreen(),
188: baseColor.getBlue(), alpha);
189: }
190:
191: }
|