01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.logistics.ui.inventory.dialog;
28:
29: import javax.swing.*;
30: import java.awt.*;
31: import java.awt.geom.AffineTransform;
32: import java.util.Collections;
33: import java.util.Map;
34:
35: /**
36: * An icon used in the Color Preferences tab.
37: */
38: public class ColoredBox implements Icon {
39: /** The width of the outline, if any **/
40: private static final int STROKE_WIDTH = 1;
41: /** The margin around the rectangle to prevent clipping **/
42: private static final int MARGIN = STROKE_WIDTH / 2;
43: /** The main color of the rectangle. **/
44: Color color;
45: /** The size of the rectangle (width and height) **/
46: int width;
47: /** Should the outline be drawn. **/
48: protected boolean drawOutline = true;
49: /** Hints on how to render the rectangle. **/
50: static final Map hints = Collections.singletonMap(
51: RenderingHints.KEY_ANTIALIASING,
52: RenderingHints.VALUE_ANTIALIAS_ON);
53: /** A wide stroke to draw the outline if any. **/
54: static final Stroke wideStroke = new BasicStroke(STROKE_WIDTH);
55:
56: /**
57: * Create a Colored rectangle icon.
58: * @param c the color
59: * @param width the width
60: **/
61: public ColoredBox(Color c, int width) {
62: this .color = c;
63: this .width = width;
64: }
65:
66: /**
67: * Paint the icon into the given Component at the specified
68: * location.
69: **/
70: public void paintIcon(Component c, Graphics g, int x, int y) {
71: Graphics2D g2d = (Graphics2D) g;
72: Color oldColor = g2d.getColor();
73: RenderingHints oldHints = g2d.getRenderingHints();
74: g2d.addRenderingHints(hints);
75: g2d.setColor(color);
76: g2d.fillRect(x + MARGIN, y + MARGIN, width - 2 * MARGIN, width
77: - 2 * MARGIN);
78: if (drawOutline) {
79: Stroke oldStroke = g2d.getStroke();
80: g2d.setStroke(wideStroke);
81: g2d.setColor(Color.darkGray);
82: g2d.drawRect(x + MARGIN, y + MARGIN, width - 2 * MARGIN,
83: width - 2 * MARGIN);
84: g2d.setStroke(oldStroke);
85: }
86: g2d.setColor(oldColor);
87: g2d.setRenderingHints(oldHints);
88: }
89:
90: public int getIconWidth() {
91: return width;
92: }
93:
94: public int getIconHeight() {
95: return width;
96: }
97:
98: }
|