Setting the end caps on a Spark Line stroke in Flex 4

The following example shows how you can set the end caps on a Spark SolidColorStroke object by setting the caps property to one of the static constant values in the flash.display.CapsStyle class.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/24/setting-the-end-caps-on-a-spark-line-stroke-in-flex-4/ -->
<s:Application name="Spark_Line_stroke_caps_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="caps:">
                <s:DropDownList id="dropDownList" requireSelection="true">
                    <s:dataProvider>
                        <s:ArrayList source="[round,square,none]" />
                    </s:dataProvider>
                </s:DropDownList>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <mx:VRule height="{vGr.height+20}"
            horizontalCenter="-100"
            verticalCenter="0" />
    <mx:VRule height="{vGr.height+20}"
            horizontalCenter="100"
            verticalCenter="0" />
 
    <s:VGroup id="vGr" gap="10" horizontalCenter="0" verticalCenter="0">
        <s:Line width="200">
            <s:stroke>
                <s:SolidColorStroke color="red"
                        caps="{dropDownList.selectedItem}"
                        weight="20"
                        alpha="0.4" />
            </s:stroke>
        </s:Line>
    </s:VGroup>
 
</s:Application>

View source is enabled in the following example.

[GoogleAdsWide]

You can also set the caps property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/24/setting-the-end-caps-on-a-spark-line-stroke-in-flex-4/ -->
<s:Application name="Spark_Line_stroke_caps_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Script>
        <![CDATA[
            import mx.graphics.SolidColorStroke;
            import spark.events.IndexChangeEvent;
 
            protected function dropDownList_change(evt:IndexChangeEvent):void {
                lineStroke.caps = dropDownList.selectedItem;
                /* or
                SolidColorStroke(line.stroke).caps = dropDownList.selectedItem;
                */
            }
        ]]>
    </fx:Script>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="caps:">
                <s:DropDownList id="dropDownList"
                        requireSelection="true"
                        change="dropDownList_change(event);">
                    <s:dataProvider>
                        <s:ArrayList source="[round,square,none]" />
                    </s:dataProvider>
                </s:DropDownList>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <mx:VRule height="{vGr.height+20}"
            horizontalCenter="-100"
            verticalCenter="0" />
    <mx:VRule height="{vGr.height+20}"
            horizontalCenter="100"
            verticalCenter="0" />
 
    <s:VGroup id="vGr" gap="10" horizontalCenter="0" verticalCenter="0">
        <s:Line id="line" width="200">
            <s:stroke>
                <s:SolidColorStroke id="lineStroke"
                        color="red"
                        weight="20"
                        alpha="0.4" />
            </s:stroke>
        </s:Line>
    </s:VGroup>
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/24/setting-the-end-caps-on-a-spark-line-stroke-in-flex-4/ -->
<s:Application name="Spark_Line_stroke_caps_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayList;
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.VRule;
            import mx.graphics.SolidColorStroke;
            import spark.components.DropDownList;
            import spark.components.VGroup;
            import spark.events.IndexChangeEvent;
            import spark.primitives.Line;
 
            private var dropDownList:DropDownList;
            private var line:Line;
            private var lineStroke:SolidColorStroke;
            private var vGr:VGroup;
 
            private function init():void {
                var arr:Array = [CapsStyle.ROUND, CapsStyle.SQUARE, CapsStyle.NONE];
                var arrList:ArrayList = new ArrayList(arr);
 
                dropDownList = new DropDownList();
                dropDownList.dataProvider = arrList;
                dropDownList.requireSelection = true;
                dropDownList.addEventListener(IndexChangeEvent.CHANGE, dropDownList_change);
 
                var formItem:FormItem = new FormItem();
                formItem.label = "caps:";
                formItem.addElement(dropDownList);
 
                var form:Form = new Form();
                form.styleName = "plain";
                form.addElement(formItem);
 
                var appControlBar:ApplicationControlBar = new ApplicationControlBar();
                appControlBar.percentWidth = 100;
                appControlBar.setStyle("cornerRadius", 0);
                appControlBar.addElement(form);
                addElementAt(appControlBar, 0);
 
                var vRule1:VRule = new VRule();
                vRule1.height = 40;
                vRule1.horizontalCenter = -100;
                vRule1.verticalCenter = 0;
                addElement(vRule1);
 
                var vRule2:VRule = new VRule();
                vRule2.height = 40;
                vRule2.horizontalCenter = 100;
                vRule2.verticalCenter = 0;
                addElement(vRule2);
 
                lineStroke = new SolidColorStroke();
                lineStroke.color = 0xFF0000; // red
                lineStroke.caps = dropDownList.selectedItem;
                lineStroke.weight = 20;
                lineStroke.alpha = 0.4; // 40%
 
                line = new Line();
                line.width = 200;
                line.stroke = lineStroke;
 
                vGr = new VGroup();
                vGr.gap = 10;
                vGr.horizontalCenter = 0;
                vGr.verticalCenter = 0;
                vGr.addElement(line);
                addElement(vGr);
            }
 
            protected function dropDownList_change(evt:IndexChangeEvent):void {
                lineStroke.caps = dropDownList.selectedItem;
                /* or
                SolidColorStroke(line.stroke).caps = dropDownList.selectedItem;
                */
            }
        ]]>
    </fx:Script>
 
</s:Application>

This entry is based on a beta version of the Flex 4 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 4 SDK.