HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"JColor"
Volume: 3 Issue: 12, p. 16

Download files assoicated with this article

	

Listing 1.
 
public interface ColorConstants 
{ 
  public static final int RED = 1; 
  public static final int GREEN = 2; 
  public static final int BLUE = 3; 
  public static final int HUE = 4; 
  public static final int SATURATION = 5; 
  public static final int BRIGHTNESS = 6; 
} 

Listing 2.
 
public class ColorSliderImage extends SyntheticImage 
  implements ColorConstants 
{ 
  protected int style; 

  public ColorSliderImage(int width, int height, int style) 
  { 
    super(width, height); 
    this.style = style; 
  } 

  protected void computeRow(int y, int[] row) 
  { 
    for(int x = 0; x < width; x++) 
    { 
      if (style == RED) 
      { 
        int r = 255 - 
          (int)(((float)y / (float)height) * 255); 
        row[x] = makeColor(r, 0, 0); 
      } 
      if (style == GREEN) 
      { 
        int g = 255 - 
          (int)(((float)y / (float)height) * 255); 
        row[x] = makeColor(0, g, 0); 
      } 
      if (style == BLUE) 
      { 
        int b = 255 - 
          (int)(((float)y / (float)height) * 255); 
        row[x] = makeColor(0, 0, b); 
      } 

      if (style == HUE) 
      { 
        float h = 1f - (float)y / (float)height; 
        row[x] = Color.HSBtoRGB(h, 1f, 1f); 
      } 
      if (style == SATURATION) 
      { 
        float s = 1f - (float)y / (float)height; 
        row[x] = Color.HSBtoRGB(1f, s, 1f); 
      } 
      if (style == BRIGHTNESS) 
      { 
        float b = 1f - (float)y / (float)height; 
        row[x] = Color.HSBtoRGB(1f, 1f, b); 
      } 
    } 
  } 

  private int makeColor(int r, int g, int b) 
  { 
    return (0xff << 24) | (r << 16) | (g << 8) | b; 
  } 
} 
  

Listing 3.
 
public class ColorMatrixImage extends SyntheticImage 
  implements ColorConstants 
{ 
  protected int style; 

  public ColorMatrixImage(int width, int height, int style) 
  { 
    super(width, height); 
    this.style = style; 
  } 

  protected void computeRow(int y, int[] row) 
  { 
    for(int x = 0; x < width; x++) 
    { 
      if (style == RED) 
      { 
        int g = (int)(((float)x / (float)width) * 255); 
        int b = 255 - (int)(((float)y / (float)height) * 255); 
        row[x] = makeColor(0, g, b); 
      } 
      if (style == GREEN) 
      { 
        int r = (int)(((float)x / (float)width) * 255); 
        int b = 255 - (int)(((float)y / (float)height) * 255); 
        row[x] = makeColor(r, 0, b); 
      } 
      if (style == BLUE) 
      { 
        int r = (int)(((float)x / (float)width) * 255); 
        int g = 255 - (int)(((float)y / (float)height) * 255); 
        row[x] = makeColor(r, g, 0); 
      } 

      if (style == HUE) 
      { 
        float s = (float)x / (float)width; 
        float b = 1f - (float)y / (float)height; 
        row[x] = Color.HSBtoRGB(1f, s, b); 
      } 
      if (style == SATURATION) 
      { 
        float h = (float)x / (float)width; 
        float b = 1f - (float)y / (float)height; 
        row[x] = Color.HSBtoRGB(h, 1f, b); 
      } 
      if (style == BRIGHTNESS) 
      { 
        float h = (float)x / (float)width; 
        float s = 1f - (float)y / (float)height; 
        row[x] = Color.HSBtoRGB(h, s, 1f); 
      } 
    } 
  } 

  private int makeColor(int r, int g, int b) 
  { 
    return (0xff << 24) | (r << 16) | (g << 8) | b; 
  } 
} 
  

Listing 4.
 
public abstract class AbstractColorSelector extends JComponent 
  implements MouseListener, KeyListener, 
    FocusListener, ColorConstants 
{ 
  protected Vector listeners = new Vector(); 
  protected ImageIcon image = null; 
  protected Dimension size; 
  protected int style; 

  public void addActionListener(ActionListener listener) 
  { 
    listeners.addElement(listener); 
  } 

  public void removeActionListener(ActionListener listener) 
  { 
    listeners.removeElement(listener); 
  } 

  public void fireActionEvent() 
  { 
    Vector list = (Vector)listeners.clone(); 
    ActionEvent event = new ActionEvent(this, 
      ActionEvent.ACTION_PERFORMED, "Action"); 
    ActionListener listener; 
    for (int i = 0; i < list.size(); i++) 
    { 
      listener = (ActionListener)list.elementAt(i); 
      listener.actionPerformed(event); 
    } 
  } 

  public boolean isFocusTraversable() 
  { 
    return true; 
  } 

  public void mouseClicked(MouseEvent event) {} 
  public void mouseReleased(MouseEvent event) {} 
  public void mouseEntered(MouseEvent event) {} 
  public void mouseExited(MouseEvent event) {} 

  public void keyTyped(KeyEvent event) {} 
  public void keyReleased(KeyEvent event) {} 

  public void focusGained(FocusEvent event) {} 
  public void focusLost(FocusEvent event) {} 
} 
  

Listing 5.
 
public class JColorSlider extends AbstractColorSelector 
{ 
  protected int y = 127 + 5; 
  protected boolean hasFocus = false; 

  public JColorSlider(int style) 
  { 
    setStyle(style); 
    addKeyListener(this); 
    addMouseListener(this); 
    addFocusListener(this); 
  } 

  public void setStyle(int style) 
  { 
    this.style = style; 
    image = null; 
    repaint(); 
  } 

  public Dimension getPreferredSize() 
  { 
    return new Dimension(30, 266); 
  } 

  public int getYValue() 
  { 
    return 255 - (int)(((float)(y - 5) / 
      (float)(getSize().height - 10)) * 256); 
  } 

  public void setYValue(int value) 
  { 
    y = 5 + (int)((getSize().height - 10) * 
      ((float)(255 - value) / 256f)); 
    repaint(); 
  } 

  public void paintComponent(Graphics g) 
  { 
    if (image == null || !size.equals(getSize())) 
    { 
      size = getSize(); 
      int w = size.width - 10; 
      int h = size.height - 10; 
      image = new ImageIcon(createImage( 
        new ColorSliderImage(w, h, style))); 
    } 
    g.drawImage(image.getImage(), 5, 5, this); 
    drawFocus(g); 
    g.setColor(getForeground()); 
    drawLeftArrow(g, y); 
    drawRightArrow(g, y); 
    drawCrosshair(g, getSize().width / 2, y); 
  } 

  protected void drawFocus(Graphics g) 
  { 
    if (hasFocus) 
    { 
      int w = getSize().width - 5; 
      int h = getSize().height - 5; 

      g.setColor(getBackground().darker()); 
      g.drawLine(w, 4, w, h); 
      g.drawLine(4, h, w, h); 
      g.setColor(getBackground().brighter()); 
      g.drawLine(4, 4, w, 4); 
      g.drawLine(4, 4, 4, h); 
    } 
  } 

  protected void drawCrosshair(Graphics g, int x, int y) 
  { 
    g.setColor(Color.black); 
    g.drawLine(x - 3, y, x - 7, y); 
    g.drawLine(x + 3, y, x + 7, y); 

    g.setColor(Color.white); 
    g.drawLine(x - 3, y - 1, x - 7, y - 1); 
    g.drawLine(x - 3, y + 1, x - 7, y + 1); 
    g.drawLine(x + 3, y - 1, x + 7, y - 1); 
    g.drawLine(x + 3, y + 1, x + 7, y + 1); 
  } 

  protected void drawLeftArrow(Graphics g, int y) 
  { 
    int w = 5; 
    int h = 5; 
    int l = 5; 
    Polygon arrow = new Polygon(); 
    arrow.addPoint(l, y); 
    arrow.addPoint(l - w, y - h); 
    arrow.addPoint(l - w, y + h); 
    arrow.addPoint(l, y); 
    g.fillPolygon(arrow); 
  } 

  protected void drawRightArrow(Graphics g, int y) 
  { 
    int w = 5; 
    int h = 5; 
    int r = getSize().width - 5; 
    Polygon arrow = new Polygon(); 
    arrow.addPoint(r, y); 
    arrow.addPoint(r + w, y - h); 
    arrow.addPoint(r + w, y + h); 
    arrow.addPoint(r, y); 
    g.fillPolygon(arrow); 
  } 

  public void mousePressed(MouseEvent event) 
  { 
    requestFocus(); 
    int yy = event.getY(); 
    if (yy < 5 || yy >= getSize().height - 5) return; 
      y = yy; 
    repaint(); 
    fireActionEvent(); 
  } 

  public void keyPressed(KeyEvent event) 
  { 
    int code = event.getKeyCode(); 
    if (code == KeyEvent.VK_UP & y > 5) y--; 
    if (code == KeyEvent.VK_DOWN & 
      y < getSize().height - 6) y++; 
    repaint(); 
    fireActionEvent(); 
  } 

  public void focusGained(FocusEvent event) 
  { 
    hasFocus = true; 
    repaint(); 
  } 

  public void focusLost(FocusEvent event) 
  { 
    hasFocus = false; 
    repaint(); 
  } 
} 

Listing 6.
 
public class JColorMatrix extends JColorSlider 
{ 
  protected int x = 128 + 5; 

  public JColorMatrix(int style) 
  { 
    super(style); 
  } 

  public Dimension getPreferredSize() 
  { 
    return new Dimension(266, 266); 
  } 

  public int getXValue() 
  { 
    int value = (int)(((float)(x - 5) / 
      (float)(getSize().width - 10)) * 256); 
    return value; 
  } 

  public void setXValue(int value) 
  { 
    x = 5 + (int)((getSize().width - 10) * 
      ((float)value / 256f)); 
    repaint(); 
  } 

  public void paintComponent(Graphics g) 
  { 
    if (image == null || !size.equals(getSize())) 
    { 
      size = getSize(); 
      int w = size.width - 10; 
      int h = size.height - 10; 
      image = new ImageIcon(createImage( 
        new ColorMatrixImage(w, h, style))); 
    } 
    g.drawImage(image.getImage(), 5, 5, this); 
    drawFocus(g); 
    g.setColor(getForeground()); 
    drawLeftArrow(g, y); 
    drawRightArrow(g, y); 
    drawTopArrow(g, x); 
    drawBottomArrow(g, x); 
    drawCrosshair(g, x, y); 
  } 

  protected void drawCrosshair(Graphics g, int x, int y) 
  { 
    g.setColor(Color.black); 
    g.drawLine(x, y - 3, x, y - 6); 
    g.drawLine(x, y + 3, x, y + 6); 
    g.drawLine(x - 3, y, x - 6, y); 
    g.drawLine(x + 3, y, x + 6, y); 

    g.setColor(Color.white); 
    g.drawLine(x - 1, y - 3, x - 1, y - 6); 
    g.drawLine(x + 1, y - 3, x + 1, y - 6); 
    g.drawLine(x - 1, y + 3, x - 1, y + 6); 
    g.drawLine(x + 1, y + 3, x + 1, y + 6); 
    g.drawLine(x - 3, y - 1, x - 6, y - 1); 
    g.drawLine(x - 3, y + 1, x - 6, y + 1); 
    g.drawLine(x + 3, y - 1, x + 6, y - 1); 
    g.drawLine(x + 3, y + 1, x + 6, y + 1); 
  } 

  protected void drawTopArrow(Graphics g, int x) 
  { 
    int w = 5; 
    int h = 5; 
    int t = 5; 
    Polygon arrow = new Polygon(); 
    arrow.addPoint(x, t); 
    arrow.addPoint(x - w, t - h); 
    arrow.addPoint(x + w, t - h); 
    arrow.addPoint(x, t); 
    g.fillPolygon(arrow); 
  } 

  public void drawBottomArrow(Graphics g, int y) 
  { 
    int w = 5; 
    int h = 5; 
    int b = getSize().height - 5; 
    Polygon arrow = new Polygon(); 
    arrow.addPoint(x, b); 
    arrow.addPoint(x - w, b + h); 
    arrow.addPoint(x + w, b + h); 
    arrow.addPoint(x, b); 
    g.fillPolygon(arrow); 
  } 

  public void mousePressed(MouseEvent event) 
  { 
    requestFocus(); 
    int xx = event.getX(); 
    int yy = event.getY(); 
    if (yy < 5 || yy >= getSize().height - 5) return; 
    if (xx < 5 || xx >= getSize().width - 5) return; 
    x = xx; 
    y = yy; 
    repaint(); 
    fireActionEvent(); 
  } 

  public void keyPressed(KeyEvent event) 
  { 
    int code = event.getKeyCode(); 
    if (code == KeyEvent.VK_UP & y > 5) y--; 
    if (code == KeyEvent.VK_LEFT & x > 5) x--; 
    if (code == KeyEvent.VK_DOWN & 
      y < getSize().height - 6) y++; 
    if (code == KeyEvent.VK_RIGHT & 
      x < getSize().width - 6) x++; 
    repaint(); 
    fireActionEvent(); 
  } 
} 
  

Listing 7.
 
public void actionPerformed(ActionEvent event) 
{ 
  Object source = event.getSource(); 

  if (source == rRadioRGB) style =  RED; 
  if (source == gRadioRGB) style =  GREEN; 
  if (source == bRadioRGB) style =  BLUE; 
  if (source == hRadioHSB) style =  HUE; 
  if (source == sRadioHSB) style =  SATURATION; 
  if (source == bRadioHSB) style =  BRIGHTNESS; 
  if (source instanceof JRadioButton) 
  { 
    slider.setStyle(style); 
    matrix.setStyle(style); 
  } 

  if (source == rFieldRGB) 
  { 
    if (!(rRadioRGB.isSelected() || 
          gRadioRGB.isSelected() || 
          bRadioRGB.isSelected())) 
    { 
      rRadioRGB.setSelected(true); 
      style = RED; 
      slider.setStyle(style); 
      matrix.setStyle(style); 
    } 
    if (style == RED) 
      slider.setYValue(fieldValue(rFieldRGB)); 
    if (style == GREEN) 
      matrix.setXValue(fieldValue(rFieldRGB)); 
    if (style == BLUE) 
      matrix.setXValue(fieldValue(rFieldRGB)); 
  } 

  // Radio button handlers omited 

  setColorText(); 
  swatch.repaint(); 
} 
  

Listing 8.
 
public class JColorTest extends WindowAdapter 
{ 
  static JColor jcolor; 

  public void windowClosing(WindowEvent event) 
  { 
    System.out.println(jcolor.getColor().toString()); 
    System.exit(0); 
  } 

  public static void main(String[] args) 
  { 
    PLAF.setNativeLookAndFeel(true); 
    jcolor = new JColor(); 
    JFrame frame = new JFrame("JColor Test"); 
    frame.getContentPane().add("Center", jcolor); 
    frame.addWindowListener(new JColorTest()); 
    frame.pack(); 
    frame.show(); 
    jcolor.setColor(Color.green); 
  } 
} 
  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.