/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.gui.customcomponents;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.RadialGradientPaint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.Shape;
import java.awt.Transparency;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.border.Border;
/** A dark button, principly used as the search button.
* @author ug22cmg */
public class DarkButton extends JButton
{
// Constants
private static int H = 45;
private static int W = 80;
private static final Key AA_KEY = RenderingHints.KEY_ANTIALIASING;
private static final Object AA_VALUE = RenderingHints.VALUE_ANTIALIAS_ON;
private static final float DIST1 = 0.7f;
private static final float DIST2 = 0.9f;
private static final Color C1 = new Color(50, 50, 50, 70);
private static final Color C2 = new Color(50, 50, 50, 0);
private static final Color C3 = Color.GRAY.darker().darker();
private static final Color C4 = Color.BLACK.brighter().brighter().brighter();
private static final Color C5 = Color.GRAY.darker();
private static final Color C6 = Color.BLACK.brighter().brighter().brighter().
brighter().brighter();
private static final Color C7 = Color.DARK_GRAY.brighter();
private static final CycleMethod CYCLE = CycleMethod.NO_CYCLE;
private static final Rectangle RECT = new Rectangle(3, 0, W - 1, H - 1);
private static final Paint SHADOW_PAINT = getShadow();
private static final Dimension DIMENSIONS = new Dimension(W, H);
private static final Font FONT = new Font("Arial", Font.PLAIN, 13);
private static final Color TRANSPARENT = new Color(255, 255, 255, 0);
private static final Border B = BorderFactory.createEmptyBorder(10, 0, 10, 5);
private static final int CURSORID = Cursor.HAND_CURSOR;
private static final Cursor CURSOR = Cursor.getPredefinedCursor(CURSORID);
private static final Paint GRAD1 = new GradientPaint(0, 10, C3, 0, H - 2, C4);
private static final Paint GRAD2 = new GradientPaint(0, 10, C5, 0, H - 2, C6);
private static final Paint GRAD3 = new GradientPaint(0, 0, Color.DARK_GRAY.
brighter(), 0, H - 18, C3);
private static final Paint GRAD4 = new GradientPaint(0, 0, C7, 0, H - 13, Color.DARK_GRAY.darker().darker());
private static final Shape RRECT = new RoundRectangle2D.Double(5, 8, W - 11,
H - 12, 10, 10);
private static final Shape RRECT2 = new RoundRectangle2D.Double(3, 9, W - 10,
H - 13, 10, 10);
private static final int TYPE = Transparency.TRANSLUCENT;
// Fields
private boolean isHovered;
private BufferedImage bi;
/** Initializes the DarkButton.
* @param label The button label. */
public DarkButton(String label)
{
super(label);
super.setSize(DIMENSIONS);
super.setPreferredSize(DIMENSIONS);
super.setFont(FONT);
super.setForeground(Color.WHITE);
super.setBackground(TRANSPARENT);
super.setOpaque(false);
super.setFocusPainted(false);
super.setBorderPainted(false);
super.setRolloverEnabled(false);
super.setBorder(B);
super.addMouseListener(new MouseManager());
super.setCursor(CURSOR);
}
/** Sets the new hovered state of the button.
* @param isHovered Whether the button is now hovered or not. */
public void setHovered(boolean isHovered)
{
this.isHovered = isHovered;
setForeground(Color.WHITE);
repaint();
}
/** Gets the shadow paint.
* @return The shadow paint. */
private static final RadialGradientPaint getShadow()
{
final float[] dist =
{
DIST1, DIST2
};
final Color[] colors =
{
C1, C2
};
return new RadialGradientPaint(RECT, dist, colors, CYCLE);
}
/** Paints onto the double buffered context.
* @param g2d The graphics object to paint with. */
private void paintOnImage(Graphics2D g2d)
{
g2d.setRenderingHint(AA_KEY, AA_VALUE);
g2d.translate(-10, 0);
g2d.setPaint(SHADOW_PAINT);
g2d.fillRoundRect(0, 1, W - 1, H - 2, 10, 10);
if (!isHovered) g2d.setPaint(GRAD1);
else g2d.setPaint(GRAD2);
g2d.fill(RRECT);
g2d.setPaint(Color.BLACK);
g2d.draw(RRECT);
if (isHovered) g2d.setPaint(GRAD3);
else g2d.setPaint(GRAD4);
g2d.draw(RRECT2);
g2d.translate(4, 0);
}
/** Handles doubled buffered painting.
* @param g The graphics object to paint with. */
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(AA_KEY, AA_VALUE);
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
bi = gc.createCompatibleImage(getWidth(), H, TYPE);
paintOnImage(g2d);
g2d.drawImage(bi, 0, 0, null);
super.paintComponent(g2d);
}
/** For handling internal events. */
private class MouseManager extends MouseAdapter
{
/** Handles mouse hover.
* @param e The event arguments. */
@Override
public void mouseEntered(MouseEvent e)
{
setHovered(true);
}
/** Handles mouse out.
* @param e The event arguments. */
@Override
public void mouseExited(MouseEvent e)
{
setHovered(false);
}
}
}