Using AsWing/as3 with Haxe

This simple tutorial explains how to use the powerfull AsWing GUI framework within Haxe
This tutorial covers AsWing 2.0 port HaxeAsWing and Haxe 2.09/NME 3.31, for older versions see history

Download AsWing


Setup a Test Project

Now let's start a new haxe project for testing.

  • Copy HaxeAsWing source code into your project directory.
  • Create a file Hello.hxml:
    -swf Hello.swf
    -swf-header 600:400:21:ffffff
    -main Hello
    -swf-version 9

Create a file Hello.hx:

import org.aswing.JButton;
import org.aswing.JFrame;
import org.aswing.geom.IntDimension;
import org.aswing.AsWingManager;

import flash.Lib;
import flash.display.Sprite;
import flash.display.StageScaleMode;

class Hello extends Sprite {
    public function new() {
        super();
    //reset ui to its defaults
    AsWingManager.initAsStandard(flash.Lib.current);
        //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 Look And Feel


Old LAFs are not compatible with AsWing 2.0, but you can easily customize new default LAF as in example below
...
import org.aswing.UIDefaults;
import org.aswing.UIManager;
import org.aswing.plaf.ASColorUIResource;
import org.aswing.plaf.basic.BasicLookAndFeel;

class MyLAF extends BasicLookAndFeel
{
    override private function initComponentDefaults(table:UIDefaults):Void{  
        super.initComponentDefaults(table);
        var comDefaults = [
            "Button.background", new ASColorUIResource(0x000000),
        ];
        table.putDefaults(comDefaults);
    }
}

class Hello extends Sprite {
    public function new() {
        super();
        //create a frame
        AsWingManager.initAsStandard(flash.Lib.current);
        UIManager.setLookAndFeel(new MyLAF());
        ...
    }
}

This code makes the button to be black. For more information look into org/aswing/plaf/basic/BasicLookAndFeel.hx

Tips

  • use GUIBuilder application from downloaded AsWing package to design complex interfaces. Though it is a powerful tool and can produce Haxe code, last version is not fully compatible and you'll have to make this code work.
version #13841, modified 2012-04-24 04:17:28 by paling