001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.gui.component; 020 021import icy.util.ColorUtil; 022 023import java.awt.Color; 024import java.awt.Graphics; 025 026import javax.swing.JPanel; 027 028/** 029 * @author Stephane 030 */ 031public class ColorComponent extends JPanel 032{ 033 /** 034 * 035 */ 036 private static final long serialVersionUID = 2883762420253112984L; 037 038 private Color color; 039 040 public ColorComponent(Color color) 041 { 042 super(true); 043 044 this.color = color; 045 046 setOpaque(true); 047 setVisible(true); 048 } 049 050 public ColorComponent() 051 { 052 this(null); 053 } 054 055 /** 056 * @return the color 057 */ 058 public Color getColor() 059 { 060 return color; 061 } 062 063 /** 064 * @param color 065 * the color to set 066 */ 067 public void setColor(Color color) 068 { 069 this.color = color; 070 071 if (color != null) 072 setToolTipText(ColorUtil.toString(color.getRGB(), false, " : ")); 073 else 074 setToolTipText(""); 075 076 repaint(); 077 } 078 079 /** 080 * @param rgb 081 * the color to set 082 */ 083 public void setColor(int rgb) 084 { 085 setColor(new Color(rgb)); 086 } 087 088 @Override 089 protected void paintComponent(Graphics g) 090 { 091 if (color == null) 092 super.paintComponent(g); 093 else 094 { 095 final int w = getWidth(); 096 final int h = getHeight(); 097 098 g.setColor(color); 099 g.fillRect(0, 0, w, h); 100 g.setColor(Color.black); 101 g.drawRect(0, 0, w, h); 102 } 103 } 104}