An icon for painting a square swatch of a specified Color. : Icon « 2D Graphics GUI « Java






An icon for painting a square swatch of a specified Color.

  
/*
 *  ColorSwatch.java
 *  2007-03-03
 */

//cb.aloe.decor;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;

/**
 * An icon for painting a square swatch of a specified Color.
 * 
 * @author Christopher Bach
 */
public class ColorSwatch implements Icon {

  private Color ourSwatchColor = Color.white;

  private Color ourBorderColor = Color.black;

  private boolean ourBorderPainted = true;

  private boolean ourSwatchIsMultiColor = false;

  private boolean ourSwatchIsVoid = false;

  private int ourSwatchSize = 14;

  /**
   * Creates a standard 14 x 14 swatch with a black border and white background.
   */
  public ColorSwatch() {

  }

  /**
   * Creates a swatch of the specified size with a black border and white
   * background.
   */
  public ColorSwatch(int size) {
    setSwatchSize(size);
  }

  /**
   * Creates a swatch of the specified size with a black border and white
   * background and determines whether or n not the border should be painted.
   */
  public ColorSwatch(int size, boolean borderPainted) {
    setSwatchSize(size);
    setBorderPainted(borderPainted);
  }

  /**
   * 
   */
  public ColorSwatch(Color color) {
    setColor(color);
  }

  /**
   * 
   */
  public ColorSwatch(int size, Color color) {
    setSwatchSize(size);
    setColor(color);
  }

  /**
   * 
   */
  public ColorSwatch(int size, Color color, Color borderColor) {
    setSwatchSize(size);
    setColor(color);
    setBorderColor(borderColor);
    setBorderPainted(true);
  }

  /**
   * Sets the size of this swatch.
   */
  public void setSwatchSize(int size) {
    if (size > 0)
      ourSwatchSize = size;
    else
      ourSwatchSize = 14;
  }

  /**
   * Returns the size of this swatch.
   */
  public int getSwatchSize() {
    return ourSwatchSize;
  }

  /**
   * Determines whether or not this swatch's border should be painted.
   */
  public void setBorderPainted(boolean borderPainted) {
    ourBorderPainted = borderPainted;
  }

  /**
   * Returns whether or not this swatch's border is painted.
   */
  public boolean isBorderPainted() {
    return ourBorderPainted;
  }

  /**
   * Sets the color of this swatch's border.
   */
  public void setBorderColor(Color color) {
    ourBorderColor = color;
  }

  /**
   * Returns the color of this swatch's border.
   */
  public Color getBorderColor() {
    return ourBorderColor;
  }

  /**
   * Sets the color that this swatch represents.
   */
  public void setColor(Color color) {
    ourSwatchIsMultiColor = false;
    ourSwatchColor = color;
  }

  /**
   * Returns the color that this swatch represents.
   */
  public Color getColor() {
    return ourSwatchColor;
  }

  /**
   * Sets this swatch to represent more than one color.
   */
  public void setMultiColor() {
    ourSwatchIsMultiColor = true;
  }

  /**
   * Returns whether or not this swatch represents more than one color.
   */
  public boolean isMultiColor() {
    return ourSwatchIsMultiColor;
  }

  /**
   * Determines whether or not this swatch is void. If the swatch is void, it
   * will not be painted at all.
   */
  public void setVoid(boolean isVoid) {
    // When true, this icon will not be painted at all.
    ourSwatchIsVoid = isVoid;
  }

  /**
   * Returns whether this swatch is void. If the swatch is void, it will not be
   * painted at all.
   */
  public boolean isVoid() {
    return ourSwatchIsVoid;
  }

  // // Icon implementation ////

  /**
   * Returns the width of this Icon.
   */
  public int getIconWidth() {
    return ourSwatchSize;
  }

  /**
   * Returns the height of this Icon.
   */
  public int getIconHeight() {
    return ourSwatchSize;
  }

  /**
   * Paints this Icon into the provided graphics context.
   */
  public void paintIcon(Component c, Graphics g, int x, int y) {
    if (ourSwatchIsVoid)
      return;

    Color oldColor = g.getColor();

    if (ourSwatchIsMultiColor) {
      g.setColor(Color.white);
      g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
      g.setColor(ourBorderColor);
      for (int i = 0; i < ourSwatchSize; i += 2) {
        g.drawLine(x + i, y, x + i, y + ourSwatchSize);
      }
    }

    else if (ourSwatchColor != null) {
      g.setColor(ourSwatchColor);
      g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
    }

    else {
      g.setColor(Color.white);
      g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
      g.setColor(ourBorderColor);
      g.drawLine(x, y, x + ourSwatchSize, y + ourSwatchSize);
      g.drawLine(x, y + ourSwatchSize, x + ourSwatchSize, y);
    }

    if (ourBorderPainted) {
      g.setColor(ourBorderColor);
      g.drawRect(x, y, ourSwatchSize, ourSwatchSize);
    }

    g.setColor(oldColor);
  }

}

   
    
  








Related examples in the same category

1.Create a dynamic icon
2.A simple application to test the functionality of the OvalIcon classA simple application to test the functionality of the OvalIcon class
3.Icon DisplayerIcon Displayer
4.Implement the Icon interfaceImplement the Icon interface
5.Calendar Page icons with Weekday, Day and MonthCalendar Page icons with Weekday, Day and Month
6.MemImage is an in-memory icon showing a Color gradientMemImage is an in-memory icon showing a Color gradient
7.Example of an icon that changes formExample of an icon that changes form
8.Custom Icon DemoCustom Icon Demo
9.Create Image Icon from PNG file
10.Reading an Image or Icon from a File
11.Draw an Icon object
12.Plain Color Icon
13.Color Icon
14.Arrow Icon
15.Icon Line
16.Return a filled oval as an Icon
17.Layered Icon
18.An empty icon with arbitrary width and height.
19.Transparent icon with no content
20.Creates a transparent icon.
21.Icon Codec