▼ActionScript 3.0メモ▼
ビットマップの利用


ビットマップを利用するFlashアプリを作成する

素材の準備
以下の画像をソースコードと同じ場所に配置。

sample.gif


ソースコード

BitmapEx.as
package {
    import flash.display.Bitmap;
    import flash.display.Sprite;
    
    //ビットマップの利用
    [SWF(width=240, height=240, backgroundColor=0xFFFFFF)]
    public class BitmapEx extends Sprite {
        //画像
        [Embed(source='sample.gif')]
        private var GIF_FILE:Class;		
        
        //変数
        private var g:Graphics;

        //コンストラクタ
        public function BitmapEx() {
            //グラフィックス
            g = new Graphics(240, 240);			
            addChild(new Bitmap(g.getBitmapData()));			
                                  
            //ラインの描画
            g.setColor(0xff3333);
            g.drawLine(25, 5, 25, 45);			

            //ポリラインの描画
            var polylinePoint:Array = [[55, 5], [85, 10], [65, 25], [95, 40], [55, 45]];			
            g.setColor(0xff3333);
            g.drawPolyline(polylinePoint);

            //ポリゴンの描画
            var polygonPoint:Array = [[105, 5], [135, 10], [115, 25], [145, 40], [105, 45]];			
            g.setColor(0xff3333);
            g.fillPolygon(polygonPoint);

            //矩形の描画
            g.setColor(0x55ff55);
            g.drawRect(5, 55, 40, 40);			

            //矩形の塗り潰し
            g.setColor(0x55ff55);
            g.fillRect(55, 55, 40, 40);			

            //角丸矩形の描画
            g.setColor(0x55ff55);
            g.drawRoundRect(105, 55, 40, 40, 30);			

            //角丸矩形の塗り潰し
            g.setColor(0x55ff55);
            g.fillRoundRect(155, 55, 40, 40, 30);			
            
            //円の描画
            g.setColor(0x5555ff);
            g.drawCircle(25, 125, 20);			

            //円の塗り潰し
            g.setColor(0x5555ff);
            g.fillCircle(75,125,20);
            
            //文字列の描画
            g.setColor(0xff3333);
            g.setFontSize(12);
            g.drawString("12dot Font", 105, 105 + 12);			
            g.setFontSize(16);
            g.drawString("16dot Font", 105, 105 + 16 + 14);			
            
            //イメージの描画
            var imgSample:Bitmap = new GIF_FILE();			
            g.drawImage(imgSample, 5, 155);			
        }
    }
}


Graphics.as
package {
    import flash.display.*;
    import flash.geom.*
    import flash.system.*;
    import flash.text.*;
    
    //グラフィックス
    public class Graphics {
        //色定数
        public static const 
            AQUA:int    = (  0 << 16) + (255 << 8) + 255,			
            BLACK:int   = (  0 << 16) + (  0 << 8) +   0,			
            BLUE:int    = (  0 << 16) + (  0 << 8) + 255,			
            FUCHSIA:int = (255 << 16) + (  0 << 8) + 255,			
            GRAY:int    = (128 << 16) + (128 << 8) + 128,			
            GREEN:int   = (  0 << 16) + (128 << 8) +   0,			
            LIME:int    = (  0 << 16) + (255 << 8) +   0,			
            MAROON:int  = (128 << 16) + (  0 << 8) +   0,			
            NAVY:int    = (  0 << 16) + (  0 << 8) + 128,			
            OLIVE:int   = (128 << 16) + (128 << 8) +   0,			
            PURPLE:int  = (128 << 16) + (  0 << 8) + 128,			
            RED:int     = (255 << 16) + (  0 << 8) +   0,			
            SILVER:int  = (192 << 16) + (192 << 8) + 192,			
            TEAL:int    = (  0 << 16) + (128 << 8) + 128,			
            WHITE:int   = (255 << 16) + (255 << 8) + 255,			
            YELLOW:int  = (255 << 16) + (255 << 8) +   0;			

        //変数
        private var bd       :BitmapData;//BMPデータ
        private var color    :int;       //色
        private var label    :TextField; //ラベル
        private var format   :TextFormat;//フォーマット
        private var fontSize :Number;    //フォントサイズ
        private var lineWidth:Number;    //ライン幅


//====================
//初期化
//====================
        //コンストラクタ
        public function Graphics(w:int, h:int) {           			
            //BMPデータ
            bd = new BitmapData(w, h, false, 0xffffff);			

            //色
            color = 0x000000; 			

            //ラベル
            label = new TextField();			
            label.autoSize = TextFieldAutoSize.LEFT;			

            //フォーマット
            format = new TextFormat();			
            format.font = "_等幅";			
            format.color = 0x000000;			
            setFontSize(12);
            
            //ライン幅
            lineWidth=1;                       
        }


//====================
//アクセス
//====================
        //フォントサイズの指定
        public function setFontSize(fontSize:int):void {			
            this.fontSize = fontSize;			
            format.size = fontSize * Capabilities.screenDPI / 72;			
        }      
        
        //フォントサイズの取得
        public function getFontSize():Number {
            return fontSize;
        } 
        
        //フォント名の指定
        public function setFontName(fontName:String):void {
            format.font = fontName;			
            setFontSize(fontSize);
        }
        
        //フォント名の取得
        public function getFontName():String {
            return format.font;
        }

        //文字列幅の取得
        public function stringWidth(text:String):int {
            label.text = text;			
            label.setTextFormat(format);
            return label.textWidth; 
        }

        //文字列高さの取得
        public function stringHeight(text:String):int {
            label.text = text;			
            label.setTextFormat(format);
            return label.textHeight; 
        }
        
        //BMPデータの取得
        public function getBitmapData():BitmapData {
            return bd;
        }

        //色の指定
        public function setColor(color:int):void {
            this.color = color;			
            format.color=color;
        }

        //色の取得
        public static function getColorOfRGB(r:int, g:int, b:int):int {			
            return (r << 16) + (g << 8) + b;			
        }
        
        //色の取得
        public static function getColorOfName(name:int):int {
            return name;
        }     
        
        //ライン幅の指定
        public function setLineWidth(lineWidth:int):void {
            this.lineWidth = lineWidth;			
        }   
        
        //ライン幅の取得
        public function getLineWidth():int {
            return lineWidth;
        }     
        
 
//====================
//描画
//====================
        //ラインの描画
        public function drawLine(x0:int, y0:int, x1:int, y1:int):void {			
            var line:Shape = new Shape();			
            line.graphics.lineStyle(lineWidth, color);			
            line.graphics.moveTo(x0, y0);			
            line.graphics.lineTo(x1, y1); 			
            bd.draw(line);			
        }

        //ポリラインの描画
        public function drawPolyline(point:Array):void {
            var polyline:Shape = new Shape();			
            polyline.graphics.lineStyle(lineWidth, color);			
            polyline.graphics.moveTo(point[0][0], point[0][1]);			
            for (var i:int = 1; i < point.length; i++) {				
                 polyline.graphics.lineTo(point[i][0], point[i][1]);				 
            }
            bd.draw(polyline);			
        }

        //ポリゴンの塗り潰し
        public function fillPolygon(point:Array):void {			
            var polygon:Shape = new Shape();            		
            polygon.graphics.beginFill(color);			
            polygon.graphics.moveTo(point[0][0], point[0][1]);			
            for (var i:int = 1; i < point.length; i++) {				
                 polygon.graphics.lineTo(point[i][0], point[i][1]);				 
            }
            polygon.graphics.endFill();			
            bd.draw(polygon);
        }

        //矩形の描画
        public function drawRect(x:int, y:int, w:int, h:int):void {			
            var rect:Shape = new Shape();			
            rect.graphics.lineStyle(lineWidth, color);			
            rect.graphics.drawRect(x, y, w, h);			
            bd.draw(rect);
        }
        
        //矩形の塗り潰し
        public function fillRect(x:int, y:int, w:int, h:int):void {			
            var rect:Shape = new Shape();			
            rect.graphics.beginFill(color);			
            rect.graphics.drawRect(x, y, w, h);			
            rect.graphics.endFill();			
            bd.draw(rect);
        }

        //角丸矩形の描画
        public function drawRoundRect(x:int, y:int, w:int, h:int, r:int):void {			
            var rect:Shape = new Shape();			
            rect.graphics.lineStyle(lineWidth, color);			
            rect.graphics.drawRoundRect(x, y, w, h, r);			
            bd.draw(rect);
        }
        
        //角丸矩形の塗り潰し
        public function fillRoundRect(x:int, y:int, w:int, h:int, r:int):void {			
            var rect:Shape = new Shape();			
            rect.graphics.beginFill(color);			
            rect.graphics.drawRoundRect(x, y, w, h, r);			
            rect.graphics.endFill();
            bd.draw(rect);
        }

        //円の描画
        public function drawCircle(x:int, y:int, r:int):void {			
            var circle:Shape = new Shape();			
            circle.graphics.lineStyle(lineWidth, color);			
            circle.graphics.drawCircle(x, y, r);			
            bd.draw(circle);
        }
        
        //円の塗り潰し
        public function fillCircle(x:int,y:Number,r:Number):void {
            var circle:Shape=new Shape();
            circle.graphics.beginFill(color);
            circle.graphics.drawCircle(x,y,r);
            bd.draw(circle);
        }
        
        //文字列の描画
        public function drawString(text:String, x:int, y:int):void {			
            if (text == null) return;			
            label.text = text;			
            label.setTextFormat(format);			
            var trans:Matrix = new Matrix();
			trans.translate(
                x - (label.width -label.textWidth) / 2,				
                y - (label.height - label.textHeight) / 2);				
            bd.draw(label, trans);     			
        }
        
        //イメージの描画
        public function drawImage(source:DisplayObject, x:int, y:int):void {			
            if (source == null) return;			
            var trans:Matrix = new Matrix();
            trans.translate(x,y);
            bd.draw(source, trans);			
        }
    }
}



−戻る−