| 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.Dimension; |
| 18 | import java.awt.Font; |
| 19 | import java.awt.FontMetrics; |
| 20 | import java.awt.Graphics; |
| 21 | import java.awt.Insets; |
| 22 | |
| 23 | import javax.swing.BorderFactory; |
| 24 | import javax.swing.Icon; |
| 25 | import javax.swing.JPanel; |
| 26 | |
| 27 | /** |
| 28 | * Special component to be used as the preview panel of JColorChooser. |
| 29 | * It displays a rectangle and some text over an Icon, in order to easily |
| 30 | * see colors that have an alpha channel (transparency). |
| 31 | * |
| 32 | * @author Jean-Francois Poilpret |
| 33 | */ |
| 34 | public class AlphaRGBPreviewPanel extends JPanel |
| 35 | { |
| 36 | static final private int INSETS_X = 10; |
| 37 | static final private int INSETS_Y = 10; |
| 38 | static final private String TEXT = "ABCDEF abcdef 1234567890"; |
| 39 | |
| 40 | public AlphaRGBPreviewPanel(Icon icon) |
| 41 | { |
| 42 | setName("color-chooser-preview"); |
| 43 | setBackgroundIcon(icon); |
| 44 | setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0)); |
| 45 | } |
| 46 | |
| 47 | @Override public void paintComponent(Graphics g) |
| 48 | { |
| 49 | super.paintComponent(g); |
| 50 | |
| 51 | Dimension size = getSize(); |
| 52 | Insets insets = getInsets(); |
| 53 | int x = (size.width - _icon.getIconWidth() - insets.left - insets.right) / 2; |
| 54 | int y = (size.height - _icon.getIconHeight() - insets.top - insets.bottom) / 2; |
| 55 | |
| 56 | // Paint the icon in the center of the panel |
| 57 | _icon.paintIcon(this, g, x, y); |
| 58 | |
| 59 | // Paint a rectangle over the icon |
| 60 | int width = _icon.getIconWidth() - 2 * INSETS_X; |
| 61 | int height = (_icon.getIconHeight() - 2 * INSETS_Y) / 2; |
| 62 | g.setColor(getForeground()); |
| 63 | g.fillRect(x + INSETS_X, y + INSETS_Y, width, height); |
| 64 | |
| 65 | // Paint text over the icon |
| 66 | g.setFont(_font); |
| 67 | FontMetrics metrics = g.getFontMetrics(); |
| 68 | int textWidth = metrics.stringWidth(TEXT); |
| 69 | x += (width - textWidth) / 2; |
| 70 | y += height + metrics.getHeight(); |
| 71 | g.drawString(TEXT, x + INSETS_X, y + INSETS_Y); |
| 72 | } |
| 73 | |
| 74 | @Override public Dimension getPreferredSize() |
| 75 | { |
| 76 | // Warning: MUST return a new Dimension object every time else a strange |
| 77 | // JColorChooser bug will occur. |
| 78 | return new Dimension(_size); |
| 79 | } |
| 80 | |
| 81 | public void setBackgroundIcon(Icon icon) |
| 82 | { |
| 83 | _icon = icon; |
| 84 | _size = new Dimension(icon.getIconWidth(), icon.getIconHeight()); |
| 85 | setSize(_size); |
| 86 | repaint(); |
| 87 | } |
| 88 | |
| 89 | protected Icon _icon; |
| 90 | protected Dimension _size; |
| 91 | |
| 92 | protected final Font _font = new Font("Monospaced", Font.PLAIN, 18); |
| 93 | |
| 94 | } |