
Macromedia Flex comes with many components, such as buttons, labels, and containers. However, there are times you will want to write your own component when none of the standard Flex components meet your needs. The easiest way to customize components, is by extending existing components if they do most, but not all, of what you require. However, there are times when your needs are so specific that you want to build a completely new component.
This tutorial is part one of a two-part series, and teaches you how to create a completely new component from the ground up. The example component in this article is an old-fashioned analog clock. Because an analog clock has several elements, such as the face and hands, it is a good example of a simple component that you can improve upon later. You can use the techniques in this article to get started, and you'll learn how to change a simple clock into one that has more complex functionality.
To complete this tutorial you will need to install the following software and files:
Figure 1. The Basic Analog Clock
When writing a component, you can put your code into certain places to override known methods so that the Flex component framework activates and draws your component at the right time. Failure to follow this framework leads to frustration and non-working components.
To change one part of the component without rewriting the entire component, I like to break the component into smaller parts. Each of the elements, face and hands, is a child object of the whole component. This way, each element draws only when necessary and you can easily position them. This is important since the clock shows the current time. With the hands constantly moving, you do not want to draw hands for every second.