Posts Tagged ‘BitmapData’

Using filters to aid logic calculations

Wednesday, February 20th, 2008

With the introduction of flash 8 we had the ability to manipulate bitmaps with filters. Flash player 9 optimises the calculations of these filters and flash player 10 promises to preform these calculations on the clients graphics hardware. I believe that there are many non-graphic applications of these technologies such as path finding.

In the example below I have investigated using a simple convolution filter to expand the area of colour by one pixel. Repetitively applying this filter allowed me to fill a maze like pouring a can of paint in a mouse maze. Once the destination is reached it is a simple process to back track and evaluate the route taken. This route will be the fastest possible.

maze2.zip

Simple Drawing in AS3 revisited

Monday, February 18th, 2008

This code draws with a predefined brush “brush” onto the screen when the mouse is down and moving. A colour picker allows you to modify the colour. 

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.geom.ColorTransform
import fl.controls.ColorPicker;
import fl.events.ColorPickerEvent;
var ct:ColorTransform = new ColorTransform();
var myBitmapData:BitmapData = new BitmapData(200,200, false, 0xFFCCCCCC);
var myBitmap:Bitmap = new Bitmap(myBitmapData,”auto”,false);
var canvas:Sprite = new Sprite();
var size:Number = brush.width;
var offSet:Number = Math.floor(size/2);
var translateMatrix:Matrix = new Matrix()
var brushData:BitmapData = new BitmapData(int(brush.width), int(brush.height), true, 0×00FF0000);
init();
function init(){
 brushData.draw(brush, new Matrix, new ColorTransform, “normal”, brushData.rect, false);
 brushColour.addEventListener(ColorPickerEvent.CHANGE,updateColour);
 canvas.addChild(myBitmap);
 addChild(canvas)
 canvas.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
 canvas.addEventListener(MouseEvent.MOUSE_UP, endDraw);
 canvas.addEventListener(MouseEvent.ROLL_OUT, endDraw);
 brushColour.selectedColor = 0xFFFFFF;
}
function updateColour(e:Event){
 ct.color = brushColour.selectedColor;
}
function startDraw(event:MouseEvent):void {
 canvas.addEventListener(MouseEvent.MOUSE_MOVE, drawPoint);
}
function endDraw(event:MouseEvent):void {
 canvas.removeEventListener(MouseEvent.MOUSE_MOVE, drawPoint);
}
function drawPoint(event:MouseEvent):void {
 translateMatrix.tx = canvas.mouseX-offSet;
 translateMatrix.ty = canvas.mouseY-offSet;
 myBitmapData.draw(brushData,translateMatrix,ct,null,null,false)
}

Simple Drawing in AS3

Saturday, February 16th, 2008

This code draws blue blocks onto the screen when the mouse is down and moving.

import flash.display.BitmapData;
import flash.geom.Rectangle;

var myBitmapData:BitmapData = new BitmapData(200,200, false, 0xFFCCCCCC);
var myBitmap:Bitmap = new Bitmap(myBitmapData,”auto”,false);
var maze:Sprite = new Sprite();
var size:Number = 10;
var offSet:Number = Math.floor(size/2);

maze.addChild(myBitmap);
addChild(maze)
maze.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
maze.addEventListener(MouseEvent.MOUSE_UP, endDraw);
maze.addEventListener(MouseEvent.ROLL_OUT, endDraw);

function startDraw(event:MouseEvent):void {
 maze.addEventListener(MouseEvent.MOUSE_MOVE, drawPoint);
}
function endDraw(event:MouseEvent):void {
 maze.removeEventListener(MouseEvent.MOUSE_MOVE, drawPoint);
}
function drawPoint(event:MouseEvent):void {
 var rect:Rectangle = new Rectangle(maze.mouseX-offSet, maze.mouseY-offSet,size,size)
 myBitmapData.fillRect(rect,0xFF0000FF);
}