/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.gui.panels;
import java.awt.BasicStroke;
import java.awt.Color;
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.Paint;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
/** A panel with a dark gradient background (with buffered optimization)
* @author ug22cmg */
public class DarkPanel extends JPanel
{
private static final Key AA_KEY = RenderingHints.KEY_ANTIALIASING;
private static final Object AA_VALUE = RenderingHints.VALUE_ANTIALIAS_ON;
private static final Color C1 = Color.DARK_GRAY.darker().darker();
private static final Color C2 = Color.DARK_GRAY.brighter();
private static final Paint GRAD = new GradientPaint(0, 0, C1, 0, 50, C2);
private static final int TRANSPARENCY = Transparency.TRANSLUCENT;
private int cachedHeight = Integer.MIN_VALUE;
private int cachedWidth = Integer.MIN_VALUE;
private BufferedImage bi;
/** Initializes the DarkPanel */
public DarkPanel()
{
super.setOpaque(false);
}
/** Draws onto the buffered image. */
private void drawUncached()
{
Graphics2D g2d = (Graphics2D) bi.getGraphics();
g2d.setRenderingHint(AA_KEY, AA_VALUE);
g2d.setPaint(GRAD);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setStroke(new BasicStroke(1));
g2d.setPaint(Color.BLACK);
g2d.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
g2d.dispose();
}
/** Creates the image to double buffer. */
private void createImage()
{
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
bi = gc.createCompatibleImage(getWidth(), getHeight(), TRANSPARENCY);
}
/** Gets whether there are cached resources.
* @return True if cached. */
private boolean isCached()
{
return cachedHeight == getHeight() && cachedWidth == getWidth();
}
/** Paints the component.
* @param g The graphics object to paint onto. */
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(AA_KEY, AA_VALUE);
if (!isCached())
{
createImage();
drawUncached();
cachedHeight = getHeight();
cachedWidth = getWidth();
}
g2d.drawImage(bi, 0, 0, null);
super.paintComponent(g2d);
}
}