User Guide

Table Of Contents
58 Chapter 3: Creating Advanced Components in Flash MX 2004
{
mode_mc.move(layoutWidth - mode_mc.width, 0);
text_mc.move(0, 0);
}
else {
mode_mc.move(0, 0);
text_mc.move(mode_mc.width, 0);
}
}
Implementing the draw() method
The
draw() method displays objects on the screen. Whenever the component needs to draw an
interface element, it calls a
draw() method. You use the draw() method to create or modify
elements that are subject-to-change.
Everything is made visible in the
draw() method. A border does not actually call the drawing API
until its
draw() method is called. Any graphical assets that you bring in for the purposes of
measuring are invisible until the
draw() method is called.
You should not call the
draw() method directly. Instead, call one of the invalidation methods,
and that method calls the
draw() method. Flex also calls the draw() method from the redraw()
method. However, Flex calls the
redraw() method only if the object is invalidated, so you should
actually call an invalidation method if you want Flex to invoke the
draw() or redraw() method.
If you do not call an invalidation method, the component remains invisible unless you set its
visibility property to
true in MXML. For more information, see About invalidation
on page 68.
Flex also calls the
draw() method after the layoutChildren() method.
Inside the
draw() method, you can use calls to the Flash drawing API to draw borders, rules, and
other graphical elements. You can also call the
clear() method, which removes the visible
objects. In general, to set lengths in the
draw() method, you should use this.layoutWidth and
this.layoutHeight instead of width and height.
The following example clears the component and then draws a border around the component:
function draw():Void {
clear();
if (bTextChanged) {
bTextChanged = false;
text_mc.text = text;
}
// Draw a border around everything.
drawRect(0, 0, this.layoutWidth, this.layoutHeight);
}
Defining getters and setters
Getters and setters provide visibility to component properties and control access to those
properties by other objects.