Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The first concrete extension of the AnimatedBorder class is the ColorAnimatedBorder. This class paints a matte type border in a different color each animation cycle. This can be used to smoothly fade in or out a border color, or to grab attention by starkly varying the border color. The array of colors is created in the constructor. Each time the border is painted, the next color in the array is used for the color of the border.

The constructor calls the makeColors method, which creates the array of colors cycled through during the animation. This class could be extended, and overriding the makeColors method to create a different array of colors gives any color effect desired. The complete source listing for the ColorAnimatedBorder is given in Listing 5.2.

Listing 5.2 The ColorAnimatedBorder Class

package com.foley.borders;

import java.awt.Insets;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Color;

import javax.swing.JComponent;

import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

/**
 * ColorAnimatedBorder is a concrete AnimatedBorder.  It
 * paints the border a different color each animation cycle.
 *
 * @see AnimatedBorder
 * @see Border
 * @author Mike Foley
 **/
public class ColorAnimatedBorder extends AnimatedBorder {

    /**
     * The colors used for the border.  The elements in this
     * array are stepped through, and drawn each cycle.
     **/
    protected Color[] colors = null;

    /**
     * The index of the next color to use when drawing the border.
     **/
    private int currentColorIndex = 0;

    /**
     * Constructor.
     * Create insets from the raw parameter and call our
     * constructor taking Insets to do the work.
     *
     * @param c The component we border.
     * @param top The top border width.
     * @param left The left border width.
     * @param bottom The bottom border width.
     * @param right The right border width.
     **/
    public ColorAnimatedBorder( JComponent c, int top, int left,
                           int bottom, int right ) {
        this( c, new Insets( top, left, bottom, right ) );
    }

    /**
     * Constructor.
     * Make the colors used to paint the border.
     *
     * @param c The component we border.
     * @param insets The border size.
     **/
    public ColorAnimatedBorder( JComponent c, Insets insets ) {
        super( c, insets );
        colors = makeColors();
    }

    /**
     * Make an array of colors that are stepped through during
     * the paintBorder methods.
     * If a great number of colors (thousands) are to be animated,
     * it may better to create one color each paint cycle, and let the
     * GC free the colors when no longer needed.
     **/
    protected void makeColors() {
        Color[] colors = new Color[ 20 ];
        int step = 12;
        for( int i = 0; i < colors.length; i++ ) {
            colors[ i ] = new Color( 100, 100, i * step );
        }

        return( colors );

    } // makeColors

    /**
     * Paint the border.
     * Get the color at the current color index.  Then update the index.
     * The index is wrapped, so that the array of colors repeat every
     * length of the array.
     *
     * @param c The bordered component.
* @param g The graphics to paint with.
     * @param x The X location of the bordered component.
     * @param y The Y location of the bordered component.
     * @param width The width of the bordered component.
     * @param height The height of the bordered component.
     **/
    public void paintBorder( Component c, Graphics g, int x,
                             int y, int width, int height ) {

        //
        // Save the current color so we can restore at end of method.
        //
        Color oldColor = g.getColor();

        g.translate( x, y );

        //
        // Set the color to paint with.  Increment the color index.
        // Wrap if needed.
        //
        Color color = colors[ currentColorIndex++ ];
        currentColorIndex %= colors.length;
        //

        // Get our size.
        //
        Insets insets = getBorderInsets( c );

        //
        // Paint the border.
        //
        g.setColor( color );
        g.fillRect( 0, 0, width - insets.right, insets.top );
        g.fillRect( 0, insets.top, insets.left, height - insets.top );
g.fillRect( insets.left, height - insets.bottom,
                    width - insets.left, insets.bottom );
g.fillRect( width - insets.right, 0,
                    insets.right, height - insets.bottom );

        //
        // Restore the Graphics object’s state.
        //
        g.translate( -x, -y );
        g.setColor( oldColor );
    } // paintBorder

} // ColorAnimatedBorder

The second child of the AnimatedBorder class is the ChaserLightsBorder class. This class provides a border that mimics chaser lights. The lights are drawn as simple circles whose diameters are the border width. A single pixel is placed between each light. By changing the direction property, the chasing direction can be altered. The on and off color of the lights can be specified by setting bound properties. The number of “on” lights between the “off” chaser light is also a bound property. Setting this property to 2 turns every other light on. The complete source listing for the ChaserLightsBorder class is given in Listing 5.3.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.