
Conclusion
Well, we should have a 28 lines of code class. If not, here is the complete CelsiusConverter class:
package {
import org.flashapi.swing.*;
import org.flashapi.swing.event.*;
public class CelsiusConverter extends Popup {
public function CelsiusConverter() {
super("Convert Celsius to Fahrenheit", 250);
initialize();
}
private var _result:Label;
private var _input:TextInput;
private function initialize():void {
autoHeight = true;
padding = horizontalGap = verticalGap = 10;
_input = new TextInput("0");
_input.maxChars = 5;
_input.restrict = "0-9";
_result = new Label("fahrenheit");
var _btn = new Button("Convert", _input.width);
var celsiusLabel:Label = new Label("celsius");
addGraphicElements(_input, celsiusLabel, _btn, _result);
eventCollector.addEvent(_btn, UIMouseEvent.CLICK, convertTemp);
}
private function convertTemp(event:UIMouseEvent):void {
var temp:Number = Number(_input.label);
_result.label = String(temp * 1.8 + 32) + " fahrenheit";
}
}
}
Let's see how to use our widget directly from the Flash CS3 main timeline.
When you use SPAS 3.0 from Flash IDE instead of the Application class (org.flashapi.swing.Application) you must declare the Stage instance. (This is a consequence of the impossibility to access to a global reference of the Stage instance in Actionscript 3.0.) Said like that, this could seem complex. But it's not! Indeed, you just have to put the following lines on the first frame of the main timeline:
import org.flashapi.swing.*;
UIManager.initialize(this);
Once you have done that, you can easily create and display the CelsiusConverter object, and even use SPAS 3.0 built-in effects such as shadow or reflection:
import org.flashapi.swing.*;
UIManager.initialize(this);
var cc:CelsiusConverter = new CelsiusConverter();
cc.shadow = cc.reflection = true;
cc.display();
Ok! It's finished; just compile the FLA to see the result!
I hope you've enjoyed this tutorial. You can find more resources and tutorials on the SPAS 3.0 Web site at: http://www.flashapi.org/
