The following example shows how you can create a simple image map in Flex 4 using a Group container with nested Graphic tags and then drawing hit area shapes using the Rect, Ellipse, or Path classes.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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/2010/03/22/creating-a-simple-image-map-in-flex-4/ -->
<s:Application name="Spark_Graphic_mouseEnabledWhereTransparent_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <s:Label text="alpha:" />
        <s:HSlider id="sldr"
                minimum="0.0" maximum="1.0"
                value="0.3"
                snapInterval="0.1" stepSize="0.1" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
        ]]>
    </fx:Script>
 
    <s:Group horizontalCenter="0" verticalCenter="0">
        <s:BitmapImage id="img" source="@Embed('fx_appicon-tn.gif')" />
        <s:Graphic buttonMode="true"
                mouseEnabledWhereTransparent="false"
                toolTip="Link 1"
                useHandCursor="true"
                click="Alert.show('click1');">
            <s:Rect id="hit1"
                    width="20" height="20"
                    x="5" y="5">
                <s:fill>
                    <s:SolidColor color="red" alpha="{sldr.value}" />
                </s:fill>
            </s:Rect>
        </s:Graphic>
        <s:Graphic buttonMode="true"
                mouseEnabledWhereTransparent="false"
                toolTip="Link 2"
                useHandCursor="true"
                click="Alert.show('click2');">
            <s:Ellipse id="hit2"
                    width="20" height="20"
                    x="25" y="10">
                <s:fill>
                    <s:SolidColor color="haloOrange" alpha="{sldr.value}" />
                </s:fill>
            </s:Ellipse>
        </s:Graphic>
        <s:Graphic buttonMode="true"
                mouseEnabledWhereTransparent="false"
                toolTip="Link 3"
                useHandCursor="true"
                click="Alert.show('click3');">
            <s:Path id="hit3"
                    data="M 10 0 L 20 20 L 0 20 L 10 0 Z"
                    x="10" y="25">
                <s:fill>
                    <s:SolidColor color="yellow" alpha="{sldr.value}" />
                </s:fill>
            </s:Path>
        </s:Graphic>
    </s:Group>
 
</s:Application>

View source is enabled in the following example.

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.

 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

8 Responses to Creating a simple image map in Flex 4

  1. Anonymous says:

    Hello!
    Is it possible to post link for working example? Oh just screenshot?

  2. Nuwan says:

    Thanks for the nice tutorial!

  3. Doug Edwards says:

    the code generates three declaration errors. Finding it difficult to find many Flex examples.

  4. sandeep pp says:

    Hi Peter,

    Is it possible to do something like thin flex 3?

    Thanks
    Sandeep

  5. ndriana says:

    Hi,

    Seeing the data for the triangle there. I know it’s off topics, but is there a way to get them automatically from Flash builder for a drawing?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree