月曜日, 1月 11, 2010

[haXe] aswingを使う(ウィンドウ内に描画)

同様にhaXeにてaswingのウィンドウ内に絵を描く(linetoなど)には、FrameのContainerをとって、そこに、絵が描かれたGroudDecoratorをBackgroundDecoratorとしてセット、するみたいです。。。中国のaswingサイトに例がありました。
import flash.display.Sprite;

import org.aswing.ASColor;
import org.aswing.Component;
import org.aswing.Container;
import org.aswing.JFrame;
import org.aswing.graphics.Graphics2D;
import org.aswing.graphics.IBrush;
import org.aswing.graphics.SolidBrush;

class FillTest extends Sprite
{
public function new()
{
super();
var frame:JFrame = new JFrame(this);
frame.setSizeWH(400, 370);

var c:Container = frame.getContentPane();
c.setBackgroundDecorator(new MyCanvas());

frame.show();
}
}

import org.aswing.GroundDecorator;
import flash.display.Shape;
import org.aswing.Component;
import org.aswing.graphics.Graphics2D;
import org.aswing.geom.IntRectangle;
import flash.display.DisplayObject;
import org.aswing.graphics.IBrush;
import org.aswing.graphics.SolidBrush;
import org.aswing.ASColor;
import org.aswing.graphics.GradientBrush;

import flash.geom.Matrix;

class MyCanvas implements GroundDecorator {

private var shape:Shape;

private var H_GAP:UInt;
private var V_GAP:UInt;

private var WIDTH:UInt;
private var HEIGHT:UInt;

public function new()
{
shape = new Shape();

H_GAP = 10;
V_GAP = 10;

WIDTH = 100;
HEIGHT = 100;
}

public function updateDecorator(com:Component, g:Graphics2D, bounds:IntRectangle):Void {

var g2:Graphics2D = new Graphics2D(this.shape.graphics);
g2.clear();

var rectBounds:IntRectangle = new IntRectangle();

//fill solid rect

rectBounds.x = bounds.x + H_GAP;
rectBounds.y = bounds.y + V_GAP;
rectBounds.width = WIDTH;
rectBounds.height = HEIGHT;

var solidBrush:IBrush = new SolidBrush(ASColor.RED);
g2.fillRectangle(solidBrush, rectBounds.x, rectBounds.y, rectBounds.width, rectBounds.height);

//fill liner grandient rect

rectBounds.y += HEIGHT; // move shape rect
rectBounds.y += V_GAP;

var colors:Array<UInt> = [0x000000, 0xFF0000, 0x00FF00, 0x0000FF, 0x000000];
var alphas:Array<Int> = [0, 1, 1, 1, 0];
var ratios:Array<Int> = [0x00, 0x3F, 0x7E, 0xBD, 0xFF];
var matrix:Matrix = new Matrix();

matrix.createGradientBox(rectBounds.width, rectBounds.height, 0, rectBounds.x, rectBounds.y);
var linear:IBrush = new GradientBrush(GradientBrush.LINEAR, colors, alphas, ratios, matrix);

g.fillRectangle(linear, rectBounds.x, rectBounds.y, rectBounds.width, rectBounds.height);

//fill radial grandient

rectBounds.y += HEIGHT; // move shape rect
rectBounds.y += V_GAP;

matrix.createGradientBox(rectBounds.width, rectBounds.height, 0, rectBounds.x, rectBounds.y);
var radial:IBrush = new GradientBrush(GradientBrush.RADIAL, colors, alphas, ratios, matrix);

g.fillRectangle(radial, rectBounds.x, rectBounds.y, rectBounds.width, rectBounds.height);
}

public function getDisplay(c:Component):DisplayObject {
return this.shape;
}
}
普通にSpriteをJFrameなどにaddChildもできますが、これでは折りたたんだときに表示が残るので。。

[haXe] aswingを使う


haXe(v2.04)でaswing(1_5というパック?)を使う例。
/**
 * Sample002.as
 * Thanks!! -> http://www.matzmtok.com/blog/?p=18
 */

import org.aswing.event.InteractiveEvent ;
import org.aswing.ASColor;
import org.aswing.Container;
import org.aswing.EmptyLayout;
import org.aswing.JSlider;
import org.aswing.JWindow;
import org.aswing.SoftBoxLayout;
import org.aswing.ButtonGroup;
import org.aswing.BoxLayout;
import org.aswing.BorderLayout;
import org.aswing.JLabel;
import org.aswing.JFrame;
import org.aswing.JPanel;
import org.aswing.JButton;
import org.aswing.JRadioButton;

import org.aswing.geom.IntPoint;
import org.aswing.geom.IntDimension;

class Sample002 extends JWindow{

private var rbX:JRadioButton;
private var rbY:JRadioButton;
private var group:ButtonGroup;
private var zzlayout:BoxLayout;
private var frame:JFrame;

public var bg:ButtonGroup;
public var title:String;

public function new() {
super(flash.Lib.current, true);
title = 'Example of BoxLayout';
initUI();
}

private function initUI():Void {
setBackground(new ASColor(0xffffff));

var rbPanel:JPanel = new JPanel();
rbX = new JRadioButton('BoxLayout.X_AXIS');
rbY = new JRadioButton('BoxLayout.Y_AXIS');
zzlayout = new BoxLayout();
rbX.addSelectionListener(onRadioChanged);
rbY.addSelectionListener(onRadioChanged);
rbX.setSelected(true);
group = new ButtonGroup();
group.append(rbX);
group.append(rbY);

rbPanel.append(new JLabel('方向'));
rbPanel.append(rbX);
rbPanel.append(rbY);
rbPanel.setLocation(new IntPoint(0, 0));
rbPanel.setSize(new IntDimension(400, 30));

getContentPane().append(rbPanel);

var gapPanel:JPanel = new JPanel();
gapPanel.append(new JLabel('間隔'));

var slider:JSlider = new JSlider(JSlider.HORIZONTAL, 0, 30, 0);
slider.addStateListener(onSliderChange);
gapPanel.append(slider);

gapPanel.setLocation(new IntPoint(0, 20));
gapPanel.setSize(new IntDimension(400, 30));
gapPanel.setBackground(new org.aswing.ASColor(0xFF00FF)); // 効かない?
gapPanel.setToolTipText("xxxTIPxxx");
getContentPane().append(gapPanel);

frame = new JFrame(this, title);
getContentPane().append(frame);

frame.getContentPane().setLayout(zzlayout);
frame.setLocation(new IntPoint(30, 50));
frame.setSize(new IntDimension(250, 150));

// たくさん並べるとよしなに幅や高さを等分してくれる
frame.getContentPane().append(new JButton('Button 1'));
frame.getContentPane().append(new JButton('Button 2'));
frame.getContentPane().append(new JButton('Button 3'));
frame.getContentPane().append(new JButton('Button 4'));

frame.show();
}

public function onRadioChanged(e:InteractiveEvent):Void {
var rb:JRadioButton = cast(e.target,JRadioButton);

switch(rb){
    case rbX:
zzlayout.setAxis(BoxLayout.X_AXIS);
    case rbY:
zzlayout.setAxis(BoxLayout.Y_AXIS);
    default:
trace("cannot change layout!");
}
updateLayout();
}

public function onSliderChange(e:InteractiveEvent):Void {
var s:JSlider = cast(e.target,JSlider);
zzlayout.setGap(s.getValue());
updateLayout();
}

private function updateLayout():Void {
frame.getContentPane().setLayout(zzlayout);
}
}

これを、以下のように呼び出す。
import org.aswing.geom.IntPoint;
import org.aswing.geom.IntDimension;

class Main
{
    static function main()
    {
var app:Sample002 = new Sample002();
app.setLocation(new IntPoint(0, 0));
app.setSize(new IntDimension(500, 500));
app.show();

//        flash.Lib.current.addChild(new Sample002());
    }
}

ちなみに、haxeコマンドに与えるcompile.hxmlはこんな感じ。
-swf-header 500:500:20:FFFFFF
-swf Main.swf
-cp hxclasses
-swf-lib concat.swf
-swf-version 9
-main Main

水曜日, 1月 06, 2010

[haXe] haxelibが変?

haXeでは、swfにしたアセットライブラリは、"-swf-lib"オプションに指定するのだが、これが1つしか受け付けられない様子。画像ファイルのswfとflコンポーネントのswfを同時に使いたかったので、この2つのアセットswfを纏める方法。

以下xmlを用意(ここではconcat.xmlとした)。
<movie width="320" height="240" framerate="12" version="9">
<frame><library>
<clip id="foo" import="spectrum.swf"/>
<clip id="foo" import="component.swf"/>
</library></frame>
</movie>

そして、swfmillを実行。
> swfmill.exe simple concat.xml concat.swf

意外に簡単!xmlのwidthとかは適当で良いようです。