001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.ui.console;
028:
029: import javax.swing.*;
030: import java.awt.*;
031: import java.awt.geom.AffineTransform;
032: import java.util.Collections;
033: import java.util.Map;
034:
035: /** Create a Colored circle icon from the color specified.
036: * Used in legends.
037: */
038:
039: public class ColoredCircle implements Icon {
040: /** The width of the outline, if any **/
041: private static final int STROKE_WIDTH = 2;
042: /** The margin around the circle to prevent clipping **/
043: private static final int MARGIN = STROKE_WIDTH / 2;
044: /** An image to draw on top of the circle, if desired. **/
045: protected Image img;
046: /** The main color of the circle. **/
047: Color color;
048: /** The size of the circle (width and height) **/
049: int width;
050: /** Should the outline be drawn. Set by subclass **/
051: protected boolean selected = false;
052: /** Hints on how to render the circle. **/
053: static final Map hints = Collections.singletonMap(
054: RenderingHints.KEY_ANTIALIASING,
055: RenderingHints.VALUE_ANTIALIAS_ON);
056: /** A wide stroke to draw the outline if any. **/
057: static final Stroke wideStroke = new BasicStroke(STROKE_WIDTH);
058:
059: /**
060: * Create a Colored circle icon.
061: * @param c the color
062: * @param width the width
063: **/
064: public ColoredCircle(Color c, int width, Image img) {
065: this .color = c;
066: this .width = width;
067: this .img = img;
068: }
069:
070: /**
071: * Paint the icon into the given Component as the specified
072: * location.
073: **/
074: public void paintIcon(Component c, Graphics g, int x, int y) {
075: Graphics2D g2d = (Graphics2D) g;
076: Color oldColor = g2d.getColor();
077: RenderingHints oldHints = g2d.getRenderingHints();
078: g2d.addRenderingHints(hints);
079: g2d.setColor(color);
080: g2d.fillOval(x + MARGIN, y + MARGIN, width - 2 * MARGIN, width
081: - 2 * MARGIN);
082: if (selected) {
083: Stroke oldStroke = g2d.getStroke();
084: g2d.setStroke(wideStroke);
085: g2d.setColor(Color.darkGray);
086: g2d.drawOval(x + MARGIN, y + MARGIN, width - 2 * MARGIN,
087: width - 2 * MARGIN);
088: g2d.setStroke(oldStroke);
089: }
090: if (img != null)
091: g2d.drawImage(img, new AffineTransform(1f, 0f, 0f, 1f,
092: x + 2, y + 2), null);
093: g2d.setColor(oldColor);
094: g2d.setRenderingHints(oldHints);
095: }
096:
097: public int getIconWidth() {
098: return width;
099: }
100:
101: public int getIconHeight() {
102: return width;
103: }
104:
105: }
|