Creating a simple image map in Flex 4

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 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/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>

[GoogleAdsWide]

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.

8 thoughts on “Creating a simple image map in Flex 4

    1. @Anonymous,

      I don’t have time right now to create a new Flex Project and upload the SWF, but all you need to do is copy/paste the code and embed one image.

      Peter

  1. 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?

Comments are closed.