Creating a linear gradient stroke on a Rect object in Flex Gumbo

The following example shows how you can set a linear gradient stroke on a Flex Gumbo Rect object by setting the stroke property to a LinearGradientStroke object.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3”.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/31/creating-a-linear-gradient-stroke-on-a-rect-object-in-flex-gumbo/ -->
<Application name="Rect_stroke_LinearGradientStroke_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Graphic>
        <Rect id="rect" width="300" height="200">
            <stroke>
                <LinearGradientStroke>
                    <entries>
                        <GradientEntry color="red" />
                        <GradientEntry color="white" />
                        <GradientEntry color="blue" />
                    </entries>
                </LinearGradientStroke>
            </stroke>
        </Rect>
    </Graphic>

</Application>

You can also set the LinearGradientStroke object’s entries property using data binding, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/31/creating-a-linear-gradient-stroke-on-a-rect-object-in-flex-gumbo/ -->
<Application name="Rect_stroke_LinearGradientStroke_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Declarations>
        <Array id="arr">
            <GradientEntry color="red" />
            <GradientEntry color="white" />
            <GradientEntry color="blue" />
        </Array>
    </Declarations>

    <Graphic>
        <Rect id="rect" width="300" height="200">
            <stroke>
                <LinearGradientStroke entries="{arr}" />
            </stroke>
        </Rect>
    </Graphic>

</Application>

Or, you can set the LinearGradientStroke object’s entries property using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/31/creating-a-linear-gradient-stroke-on-a-rect-object-in-flex-gumbo/ -->
<Application name="Rect_stroke_LinearGradientStroke_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        <![CDATA[
            import mx.graphics.GradientEntry;

            private var arr:Array;

            private function init():void {
                arr = [];
                arr.push(new GradientEntry(0xFF0000));
                arr.push(new GradientEntry(0xFFFFFF));
                arr.push(new GradientEntry(0x0000FF));
                linearGrad.entries = arr;

            }
        ]]>
    </Script>

    <Graphic creationComplete="init();">
        <Rect id="rect" width="300" height="200">
            <stroke>
                <LinearGradientStroke id="linearGrad" />
            </stroke>
        </Rect>
    </Graphic>

</Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/31/creating-a-linear-gradient-stroke-on-a-rect-object-in-flex-gumbo/ -->
<Application name="Rect_stroke_LinearGradientStroke_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <Script>
        <![CDATA[
            import mx.graphics.GradientEntry;
            import mx.graphics.Graphic;
            import mx.graphics.LinearGradientStroke;
            import mx.graphics.Rect;

            private var arr:Array;
            private var rect:Rect;
            private var linearGrad:LinearGradientStroke;

            private function init():void {
                arr = [];
                arr.push(new GradientEntry(0xFF0000));
                arr.push(new GradientEntry(0xFFFFFF));
                arr.push(new GradientEntry(0x0000FF));

                linearGrad = new LinearGradientStroke();
                linearGrad.entries = arr;

                rect = new Rect();
                rect.stroke = linearGrad;
                rect.width = 300;
                rect.height = 200;

                var gr:Graphic = new Graphic();
                gr.addElement(rect);
                addChild(gr);
            }
        ]]>
    </Script>

</Application>

This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.

One thought on “Creating a linear gradient stroke on a Rect object in Flex Gumbo

Comments are closed.