| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.component; |
| 16 | |
| 17 | import java.awt.Color; |
| 18 | import java.awt.Component; |
| 19 | import java.awt.Graphics; |
| 20 | |
| 21 | /** |
| 22 | * Specific Icon implementation that simply displays as a rectangle of a given |
| 23 | * color (which is settable). |
| 24 | * This is particularly useful for creating a colored JToggleButton in a |
| 25 | * JToolBar. |
| 26 | * |
| 27 | * @author jean-Francois Poilpret |
| 28 | */ |
| 29 | public class DynSizeColorIcon implements ColorIcon |
| 30 | { |
| 31 | public DynSizeColorIcon(Component parent) |
| 32 | { |
| 33 | this(parent, DEFAULT_INSET); |
| 34 | } |
| 35 | |
| 36 | public DynSizeColorIcon(Component parent, int inset) |
| 37 | { |
| 38 | this(parent, inset, inset); |
| 39 | } |
| 40 | |
| 41 | public DynSizeColorIcon(Component parent, int insetX, int insetY) |
| 42 | { |
| 43 | _parent = parent; |
| 44 | _insetX = insetX; |
| 45 | _insetY = insetY; |
| 46 | } |
| 47 | |
| 48 | public void setColor(Color color) |
| 49 | { |
| 50 | _color = color; |
| 51 | } |
| 52 | |
| 53 | public Color getColor() |
| 54 | { |
| 55 | return _color; |
| 56 | } |
| 57 | |
| 58 | public int getIconWidth() |
| 59 | { |
| 60 | return _parent.getWidth() - 2 * _insetX; |
| 61 | } |
| 62 | |
| 63 | public int getIconHeight() |
| 64 | { |
| 65 | return _parent.getHeight() - 2 * _insetY; |
| 66 | } |
| 67 | |
| 68 | public void paintIcon(Component c, Graphics g, int x, int y) |
| 69 | { |
| 70 | g.setColor(_color); |
| 71 | g.fillRect(x, y, getIconWidth(), getIconHeight()); |
| 72 | } |
| 73 | |
| 74 | private final Component _parent; |
| 75 | private final int _insetX; |
| 76 | private final int _insetY; |
| 77 | private Color _color; |
| 78 | |
| 79 | static private final int DEFAULT_INSET = 4; |
| 80 | } |