Using AsWing/as3 with Haxe
This simple tutorial explains how to use the powerfull AsWing GUI framework within Haxe
This tutorial covers AsWing 1.5 and Haxe 2.07, for older versions see history
A quick note about recent changes, you can easily skip this
Since version 2.07 release, Haxe supports automatic generation of extern class tree, so you don't have to generate it manually. Moreover, the new --gen-hx-classes implementation (changelog) allows easy use of external libraries, and - AsWing related - use of LAFs
A fly in the ointment - file AsWingConstants.hx generated incorrectly
Download AsWing
- svn get latest version (2.0) of aswing/Haxe
http://svn.aswing.org/aswing/trunk/HaxeAsWing/
Setup a Test Project
Now let's start a new haxe project for testing.
- Copy the aswing.swf also in your project directory. This library will be compiled into your final movie.
- Create a file Hello.hxml:
-swf Hello.swf -swf-header 600:400:21:ffffff -main Hello -swf-lib aswing.swf -swf-version 9
Haxe will take AsWing class definitions from your swf
Create a file Hello.hx:
import org.aswing.JButton; import org.aswing.JFrame; import org.aswing.geom.IntDimension; import flash.Lib; import flash.display.Sprite; import flash.display.StageScaleMode; class Hello extends Sprite { public function new() { super(); //create a frame var frame : JFrame = new JFrame( this, "HelloApp" ); //if you want to create a "fullscreen" interface rather than //draggable window with title, use JWindow instead frame.setSize(new IntDimension( 200, 120 ) ); //create a button var button:JButton = new JButton("Hello from AsWing in Haxe!"); //add the button to the frame frame.getContentPane().append(button); //add the frame to the stage Lib.current.stage.addChild(this); this.stage.scaleMode = StageScaleMode.NO_SCALE; //show it! frame.show(); } public static function main() { new Hello(); } }
Now let's compile it by executing:
>haxe Hello.hxml
When everything went fine, you should have a flash file Hello.swf displaying a frame with a button in it, saying Hello!
Using LAFs
Since haxe 2.07 it is possible to use AsWing Look And Feel files easily
- Go into AsWing directory and extract LAF file from *.swc like described in previous steps
- Rename and copy LAF swf into your project directory
- add it using -swf-lib (e.g. -swf-lib orangelaf.swf)
- just add the line with LAF constructor before creating a JFrame or JWindow:
UIManager.setLookAndFeel(new OrangeLookAndFeel());
Tips
- use GUIBuilder application from downloaded AsWing package to design complex interfaces. Though it is a powerful tool and can produce Haxe code, you'll have to make this code work.
- If you are using FlashDevelop, you can add compiler option
-swf-lib library.swfintoAdditional compiler optionslist, which could be found inProject - Properties - Compiler optionsmenu