月曜日, 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もできますが、これでは折りたたんだときに表示が残るので。。