0% found this document useful (0 votes)
47 views

Creating A Drawing App With Flash

This document contains code for creating a drawing app with Adobe Flash CS4. It includes functions for pencil, eraser, and text tools that allow drawing on a board movie clip. It also includes functions for saving drawings as PNG files, clearing the board, selecting colors from a color picker, and changing the brush size. The main function initializes variables, adds event listeners to the tools, and hides tool highlights initially.

Uploaded by

Debasish Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Creating A Drawing App With Flash

This document contains code for creating a drawing app with Adobe Flash CS4. It includes functions for pencil, eraser, and text tools that allow drawing on a board movie clip. It also includes functions for saving drawings as PNG files, clearing the board, selecting colors from a color picker, and changing the brush size. The main function initializes variables, adds event listeners to the tools, and hides tool highlights initially.

Uploaded by

Debasish Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Creating a Drawing App with Flash CS4

13
/* Pencil Tool shape, everything drawn with this tool and the eraser tool is
stored inside board.pencilDraw */
var pencilDraw:Shape = new Shape();
/* Text format */
var textformat:TextFormat = new TextFormat();
/* Colors */
var colorsBmd:BitmapData; //We'll use this Bitmap Data to get the pixel RGB
Value when clicked
var pixelValue:uint;
var activeColor:uint = 0x000000; //This is the current color in use,
displayed by the shapeSize MC
/* Save dialog instance */
var saveDialog:SaveDialog;
/* Active var, to check which tool is active */
var active:String;
/* Shape size color */
var ct:ColorTransform = new ColorTransform();

14
public function Main():void
{
textformat.font = "Quicksand Bold Regular"; // You can use any font you
like
textformat.bold = true;
textformat.size = 16;
/* We create these functions later */
convertToBMD();
addListeners();
/* Hide tools highlights */

pencil.visible = false;
hideTools(eraser, txt);
}

16
private function PencilTool(e:MouseEvent):void
{
/* Quit active tool */
quitActiveTool(); //This function will be created later
/* Set to Active */
active = "Pencil"; //Sets the active variable to
"Pencil"
/* Adds the listeners to the board MovieClip, to draw just in it */
board.addEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);
board.addEventListener(MouseEvent.MOUSE_UP, stopPencilTool);
/* Highlight, sets the Pencil Tool Icon to the color version, and hides
any other tool */
highlightTool(pencil);
hideTools(eraser, txt);
/* Sets the active color variable based on the Color Transform value and
uses that color for the shapeSize MovieClip */
ct.color = activeColor;
shapeSize.transform.colorTransform = ct;
}

-------private function startPencilTool(e:MouseEvent):void


{
pencilDraw = new Shape(); //We add a new shape to draw always in top (in
case of text, or eraser drawings)
board.addChild(pencilDraw); //Add that shape to the board MovieClip
pencilDraw.graphics.moveTo(mouseX, mouseY); //Moves the Drawing Position
to the Mouse Position
pencilDraw.graphics.lineStyle(shapeSize.width, activeColor);//Sets the
line thickness to the ShapeSize MovieClip size and sets its color to the
current active color
board.addEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool); //Adds a
listener to the next function

--------private function drawPencilTool(e:MouseEvent):void


{
pencilDraw.graphics.lineTo(mouseX, mouseY); //Draws a line from the
current Mouse position to the moved Mouse position
}

---------private function stopPencilTool(e:MouseEvent):void


{
board.removeEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool); //Stops
the drawing
}

17
private function EraserTool(e:MouseEvent):void
{
/* Quit active tool */
quitActiveTool();
/* Set to Active */
active = "Eraser";
/* Listeners */
board.addEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);
board.addEventListener(MouseEvent.MOUSE_UP, stopEraserTool);
/* Highlight */
highlightTool(eraser);
hideTools(pencil, txt);
/* Use White Color */
ct.color = 0x000000;
shapeSize.transform.colorTransform = ct;
}
private function startEraserTool(e:MouseEvent):void
{
pencilDraw = new Shape();
board.addChild(pencilDraw);

pencilDraw.graphics.moveTo(mouseX, mouseY);
pencilDraw.graphics.lineStyle(shapeSize.width, 0xFFFFFF); //White Color
board.addEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);
}
private function drawEraserTool(e:MouseEvent):void
{
pencilDraw.graphics.lineTo(mouseX, mouseY);
}
function stopEraserTool(e:MouseEvent):void
{
board.removeEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);
}

18
private function TextTool(e:MouseEvent):void
{
/* Quit active tool */
quitActiveTool();
/* Set to Active */
active = "Text";
/* Listener */
board.addEventListener(MouseEvent.MOUSE_UP, writeText);
/* Highlight */
highlightTool(txt);
hideTools(pencil, eraser);
}
private function writeText(e:MouseEvent):void
{
/* Create a new TextField Object, this way we won't replace older
instances */
var textfield = new TextField();
/* Set Formats, position, and focus */
textfield.type = TextFieldType.INPUT;
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.defaultTextFormat = textformat;
textfield.textColor = activeColor;
textfield.x = mouseX;

textfield.y = mouseY;
stage.focus = textfield;
/* Add text to Board */
board.addChild(textfield);
}

19
private function export():void
{
var bmd:BitmapData = new BitmapData(600, 290);//Creates a new BitmapData
with the board size
bmd.draw(board);//Draws the board MovieClip into a BitmapImage in the
BitmapData
var ba:ByteArray = PNGEncoder.encode(bmd); //Creates a ByteArray of the
BitmapData, encoded as PNG
var file:FileReference = new FileReference(); // Instantiates a new File
Reference Object to handle the save
file.addEventListener(Event.COMPLETE, saveSuccessful); //Adds a new
listener to listen when the save is complete
file.save(ba, "MyDrawing.png"); //Saves the ByteArray as a PNG
}
private function saveSuccessful(e:Event):void
{
saveDialog = new SaveDialog();// Instantiates a new SaveDialog Class
addChild(saveDialog); //Adds the SaveDialog MovieClip to the Stage
saveDialog.closeBtn.addEventListener(MouseEvent.MOUSE_UP,
closeSaveDialog);//Adds a listener to the close button of the dialog
}
private function closeSaveDialog(e:MouseEvent):void
{
removeChild(saveDialog); //Removes the dialog of the Stage
}
private function save(e:MouseEvent):void
{
export(); //Calls the export function to begin the saving process
}

20

private function clearBoard(e:MouseEvent):void


{
/* Create a white rectangle on top of everything */
var blank:Shape = new Shape();
blank.graphics.beginFill(0xFFFFFF);
blank.graphics.drawRect(0, 0, board.width, board.height);
blank.graphics.endFill();
board.addChild(blank);
}

21
private function convertToBMD():void
{
colorsBmd = new BitmapData(colors.width,colors.height);
colorsBmd.draw(colors);
}
private function chooseColor(e:MouseEvent):void
{
pixelValue = colorsBmd.getPixel(colors.mouseX,colors.mouseY);//Gets the
cliked pixel RGB value
activeColor = pixelValue;
/* Use a ColorTransform object to change the shapeSize MovieClip color */
ct.color = activeColor;
shapeSize.transform.colorTransform = ct;
}

22
private function quitActiveTool():void
{
switch (active)
{
case "Pencil" :
board.removeEventListener(MouseEvent.MOUSE_DOWN,
startPencilTool);
board.removeEventListener(MouseEvent.MOUSE_UP, stopPencilTool);
case "Eraser" :
board.removeEventListener(MouseEvent.MOUSE_DOWN,
startEraserTool);
board.removeEventListener(MouseEvent.MOUSE_UP, stopEraserTool);
case "Text" :
board.removeEventListener(MouseEvent.MOUSE_UP, writeText);
default :
}
}

------private function highlightTool(tool:DisplayObject):void


{
tool.visible=true; //Highlights tool in the parameter
}
/* Hides the tools in the parameters */
private function hideTools(tool1:DisplayObject, tool2:DisplayObject):void
{
tool1.visible=false;
tool2.visible=false;
}

23
private function changeShapeSize(e:MouseEvent):void
{
if (shapeSize.width >= 50)
{
shapeSize.width = 1;
shapeSize.height = 1;
/* TextFormat */
textformat.size = 16;
}
else
{
shapeSize.width += 5;
shapeSize.height=shapeSize.width;
/* TextFormat */
textformat.size+=5;
}
}

24
private function addListeners():void
{
pencilTool.addEventListener(MouseEvent.MOUSE_UP, PencilTool);
eraserTool.addEventListener(MouseEvent.MOUSE_UP, EraserTool);
textTool.addEventListener(MouseEvent.MOUSE_UP, TextTool);
saveButton.addEventListener(MouseEvent.MOUSE_UP, save);
clearTool.addEventListener(MouseEvent.MOUSE_UP, clearBoard);
colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
sizePanel.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);

shapeSize.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);
}

You might also like