| 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 FixedSizeColorIcon implements ColorIcon |
| 30 | { |
| 31 | public FixedSizeColorIcon() |
| 32 | { |
| 33 | this(DEFAULT_SIZE); |
| 34 | } |
| 35 | |
| 36 | public FixedSizeColorIcon(int size) |
| 37 | { |
| 38 | this(size, size); |
| 39 | } |
| 40 | |
| 41 | public FixedSizeColorIcon(int width, int height) |
| 42 | { |
| 43 | this(width, height, DEFAULT_INSET, DEFAULT_INSET); |
| 44 | } |
| 45 | |
| 46 | public FixedSizeColorIcon(int width, int height, int insetX, int insetY) |
| 47 | { |
| 48 | _insetX = insetX; |
| 49 | _insetY = insetY; |
| 50 | _width = width; |
| 51 | _height = height; |
| 52 | } |
| 53 | |
| 54 | public void setColor(Color color) |
| 55 | { |
| 56 | _color = color; |
| 57 | } |
| 58 | |
| 59 | public Color getColor() |
| 60 | { |
| 61 | return _color; |
| 62 | } |
| 63 | |
| 64 | public int getIconWidth() |
| 65 | { |
| 66 | return _width + 2 * _insetX; |
| 67 | } |
| 68 | |
| 69 | public int getIconHeight() |
| 70 | { |
| 71 | return _height + 2 * _insetY; |
| 72 | } |
| 73 | |
| 74 | public void paintIcon(Component c, Graphics g, int x, int y) |
| 75 | { |
| 76 | g.setColor(_color); |
| 77 | g.fillRect(x + _insetX, y + _insetY, _width, _height); |
| 78 | } |
| 79 | |
| 80 | private final int _insetX; |
| 81 | private final int _insetY; |
| 82 | private final int _width; |
| 83 | private final int _height; |
| 84 | private Color _color; |
| 85 | |
| 86 | static private final int DEFAULT_INSET = 2; |
| 87 | static private final int DEFAULT_SIZE = 20; |
| 88 | } |