FileDocCategorySizeDatePackage
AnimatedPanel.javaAPI DocExample6778Mon Jan 09 11:02:00 GMT 2006None

AnimatedPanel

public class AnimatedPanel extends JPanel
Display an animated panel. The panel contains a picture and a text message. As soon as start() is called, the pictures and the text glow in cycles. The animation can be stopped at anytime by calling stop(). You can set the font and its color by calling setFont() and setForeground().
author
Romain Guy

Fields Summary
protected float
gradient
protected String
message
protected Thread
animator
protected BufferedImage
convolvedImage
protected BufferedImage
originalImage
protected static AlphaComposite
composite
Constructors Summary
public AnimatedPanel(String message, ImageIcon icon)
Creates an animated panel with a message and a picture.

param
message The message to display, cannot be null nor empty.
param
icon The picture to display


                                     
         
        this.message = message;

        Image image = icon.getImage();
        originalImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        convolvedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = originalImage.createGraphics();
        g.drawImage(image, 0, 0, this);
        g.dispose();

        setBrightness(1.0f);
        setOpaque(false);
    
Methods Summary
public java.awt.DimensiongetPreferredSize()

        FontRenderContext context = ((Graphics2D) getGraphics()).getFontRenderContext();
        TextLayout layout = new TextLayout(message, getFont(), context);
        Rectangle2D bounds = layout.getBounds();
        return new Dimension(originalImage.getWidth() + 12,
                             (int) (originalImage.getHeight() + bounds.getHeight() + layout.getAscent() + 12));
    
public java.lang.StringgetText()
Returns the current displayed message.

        return message;
    
public voidpaintComponent(java.awt.Graphics g)

        super.paintComponent(g);

        if (convolvedImage != null) {
            int width = getWidth();
            int height = getHeight();

            synchronized (convolvedImage) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

                FontRenderContext context = g2.getFontRenderContext();
                TextLayout layout = new TextLayout(message, getFont(), context);
                Rectangle2D bounds = layout.getBounds();

                int x = (width - convolvedImage.getWidth(null)) / 2;
                int y = (int) (height - (convolvedImage.getHeight(null) + bounds.getHeight() + layout.getAscent())) / 2;

                g2.drawImage(convolvedImage, x, y, this);
                Color foreground = getForeground();
                g2.setColor(new Color(foreground.getRed(), foreground.getGreen(), foreground.getBlue(),
                                      (int) (gradient * 255)));
                layout.draw(g2, (float) (width - bounds.getWidth()) / 2,
                    (float) (y + convolvedImage.getHeight(null) + bounds.getHeight() + layout.getAscent()));
            }
        }
    
private voidsetBrightness(float multiple)
Changes the image luminosity.

        float[] brightKernel = { multiple };
        RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        BufferedImageOp bright = new ConvolveOp(new Kernel(1, 1, brightKernel), ConvolveOp.EDGE_NO_OP, hints);
        bright.filter(originalImage, convolvedImage);
        repaint();
    
private voidsetGradientFactor(float gradient)
Changes the text gradient control value.

        this.gradient = gradient;
    
public voidsetText(java.lang.String text)
Changes the displayed message at runtime.

param
text The message to be displayed. Can be null or empty.

        this.message = text;
        repaint();
    
public voidstart()
Starts the animation. A thread called "Highlighter" is spawned and can be interrupted at anytime by invoking stop().

        this.animator = new Thread(new HighlightCycler(), "Highlighter");
        this.animator.start();
    
public voidstop()
Safely stops the animation.

        if (this.animator != null) {
            setBrightness(1.0f);
            setGradientFactor(0.0f);
            this.animator.interrupt();
        }
        this.animator = null;