001: /*
002: * Copyright (c) 2005-2008 Flamingo / 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 Flamingo 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.flamingo.ribbon.gallery.oob;
031:
032: import java.awt.*;
033: import java.awt.geom.Ellipse2D;
034:
035: import org.jvnet.flamingo.common.icon.ResizableIcon;
036: import org.jvnet.substance.theme.SubstanceTheme;
037:
038: /**
039: * Resizable icon for <b>Substance</b> themes.
040: *
041: * @author Kirill Grouchnikov
042: */
043: public class ThemeResizableIcon implements ResizableIcon {
044: /**
045: * Current width in pixels.
046: */
047: private int currWidth;
048:
049: /**
050: * Current height in pixels.
051: */
052: private int currHeight;
053:
054: /**
055: * Original width in pixels.
056: */
057: private int origWidth;
058:
059: /**
060: * Original height in pixels.
061: */
062: private int origHeight;
063:
064: /**
065: * Associated theme. Can be <code>null</code>.
066: */
067: private SubstanceTheme theme;
068:
069: /**
070: * Creates a new icon.
071: *
072: * @param theme
073: * Associated theme (can be <code>null</code>).
074: * @param startWidth
075: * Original width in pixels.
076: * @param startHeight
077: * Original heigth in pixels.
078: */
079: public ThemeResizableIcon(SubstanceTheme theme, int startWidth,
080: int startHeight) {
081: this .theme = theme;
082: this .origWidth = startWidth;
083: this .origHeight = startHeight;
084: this .currWidth = startWidth;
085: this .currHeight = startHeight;
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.jvnet.flamingo.common.ResizableIcon#setDimension(java.awt.Dimension)
092: */
093: public void setDimension(Dimension newDimension) {
094: this .currWidth = newDimension.width;
095: this .currHeight = newDimension.height;
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see javax.swing.Icon#getIconHeight()
102: */
103: public int getIconHeight() {
104: return this .currHeight;
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see javax.swing.Icon#getIconWidth()
111: */
112: public int getIconWidth() {
113: return this .currWidth;
114: }
115:
116: /*
117: * (non-Javadoc)
118: *
119: * @see org.jvnet.flamingo.common.ResizableIcon#setHeight(int)
120: */
121: public void setHeight(int height) {
122: double coef = (double) height / (double) this .currHeight;
123: this .currWidth = (int) (coef * this .currWidth);
124: this .currHeight = height;
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.jvnet.flamingo.common.ResizableIcon#setWidth(int)
131: */
132: public void setWidth(int width) {
133: double coef = (double) width / (double) this .currWidth;
134: this .currHeight = (int) (coef * this .currHeight);
135: this .currWidth = width;
136: }
137:
138: /*
139: * (non-Javadoc)
140: *
141: * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics,
142: * int, int)
143: */
144: public void paintIcon(Component c, Graphics g, int x, int y) {
145: Graphics2D graphics = (Graphics2D) g.create();
146: graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
147: RenderingHints.VALUE_ANTIALIAS_ON);
148:
149: graphics.translate(x, y);
150:
151: double R = Math.max(3.0, this .currHeight / 10.0);
152: double r = R / 2.0;
153: double s = (this .currHeight - 2 * R) / 2.0;
154: double cx = this .currWidth / 2.0;
155: // double cy = this.currHeight / 2.0;
156: double dx = s * Math.sqrt(3.0) / 2.0;
157: // double ss = (this.currHeight - 2 * r) / 2.0;
158: // double dxs = ss * Math.sqrt(3.0) / 2.0;
159:
160: R = Math.floor(R);
161:
162: Color color1 = (this .theme == null) ? Color.red : this .theme
163: .getColorScheme().getUltraDarkColor();
164: Color color2 = (this .theme == null) ? Color.yellow : this .theme
165: .getColorScheme().getDarkColor();
166: Color color3 = (this .theme == null) ? Color.green : this .theme
167: .getColorScheme().getMidColor();
168: Color color4 = (this .theme == null) ? Color.cyan : this .theme
169: .getColorScheme().getLightColor();
170: Color color5 = (this .theme == null) ? Color.blue : this .theme
171: .getColorScheme().getExtraLightColor();
172: Color color6 = (this .theme == null) ? Color.magenta
173: : this .theme.getColorScheme().getUltraLightColor();
174:
175: this .paintCircle(graphics, cx, R, R, color1, true);
176: if (this .currHeight > 16)
177: this .paintCircle(graphics, cx + dx, R + 0.5 * s, R, color2,
178: true);
179: this .paintCircle(graphics, cx + dx, R + 1.5 * s, R, color3,
180: true);
181: if (this .currHeight > 16)
182: this .paintCircle(graphics, cx, this .currHeight - 1 - R, R,
183: color4, true);
184: this .paintCircle(graphics, cx + 1 - dx, R + 1.5 * s, R, color5,
185: true);
186: if (this .currHeight > 16)
187: this .paintCircle(graphics, cx + 1 - dx, R + 0.5 * s, R,
188: color6, true);
189:
190: if (r >= 2.0) {
191: this .paintCircle(graphics, cx + 0.5 + dx / 2.0, R + 0.25
192: * s, r, color1, false);
193: this
194: .paintCircle(graphics, cx + dx, R + s, r, color2,
195: false);
196: this .paintCircle(graphics, cx + 0.5 + dx / 2.0, R
197: + (this .currHeight - 1 + 1.5 * s) / 2.0, r, color3,
198: false);
199: this .paintCircle(graphics, cx - 0.5 - dx / 2.0, R
200: + (this .currHeight - 1 + 1.5 * s) / 2.0, r, color4,
201: false);
202: this
203: .paintCircle(graphics, cx - dx, R + s, r, color5,
204: false);
205: this .paintCircle(graphics, cx - 0.5 - dx / 2.0, R + 0.25
206: * s, r, color6, false);
207: }
208:
209: graphics.dispose();
210: }
211:
212: /**
213: * Paints a single circle.
214: *
215: * @param graphics
216: * Graphics context.
217: * @param xc
218: * X coordinate of the circle center.
219: * @param yc
220: * Y coordinate of the circle center.
221: * @param r
222: * Circle radius.
223: * @param color
224: * Circle color.
225: * @param toDrawOutline
226: * Indication whether the circle outline should be drawn.
227: */
228: private void paintCircle(Graphics2D graphics, double xc, double yc,
229: double r, Color color, boolean toDrawOutline) {
230: Shape shape = new Ellipse2D.Double(xc - r, yc - r, 2 * r, 2 * r);
231: this .paintShape(graphics, shape, color, toDrawOutline);
232: }
233:
234: /**
235: * Paints shape.
236: *
237: * @param graphics
238: * Graphics context.
239: * @param shape
240: * Shape to draw.
241: * @param color
242: * Circle color.
243: * @param toDrawOutline
244: * Indication whether the shape outline should be drawn.
245: */
246: private void paintShape(Graphics2D graphics, Shape shape,
247: Color color, boolean toDrawOutline) {
248: graphics.setColor(color);
249: graphics.fill(shape);
250: if (toDrawOutline) {
251: graphics.setColor(color.darker());
252: graphics.draw(shape);
253: }
254: }
255:
256: /*
257: * (non-Javadoc)
258: *
259: * @see org.jvnet.flamingo.common.ResizableIcon#revertToOriginalDimension()
260: */
261: public void revertToOriginalDimension() {
262: this.currHeight = this.origHeight;
263: this.currWidth = this.origWidth;
264: }
265: }
|