《Image Effects 》第一章学习笔记(2)

本文介绍了使用Flash API绘制直线的方法,通过具体实例演示了如何利用moveTo、lineTo等函数结合鼠标事件实现交互式绘图。读者将了解到如何设置线条样式、响应鼠标操作并实时更新画面。

  现在有一些时间,继续把之前那本英语书As图像处理写一下记录。在Image Effect 第一章里面是主要介绍几种Api的不同功能,并演示了不同API的的做法。现在开始,粘贴上第一个书里面的编码。

 1。绘制一条直线,主要使用的API 包括moveTo 和 lineTo 、lineStyle,clear这四个函数。

moveTo 和lineTo 都有两个参数,而两个参数都是记录坐标点。(x,y)

moveTo(x:Number,y:Number):void;

moveTo这个函数就像一支笔,一开始绘制画面的落脚点。

lineTo(x:Number,y:Number):void// 绘制一条直线,从笔的当前点,到一个新的点。

lineStyle(.....)这个函数,可以设置线条的样式。包括颜色,透明度,粗细等方式。

clear():void 清除画面

在这本书第一个程序当中,我们可以使用鼠标来绘制不同的线条。这些线条会产生不同的颜色。当鼠标按下去的时候,就像一支笔那样进行绘制。当放开鼠标的时候,线条就会结束。通过简单鼠标交互。就能编写出一个画线的程序了。

下面是源代码:可以到

http://www.friendsofed.com/downloads.html  这个网站下载到这本书给出的内容。

  package { import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Point; [SWF(width=550, height=400, backgroundColor=0xFFFFFF)] /** * Demonstrates how to draw straight lines with the drawing API. */ public class DrawingStraightLines extends Sprite { private var _currentShape:Shape; private var _color:uint; private var _startPosition:Point; /** * Constructor. Sets up listeners for mouse events. */ // 在构造函数里面,建立起鼠标事件的监听 public function DrawingStraightLines() { stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp); } /** * Draws a line from the start position to the current mouse position. */ //绘制直线 private function drawLine():void { _currentShape.graphics.clear(); _currentShape.graphics.lineStyle(3, _color); _currentShape.graphics.moveTo(_startPosition.x, _startPosition.y); _currentShape.graphics.lineTo(stage.mouseX, stage.mouseY); } /** * Handler for when the stage is clicked. This creates a new shape for the current line, * saves the starting posiiton and sets up a listener for when the mouse moves. * * @param event Event dispatched by the stage. */ //鼠标按下的时候记录该鼠标的点和随机设置线条的颜色 private function onStageMouseDown(event:MouseEvent):void { _color = Math.random()*0xFFFFFF; _currentShape = new Shape(); addChild(_currentShape); _startPosition = new Point(stage.mouseX, stage.mouseY); stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } /** * Handler for when the mouse is released. This removes the listener for when the mouse moves. * * @param event Event dispatched by the stage. */ //鼠标不按下的时候,取消鼠标移动的监听 private function onStageMouseUp(event:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } /** * Handler for when the mouse moves. This updates the drawn line based on the current mouse position. * * @param event Event dipsatched by the stage. */ //鼠标移动的时候进行拖动绘制曲线 private function onStageMouseMove(event:MouseEvent):void { drawLine(); event.updateAfterEvent(); } } }

下面就是一副使用绘制线条画出的效果。不是直接的结果。通过这个程序,我们主要是了解以上四种的API的功能。组合我们希望的程序。

下面接下去,继续探讨第一章的内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值