<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>胡矿的Blog</title>
	<link>http://thiswind.flashseer.org</link>
	<description>一个人只能全力以赴，等待命运揭晓</description>
	<pubDate>Sat, 11 Oct 2008 05:19:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>
	<language>en</language>
			<item>
		<title>试验一下代码格式是否正确</title>
		<link>http://thiswind.flashseer.org/?p=30</link>
		<comments>http://thiswind.flashseer.org/?p=30#comments</comments>
		<pubDate>Sat, 11 Oct 2008 05:08:58 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=30</guid>
		<description><![CDATA[



import java.io.File; 
public class 遍历文件系统 { 
&#160;&#160;&#160; public static void main(String[] args) {           &#160;&#160;&#160;&#160;&#160;&#160;&#160; //遍历E盘            &#160;&#160;&#160;&#160;&#160;&#160;&#160; 遍历文件系统 dir = new 遍历文件系统();            &#160;&#160;&#160;&#160;&#160;&#160;&#160; dir.dir(new [...]]]></description>
			<content:encoded><![CDATA[<table cellspacing="0" cellpadding="2" width="800" border="1">
<tbody>
<tr>
<td valign="top" width="800">
<p>import java.io.File; </p>
<p>public class 遍历文件系统 { </p>
<p>&#160;&#160;&#160; public static void main(String[] args) {           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; //遍历E盘            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 遍历文件系统 dir = new 遍历文件系统();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; dir.dir(new File(&quot;e:/&quot;)); </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //遍历全部磁盘（包括软驱和光驱在内了）           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String[] diskNames = { &quot;A:/&quot;, &quot;B:/&quot;, &quot;C:/&quot;, &quot;D:/&quot;, &quot;E:/&quot;, &quot;F:/&quot;, &quot;G:/&quot;,            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;H:/&quot;, &quot;I:/&quot;, &quot;J:/&quot;, &quot;K:/&quot;, &quot;L:/&quot;, &quot;M:/&quot;, &quot;N:/&quot;, &quot;O:/&quot;, &quot;P:/&quot;,            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;Q:/&quot;, &quot;R:/&quot;, &quot;S:/&quot;, &quot;T:/&quot;, &quot;U:/&quot;, &quot;V:/&quot;, &quot;W:/&quot;, &quot;X:/&quot;, &quot;Y:/&quot;,            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;Z:/&quot; };            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int i=0; i&lt;26; i++) {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dir.dir(new File(diskNames[i]));            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; //如果是Linux系统，则这样既可：            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; //dir.dir(new File(&quot;/&quot;));//直接从根分区开始便利就行            <br />&#160;&#160;&#160; } </p>
<p>&#160;&#160;&#160; //递归遍历           <br />&#160;&#160;&#160; public void dir(File path) { </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (path == null || !path.exists() || path.isFile()) {           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //没有这个目录，或者这个路径是一个文件而不是文件夹，则返回            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return;            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } else {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //如果这个路径是一个文件夹，则列出文件夹中的所有文件，并且对每一个文件进行递归访问            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; File[] files = path.listFiles(); </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; File file = null;           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int len = files.length;            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int i = 0; i &lt; len; i++) {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; file = files[i];            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //输出文件的绝对路径            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println(file.getAbsolutePath());            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dir(file);            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }            <br />&#160;&#160;&#160; }            <br />}</p>
</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=30</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=27</link>
		<comments>http://thiswind.flashseer.org/?p=27#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:38:12 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=27</guid>
		<description><![CDATA[AsWing入门教程 3.1 模型-视图-控制器设计模式    作者：胡矿      著作权所有，请勿转载       www.flashseer.org     Google Doc http://docs.google.com/Doc?id=dnp8gdz_33f8dk27           模型-视图-控制器设计模式又被称为MVC。这个模式是将数据模型与显示界面分离，通过一个控制器来分别与数据模型和界面沟通和进行控制。 如图3.1-1.      （图3.2-1）       由于ActionScript3.0是事件驱动的强交互性语言，所以模型-视图-控制器设计模式在ActionScript3.0当中也会与在其他语言中的定义有所不同。   数据模型是一个事件源（EventDispatcher） [...]]]></description>
			<content:encoded><![CDATA[<p><P><STRONG>AsWing入门教程 3.1 模型-视图-控制器设计模式</STRONG></P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>作者：胡矿</STRONG> </P> <P> </P> <P> </P> <P><STRONG>著作权所有，请勿转载</STRONG> </P> <P> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG><A href="http://www.flashseer.org/"><FONT color=#810081>www.flashseer.org</FONT></A></STRONG>  </P> <P> </P> <P>Google Doc</P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_33f8dk27" target=_blank><FONT color=#810081>http://docs.google.com/Doc?id=dnp8gdz_33f8dk27</FONT></A></P> <P> </P> <P> </P> <P> </P> <P>    模型-视图-控制器设计模式又被称为MVC。这个模式是将数据模型与显示界面分离，通过一个控制器来分别与数据模型和界面沟通和进行控制。 如图3.1-1.</P> <P> </P> <P> </P> <DIV id=img_5191134003714855 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_35dgzzj6df"></DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">（图3.2-1）</DIV> <P> </P> <P>    由于ActionScript3.0是事件驱动的强交互性语言，所以模型-视图-控制器设计模式在ActionScript3.0当中也会与在其他语言中的定义有所不同。</P> <UL> <LI> <DIV>数据模型是一个事件源（EventDispatcher）</DIV> <LI> <DIV>视图是一个事件源（EventDispatcher）</DIV> <LI> <DIV>控制器里面你有两个通道</DIV></LI></UL> <P>         1.一个通道监听视图的变化，并且把变化写入到模型</P> <P>         2.一个通道监听模型的变化，并且把变化输出到视图</P> <P> </P> <P>    如图3.2-2。</P> <DIV id=img_6909267789305951 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"> <DIV id=img_69607494120803 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"> <DIV id=img_5839959651087561 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><A href="http://docs.google.com/File?id=dnp8gdz_39fdj7k2ck" target=_blank><IMG src="http://docs.google.com/File?id=dnp8gdz_39fdj7k2ck"></A></DIV>（图3.2-2）</DIV></DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">    当视图（View）被触动（比如鼠标点击或者键盘输入），视图将发出事件，这时候视图-模型通道（ViewToModelChannel）收到事件并且更新模型（Model）的内容。如图3.2-3所示。</DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"> <DIV id=img_4267295965444989 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_40ftv2hsq8"></DIV>（图3.2-3）</DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">    当模型（Model）发生改变，模型将发出事件，这时候模型-视图通道（ModelToViewChannel）收到事件并且更新视图（View)的显示。如图3.2-4所示。</DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"> <DIV id=img_7951990323423103 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_41c6twxzc6"></DIV>（图3.2-4）</DIV> <P> </P> <P>    下面是这个模式的代码表述：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG><FONT size=3><U>Model</U></FONT></STRONG>  <P><STRONG></STRONG> </P> <P><STRONG>package</STRONG><BR>{<BR>     <STRONG>import</STRONG> flash.events.EventDispatcher;</P> <P> </P> <P>     <STRONG>public</STRONG> <STRONG>class</STRONG> Model <STRONG>extends</STRONG> EventDispatcher<BR>     {<BR>     }<BR>}</P></BLOCKQUOTE> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG><FONT size=3><U>View</U></FONT></STRONG>  <P> </P> <P><STRONG>package</STRONG><BR>{<BR>     <STRONG>import</STRONG> flash.events.EventDispatcher;</P> <P> </P> <P>     <STRONG>public</STRONG> <STRONG>class</STRONG> View <STRONG>extends</STRONG> EventDispatcher<BR>     {<BR>     }<BR>}</P></BLOCKQUOTE> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG><FONT size=3><U>Controller</U></FONT></STRONG>  <P> </P> <P><STRONG>package</STRONG><BR>{<BR>     <STRONG>public</STRONG> <STRONG>class</STRONG> Controller<BR>     {<BR>          <STRONG>private</STRONG> <STRONG>var </STRONG>v2mChannel:Channel;<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> m2vChannel:Channel;<BR>  <BR>          <STRONG>public</STRONG> <STRONG>function</STRONG> Controller(view:View, model:Model) {<BR>               <STRONG>this</STRONG>.v2mChannel = <STRONG>new</STRONG> ViewToModelChannel(view, model);<BR>               <STRONG>this</STRONG>.m2vChannel = <STRONG>new</STRONG> ModelToViewChannel(view, model);<BR>          }<BR>     }<BR>}</P> <P> </P> <P><STRONG>class</STRONG> Channel {<BR>     <STRONG>protected</STRONG> <STRONG>var</STRONG> view:View;<BR>     <STRONG>protected</STRONG> <STRONG>var</STRONG> model:Model;<BR> <BR>     <STRONG>public</STRONG> <STRONG>function</STRONG> Channel(view:View, model:Model) {<BR>          <STRONG>this</STRONG>.view = view;<BR>          <STRONG>this</STRONG>.model = model;<BR>     }<BR>}</P> <P> </P> <P><STRONG>class</STRONG> ViewToModelChannel <STRONG>extends</STRONG> Channel {<BR> <BR>     <STRONG>public</STRONG> <STRONG>function</STRONG> ViewToModelChannel(view:View, model:Model) {<BR>          <STRONG>super</STRONG>(view, model);<BR>  <BR>          <EM><FONT color=#009900>//监听来自视图的事件</FONT></EM><BR>          <STRONG>this</STRONG>.view.addEventListener(&#8230;&#8230;..<BR>     }<BR>}</P> <P> </P> <P><STRONG>class</STRONG> ModelToViewChannel <STRONG>extends</STRONG> Channel {<BR> <BR>     <STRONG>public</STRONG> <STRONG>function</STRONG> ModelToViewChannel(view:View, model:Model) {<BR>          <STRONG>super</STRONG>(view, model);<BR>  <BR>          <EM><FONT color=#009900>//监听来自模型的事件<BR></FONT></EM>          <STRONG>this</STRONG>.model.addEventListener(&#8230;&#8230;.<BR>     }<BR>}</P></BLOCKQUOTE> <P> </P> <P>    下面，我们用一个简单的例子来介绍这个设计模式如何使用。在后面的章节当中，我们会大量使用这里所介绍的模型-视图-控制器设计模式。</P> <P> </P> <P>    如图3.2-5和3.2-6，这个程序有一个输出框架和一个输入框架。在输入框架增加或者减少模型的顶点，输出框架根据模型当中的顶点数绘制正多边形。</P> <DIV id=k_k9 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_42ffdnr2fn"></DIV> <P>（图3.2-5）</P> <P> </P> <DIV id=j7nl style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_43dcvqttdn"></DIV> <P>（图3.2-6）</P> <P> </P> <P> </P> <P>    在这个例子当中，模型定义了一个vectorChanged事件（VECTOR_CHANGED）和一个正整数（vectors）。定义了两个公开的方法来操纵这个正整数（getVectors、setVectors）。如图3.2-7。</P> <DIV id=efpw style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_44c29qkqfw"></DIV> <P>（图3.2-7）</P> <P> </P> <P>     模型对于外面发生的事情一无所知，它只知道在vectors改变的时候发布vectorChanged事件（VECTOR_CHANGED）。</P> <P> </P> <P> </P> <HR>  <P> </P> <P><STRONG>笔记：</STRONG></P> <P> 一个对象是由数据和方法所构成的，数据定义的是对象的状态，所以有的时候，数据又被叫做属性，比如例子当中的Model，数据域vectors表示这个Model目前是处在3个顶点的状态，或者是4个顶点的状态。</P> <P> 我们要强调的是对象当中的方法。对象当中的方法是对本对象自身的数据的操作。所以方法（Method）有时候又被叫做操作（Operation）。</P> <P> 记住这句话：<STRONG>数据（属性）是对象的状态，方法是对数据（属性）的操作。</STRONG></P> <P> </P> <HR>  <P> </P> <P> </P> <P> </P> <HR>  <P> </P> <P><STRONG> 笔记：</STRONG></P> <P>面向对象的一个基本概念是服务（Service）。</P> <P>当一个对象要访问另一个对象的时候（即改变或者读取另一个对象的状态），它只能通过那个对象所提供的服务来进行操作，在对象这个层面，服务就是方法（Method）。而一个对象要让别的对象可以操作自己的状态，就要定义操作自己数据的方法。</P> <P>注意：<STRONG>一个对象的方法只应该操作自己的数据，而不应该去操作其他对象的对象的数据。要操作别的对象，应该通过别的对象所提供的服务（Method）</STRONG></P> <P> </P> <HR>  <P> </P> <P> </P> <P> </P> <P>    视图是一个有两个框架（JFrame）的界面，它只提供了一个公开的方法update，这个方法接受一个正整数作为参数，并且将根据这个正整数来绘制一个正多边形。如图3.2-8。</P> <DIV id=v5tp style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_45fkmpgn26"></DIV> <P>（图3.2-8）</P> <P> </P> <P>    视图对于模型同样一无所知。它只知道代表顶点数目的一个正整数，而对这个数字从何而来，代表什么含义一无所知。在“添加顶点”（Add Vector）和“移除顶点”（Remove Vector）按钮被按下的时候，视图将发布addVector事件（ADD_VECTOR）和removeVector事件（REMOVE_VECTOR）。</P> <P> </P> <P>    控制器负责间监听分别来自模型和视图的事件。如图3.2-9。</P> <DIV id=n6ze style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_46zbkkrbf8"></DIV> <P>（图3.2-9）</P> <P> </P> <P>    控制器有两个通道（Channel），一个负责监听和处理来自模型的事件（ModelToViewChannel），另一个负责监听和处理来自视图的事件（ViewToModelChannel）。控制器对于视图将如何显示以及模型将如何保存数据一无所知，它只知道在收到特定的事件的时候对数据进行逻辑处理，并且在需要的时候通过模型或者视图所提供的方法改变他们的状态。</P> <P> </P> <P>    我们看到，模型-视图-控制器设计模式当中，每一个层面都只负责自己层面的处理逻辑，对于其他层面是如何进行处理的一无所知。不同层面之间只是通过一些特定的接口（Method）进行通信。并且在这个设计模式当中视图和模型是被隔开的，他们之间不能直接访问，而是要通过一个控制器来来处理模型和视图之间的逻辑。这样的好处在于</P> <UL> <LI>当我们需要对某一层进行修改的时候，只要保证接口的定义不发生变化，这个修改就不会影响到其他的层面。  <LI>界面显示部分的逻辑、数据的处理逻辑、数据的存储逻辑被彻底分离。每一个层面都只关注自己层面的逻辑而不用关心别的层面具体是如何进行处理的。</LI></UL> <P> </P> <HR>  <P> </P> <P><STRONG>注意！</STRONG></P> <P>这里提到的接口是广义的接口，而不是指用关键字interface所定义的接口类。</P> <P>接口是连接两个模块的规范（在面向对象当中，最基本的模块就是对象）。在对象这个层面，接口就是一个方法定义，这个定义除了方法的返回类型和参数以外，还包括方法的前置条件（Pre-condition）和后置条件（Post-condition）。</P> <P>接口方法：提供给其它对象使用，用来操纵自身对象的方法。</P> <P>前置条件：要让这个方法得到正确的结果，需要具备一些什么样的先决条件。（比如参数不能为null）</P> <P>后置条件：这个方法结束之后将会有怎样的后果。</P> <P>前置条件和后置条件并不是ActionScript3.0用语法约束硬性规定的，它可以是一种口头约定，或者是被写在方法的注释当中或者文档当中的描述性文本。并不是所有的方法都需要注明前置条件和后置条件，但是在一些关键的接口方法的定义当中，应该写明前置条件和后置条件。</P> <P>关键的接口方法的前置条件和后置条件往往被忽略，这是造成将来维护困难的一个重要原因。</P> <P> </P> <HR>  <P> </P> <P> </P> <P>    例3.2-1是完整的程序代码。</P> <P> </P> <P><STRONG>例3.2-1</STRONG></P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG><FONT size=3><U>Model</U></FONT></STRONG>  <P><STRONG><U><FONT size=3></FONT></U></STRONG> </P> <P><STRONG>package</STRONG><BR>{<BR>     <STRONG>import</STRONG> flash.events.EventDispatcher;<BR>     <STRONG>import</STRONG> flash.events.Event;</P> <P> </P> <P>     [Event (name=&#8221;vectorChanged&#8221;, type=&#8221;flash.events.Event&#8221;)]</P> <P>     <STRONG>public</STRONG> <STRONG>class</STRONG> Model <STRONG>extends</STRONG> EventDispatcher<BR>     {<BR>          <STRONG>public</STRONG> <STRONG>static</STRONG> <STRONG>const</STRONG> VECTOR_CHANGED:String = &#8220;vector changed&#8221;;<BR>  <BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> vectors:uint = 1; <EM><FONT color=#33cc00>//默认是1个顶点<BR></FONT></EM>  <BR>          <EM><FONT color=#009900><FONT color=#33cc00>/**<BR>           * Post-condition：返回vector的数目<BR>           */ </FONT><BR></FONT></EM>          <STRONG>public</STRONG> <STRONG>function</STRONG> getVectors() :uint {<BR>               <STRONG>return</STRONG> <STRONG>this</STRONG>.vectors;<BR>          }<BR>  <BR>          <EM><FONT color=#33cc00>/**<BR>           * pre-condition: n 不能为负数<BR>           * <BR>           * post-condition: vector 的数目将会被设为 n<BR>           */ </FONT></EM><BR>          <STRONG>public</STRONG> <STRONG>function</STRONG> setVectors(n:uint) :<STRONG>void</STRONG> {<BR>               <STRONG>this</STRONG>.vectors = Math.max(n, 1); <EM><FONT color=#33cc00>//确保至少有一个顶点<BR></FONT></EM>               <EM><FONT color=#009900><FONT color=#33cc00>//发布更新事件</FONT><BR></FONT></EM>               <STRONG>var</STRONG> event:Event = <STRONG>new</STRONG> Event(Model.VECTOR_CHANGED);<BR>               <STRONG>this</STRONG>.dispatchEvent(event);<BR>          }<BR>     }<BR>}</P></BLOCKQUOTE> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG><FONT size=3><U>View</U></FONT></STRONG>  <P><STRONG><U><FONT size=3></FONT></U></STRONG> </P> <P><STRONG>package</STRONG><BR>{<BR>     <STRONG>import</STRONG> flash.display.DisplayObjectContainer;<BR>     <STRONG>import</STRONG> flash.display.Shape;<BR>     <STRONG>import</STRONG> flash.events.Event;<BR>     <STRONG>import</STRONG> flash.events.EventDispatcher;<BR>     <STRONG>import</STRONG> flash.events.MouseEvent;<BR> <BR>     <STRONG>import</STRONG> org.aswing.ASColor;<BR>     <STRONG>import</STRONG> org.aswing.AsWingManager;<BR>     <STRONG>import</STRONG> org.aswing.EmptyLayout;<BR>     <STRONG>import</STRONG> org.aswing.FlowLayout;<BR>     <STRONG>import</STRONG> org.aswing.JButton;<BR>     <STRONG>import</STRONG> org.aswing.JFrame;<BR>     <STRONG>import</STRONG> org.aswing.JPanel;<BR>     <STRONG>import</STRONG> org.aswing.geom.IntPoint;<BR>     <STRONG>import</STRONG> org.aswing.graphics.Graphics2D;<BR>     <STRONG>import</STRONG> org.aswing.graphics.Pen;<BR> <BR>     [Event (name=&#8221;addVector&#8221;, type=&#8221;flash.events.Event&#8221;)]<BR>     [Event (name=&#8221;removeVector&#8221;, type=&#8221;flash.events.Event&#8221;)]<BR>     <STRONG>public</STRONG> <STRONG>class</STRONG> View <STRONG>extends</STRONG> EventDispatcher<BR>     {<BR>          <STRONG>public</STRONG> <STRONG>static</STRONG> <STRONG>const</STRONG> ADD_VECTOR:String = &#8220;add vector&#8221;;<BR>          <STRONG>public</STRONG> <STRONG>static</STRONG> <STRONG>const</STRONG> REMOVE_VECTOR:String = &#8220;remove vector&#8221;;<BR>  <BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> owner:DisplayObjectContainer;<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> canvas:JPanel;<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> shape:Shape;<BR>  <BR>          <FONT color=#33cc00><EM>/**<BR>           * 构造函数<BR>           * pre-condition: owner 对象不能为空，而且必须已经被添加到Stage的显示列表当中<BR>           * <BR>           * post-condition：将构造视图的界面<BR>           */</EM> </FONT><BR>          <STRONG>public</STRONG> <STRONG>function</STRONG> View(owner:DisplayObjectContainer) {<BR>               <STRONG>this</STRONG>.owner = owner;<BR>   <BR>               AsWingManager.initAsStandard(<STRONG>this</STRONG>.owner);<BR>   <BR>               <EM><FONT color=#33cc00>//创建显示多边形的框架</FONT></EM><BR>               <STRONG>var</STRONG> outputFrame:JFrame = <STRONG>new</STRONG> JFrame(<STRONG>this</STRONG>.owner, &#8220;Output&#8221;);</P> <P>               <STRONG>this</STRONG>.canvas = <STRONG>new</STRONG> JPanel(<STRONG>new</STRONG> EmptyLayout()); <EM><FONT color=#33cc00>//用空布局<BR></FONT></EM>               <STRONG>this</STRONG>.canvas.setSizeWH(400, 400);</P> <P>               <STRONG>this</STRONG>.shape = new Shape();<BR>               <EM><FONT color=#33cc00>//将Shape的坐标原点移动到JPanel的中央<BR></FONT></EM>               shape.x = 200;<BR>               shape.y = 200;<BR>               <STRONG>this</STRONG>.canvas.addChild(<STRONG>this</STRONG>.shape);</P> <P>               outputFrame.getContentPane().append(<STRONG>this</STRONG>.canvas);<BR>               outputFrame.pack(); <EM><FONT color=#33cc00>//根据内容的尺寸自动计算框架的尺寸<BR></FONT></EM>   <BR>               <EM><FONT color=#33cc00>//创建输入用的框架</FONT></EM><BR>               <STRONG>var</STRONG> inputFrame:JFrame = <STRONG>new</STRONG> JFrame(<STRONG>this</STRONG>.owner, &#8220;Input&#8221;);</P> <P>               inputFrame.setLocationXY(450, 0);<BR>               inputFrame.getContentPane().setLayout(<STRONG>new</STRONG> FlowLayout());</P> <P> </P> <P>               <STRONG>var</STRONG> addVectorBtn:JButton = <STRONG>new</STRONG> JButton(&#8221;Add a Vector&#8221;);<BR>               addVectorBtn.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.__onAddVector);<BR>               inputFrame.getContentPane().append(addVectorBtn);</P> <P> </P> <P>               <STRONG>var</STRONG> removeVectorBtn:JButton = new JButton(&#8221;Remove a Vector&#8221;);<BR>               removeVectorBtn.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.__onRemoveVector);<BR>               inputFrame.getContentPane().append(removeVectorBtn);<BR>   <BR>               inputFrame.pack(); <EM><FONT color=#33cc00>//根据内容的尺寸自动计算输入框架的尺寸<BR></FONT></EM>   <BR>               outputFrame.show();<BR>               inputFrame.show();<BR>          }  <BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> __onAddVector(e:Event) :<STRONG>void</STRONG> {<BR>               <STRONG>var</STRONG> event:Event = <STRONG>new</STRONG> Event(View.ADD_VECTOR);<BR>               <STRONG>this</STRONG>.dispatchEvent(event);<BR>          }<BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> __onRemoveVector(e:Event) :<STRONG>void</STRONG> {<BR>               <STRONG>var</STRONG> event:Event = <STRONG>new</STRONG> Event(View.REMOVE_VECTOR);<BR>               <STRONG>this</STRONG>.dispatchEvent(event);<BR>          }<BR>  <BR>          <FONT color=#33cc00><EM>/**<BR>           * pre-condition: n 是一个uint类型的正整数，如果传进来的参数是一个负数，那么这个负数经过uint类型<BR>           *      转换之后将会变成一个巨大的正数，这将会造成严重的运行错误。<BR>           * <BR>           * post-condition: 将绘制一个正n边形<BR>           */</EM> </FONT><BR>          <STRONG>public</STRONG> <STRONG>function</STRONG> update(n:uint) :<STRONG>void</STRONG> {<BR>               <STRONG>var</STRONG> g:Graphics2D = <STRONG>new</STRONG> Graphics2D(shape.graphics);<BR>               <STRONG>var</STRONG> pen:Pen = new Pen(<STRONG>new</STRONG> ASColor(0&#215;666666), 3);<BR>   <BR>               g.clear();<BR>               g.drawPolygon(pen, this.calculateVectors(n));<BR>               g.endDraw();<BR>          }<BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> calculateVectors(n:uint) :Array {<BR>               <STRONG>var</STRONG> theta:Number = 2*Math.PI/n;<BR>   <BR>               <STRONG>var</STRONG> radius:uint = 150; <EM><FONT color=#33cc00>//半径150像素<BR></FONT></EM>   <BR>               <STRONG>var</STRONG> array:Array = <STRONG>new</STRONG> Array(n); <EM><FONT color=#33cc00>//初始化一个长度为n的数组<BR></FONT></EM>               <STRONG>var</STRONG> point:IntPoint;<BR>               <STRONG>var</STRONG> x:int;<BR>               <STRONG>var</STRONG> y:int;<BR>               <STRONG>var</STRONG> angle:Number;<BR>               <STRONG>for</STRONG> (<STRONG>var </STRONG>i:uint=0; i&lt;n; i++) {<BR>                    angle = i*theta;<BR>    <BR>                    angle -= Math.PI; <FONT color=#33cc00>//将角度调整到 [-PI, PI] 区间<BR></FONT>    <BR>                    x = (radius*Math.cos(angle));<BR>                    y = (radius*Math.sin(angle));<BR>                    point = new IntPoint(x, y);<BR>                    array[i] = point;<BR>               }<BR>               <STRONG>return</STRONG> array;<BR>          }<BR>     }<BR>}</P></BLOCKQUOTE> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG><U><FONT size=3>Controller</FONT></U></STRONG>  <P> </P> <P><STRONG>package</STRONG><BR>{<BR>     <STRONG>public</STRONG> <STRONG>class</STRONG> Controller<BR>     {<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> v2mChannel:Channel;<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> m2vChannel:Channel;<BR>  <BR>         <FONT color=#33cc00><EM> /**<BR>            * Pre-condition：view 不为null；model不为null<BR>            * <BR>            * Post-condition：将开始控制 view 和 model 之间的数据<BR>            */</EM> </FONT><BR>          <STRONG>public</STRONG> <STRONG>function</STRONG> Controller(view:View, model:Model) {<BR>               <STRONG>this</STRONG>.v2mChannel = <STRONG>new</STRONG> ViewToModelChannel(view, model);<BR>               <STRONG>this</STRONG>.m2vChannel = <STRONG>new</STRONG> ModelToViewChannel(view, model);<BR>          }<BR>     }<BR>}</P> <P><BR><EM><FONT color=#33cc00>//Inner classes</FONT></EM></P> <P><BR><STRONG>import</STRONG> flash.events.Event;</P> <P> </P> <P><STRONG>class</STRONG> Channel {<BR>     <STRONG>protected</STRONG> <STRONG>var</STRONG> view:View;<BR>     <STRONG>protected</STRONG> <STRONG>var</STRONG> model:Model;<BR> <BR>     <STRONG>public</STRONG> <STRONG>function</STRONG> Channel(view:View, model:Model) {<BR>          <STRONG>this</STRONG>.view = view;<BR>          <STRONG>this</STRONG>.model = model;<BR>     }<BR>}</P> <P><STRONG>class</STRONG> ViewToModelChannel <STRONG>extends</STRONG> Channel {<BR>    <BR>     <STRONG>public</STRONG> <STRONG>function</STRONG> ViewToModelChannel(view:View, model:Model) {<BR>          <STRONG>super</STRONG>(view, model);<BR>  <BR>          <EM><FONT color=#33cc00>//监听来自视图的事件<BR></FONT></EM>          <STRONG>this</STRONG>.view.addEventListener(View.ADD_VECTOR, <STRONG>this</STRONG>.__onAddVector);<BR>          <STRONG>this</STRONG>.view.addEventListener(View.REMOVE_VECTOR, <STRONG>this</STRONG>.__onRevoveVector);<BR>     }<BR> <BR>     <STRONG>private</STRONG> <STRONG>function</STRONG> __onAddVector(e:Event) :void {<BR>          <STRONG>this</STRONG>.model.setVectors(<STRONG>this</STRONG>.model.getVectors()+1);<BR>     }<BR>     <STRONG>private</STRONG> <STRONG>function</STRONG> __onRevoveVector(e:Event) :void {<BR>          <STRONG>this</STRONG>.model.setVectors(<STRONG>this</STRONG>.model.getVectors()-1);<BR>     }<BR>}</P> <P><STRONG>class</STRONG> ModelToViewChannel <STRONG>extends</STRONG> Channel {<BR>    <BR>     <STRONG>public</STRONG> <STRONG>function</STRONG> ModelToViewChannel(view:View, model:Model) {<BR>          <STRONG>super</STRONG>(view, model);<BR>  <BR>         <EM><FONT color=#33cc00> //监听来自模型的事件<BR></FONT></EM>          <STRONG>this</STRONG>.model.addEventListener(Model.VECTOR_CHANGED, <STRONG>this</STRONG>.__onVectorChanged);<BR>     }<BR> <BR>     <STRONG>private</STRONG> <STRONG>function</STRONG> __onVectorChanged(e:Event) :<STRONG>void</STRONG> {<BR>          <STRONG>this</STRONG>.view.update(<STRONG>this</STRONG>.model.getVectors());<BR>     }<BR>}</P></BLOCKQUOTE> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" color=#000000 size=3>API：org.aswing.JFrame</FONT></STRONG></P> <UL> <LI> <DIV>setLocationXY(x:int, y:int):void</DIV></LI></UL> <P>     设置框架的位置，x，y参数分别表示框架左上角定点的坐标</P> <P> </P> <UL> <LI> <DIV>pack():void</DIV></LI></UL> <P>     根据所包含组件的默认大小来自动调整框架的尺寸。</P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" color=#000000 size=3>API:flash.events.EventDispatcher</FONT></STRONG></P> <UL> <LI> <DIV>dispatchEvent(event:Event):Boolean</DIV></LI></UL> <P>     发布事件。所发布事件的event.target指向发布事件的对象</P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" color=#000000 size=3>API:org.aswing.graphics.Graphics2D</FONT></STRONG></P> <UL> <LI> <DIV>drawPolygon(pen:Pen, points:Array):void</DIV></LI></UL> <P>     绘制多边形。多边形是封闭的，第一个顶点和最后一个顶点之间有连线。</P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" color=#000000 size=3>API:Math</FONT></STRONG></P> <UL> <LI> <DIV>Math.PI</DIV></LI></UL> <P>     圆周率</P> <P> </P> <UL> <LI> <DIV>Math.cos(angle:Number):Number</DIV></LI></UL> <P>     计算余弦。angle的单位是弧度。angle位于[-PI, PI]区间</P> <P> </P> <UL> <LI> <DIV>Math.sin(angle:Number):Number </DIV></LI></UL> <P>     计算正弦。angle的单位是弧度。angle位于[-PI, PI]区间</P> <P> </P> <UL> <LI> <DIV>Math.max(value1:Number, value2:Number):Number</DIV></LI></UL> <P>     返回两个数当中较大的数</P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: rgb(51,102,255)" color=#000000 size=3>标记：Event</FONT></STRONG></P> <P>语法：[Event (name=&#8221;eventName&#8221;, type=&#8221;<EM>package.eventType</EM>&#8220;)]</P> <P>    这个标记在MXML当中很有用，对于普通的ActionScript项目来说，这个标记可以在FlexBuilder2（或者FlexBuilder3）当中触发代码提示。</P> <P>    一般来说，如果某一个类要发布事件，应该用Event标记指出它将会发布哪些事件。</P> <P> </P> <UL> <LI> <DIV>eventName </DIV></LI></UL> <P>    其所关联的类当中的属性为 EVENT_NAME，即，在标记当中，事件名称的首字母小写，之后每一个单词的第一个字母大写来区分其中的单词。而其所对应的类当中属性的名称全部用大写，单词之间用下划线分开。</P> <P> </P> <UL> <LI> <DIV>type</DIV></LI></UL> <P>    这个事件的类型，由于例子当中用的就是Event，所以这里写的就是flash.events.Event。当然，它也可以是flash.event.MouseEvent或者是你自己定义的事件类型。</P> <P> </P> <P> </P></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=27</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=26</link>
		<comments>http://thiswind.flashseer.org/?p=26#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:37:06 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=26</guid>
		<description><![CDATA[AsWing入门教程 第三章 基本AsWing组件       作者：胡矿      著作权所有，请勿转载       www.flashseer.org     Google Doc http://docs.google.com/Doc?id=dnp8gdz_34dwn4ws         从本章开始将要介绍AsWing的组件以及相关的编程技巧。
]]></description>
			<content:encoded><![CDATA[<p><P><STRONG>AsWing入门教程 第三章 基本AsWing组件</STRONG> </P> <P> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>作者：胡矿</STRONG> </P> <P> </P> <P> </P> <P><STRONG>著作权所有，请勿转载</STRONG> </P> <P> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG><A href="http://www.flashseer.org/"><FONT color=#810081>www.flashseer.org</FONT></A></STRONG>  </P> <P> </P> <P>Google Doc</P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_34dwn4ws" target=_blank>http://docs.google.com/Doc?id=dnp8gdz_34dwn4ws</A></P> <P> </P> <P> </P> <P>    从本章开始将要介绍AsWing的组件以及相关的编程技巧。</P></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=26</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=25</link>
		<comments>http://thiswind.flashseer.org/?p=25#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:35:55 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=25</guid>
		<description><![CDATA[AsWing入门教程 2.2 鼠标事件   作者：胡矿   著作权所有，请勿转载   www.flashseer.org   Google Doc http://docs.google.com/Doc?id=dnp8gdz_30dmhcpf         在上一节当中，我们介绍了普通的事件处理。在这一节我们要介绍如何捕捉鼠标事件、键盘事件和焦点事件。   2.1.1鼠标事件。       在AsWing当中的组件都是Sprite的子类，AsWing组件又可以当作Sprite使用。       图2.2-1是一个简易的涂鸦板。  （图2.2-1）       这个涂鸦板有一个“Undo”按钮，可以删除刚刚绘制的一笔。在鼠标离开框架的时候，整个框架会使用一个模糊滤镜。例2.2-1是完整的程序。   package [...]]]></description>
			<content:encoded><![CDATA[<p><P><STRONG><FONT size=3>AsWing入门教程 2.2 鼠标事件</FONT></STRONG></P> <P><STRONG><FONT size=3></FONT></STRONG> </P> <P><STRONG><FONT size=3>作者：胡矿</FONT></STRONG></P> <P><STRONG><FONT size=3></FONT></STRONG> </P> <P><STRONG><FONT size=3>著作权所有，请勿转载</FONT></STRONG></P> <P><STRONG><FONT size=3></FONT></STRONG> </P> <P><STRONG><FONT size=3><A href="http://www.flashseer.org">www.flashseer.org</A></FONT></STRONG></P> <P><STRONG><FONT size=3></FONT></STRONG> </P> <P><STRONG><FONT size=3>Google Doc</FONT></STRONG></P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_30dmhcpf" target=_blank><FONT color=#810081>http://docs.google.com/Doc?id=dnp8gdz_30dmhcpf</FONT></A></P> <P> </P> <P><STRONG><FONT size=3></FONT></STRONG> </P> <P><FONT size=3><STRONG>    </STRONG></FONT><FONT size=2>在上一节当中，我们介绍了普通的事件处理。在这一节我们要介绍如何捕捉鼠标事件、键盘事件和焦点事件。</FONT></P> <P> </P> <P>2.1.1鼠标事件。</P> <P> </P> <P>    在AsWing当中的组件都是Sprite的子类，AsWing组件又可以当作Sprite使用。</P> <P> </P> <P>    图2.2-1是一个简易的涂鸦板。</P> <DIV id=img_400704851579989 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_31dv9vq8fc"></DIV> <P>（图2.2-1）</P> <P> </P> <P>    这个涂鸦板有一个“Undo”按钮，可以删除刚刚绘制的一笔。在鼠标离开框架的时候，整个框架会使用一个模糊滤镜。例2.2-1是完整的程序。</P> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>package</STRONG> {<BR>     <STRONG>import</STRONG> flash.display.Shape;<BR>     <STRONG>import</STRONG> flash.display.Sprite;<BR>     <STRONG>import</STRONG> flash.events.Event;<BR>     <STRONG>import</STRONG> flash.events.MouseEvent;<BR>     <STRONG>import</STRONG> flash.filters.BlurFilter;<BR>     <STRONG>import</STRONG> flash.geom.Point;<BR> <BR>     <STRONG>import</STRONG> org.aswing.ASColor;<BR>     <STRONG>import</STRONG> org.aswing.AsWingManager;<BR>     <STRONG>import</STRONG> org.aswing.BorderLayout;<BR>     <STRONG>import</STRONG> org.aswing.Container;<BR>     <STRONG>import</STRONG> org.aswing.FlowLayout;<BR>     <STRONG>import</STRONG> org.aswing.JButton;<BR>     <STRONG>import</STRONG> org.aswing.JFrame;<BR>     <STRONG>import</STRONG> org.aswing.JPanel;<BR>     <STRONG>import</STRONG> org.aswing.border.LineBorder;<BR>     <STRONG>import</STRONG> org.aswing.graphics.Graphics2D;<BR>     <STRONG>import</STRONG> org.aswing.graphics.IBrush;<BR>     <STRONG>import</STRONG> org.aswing.graphics.Pen;<BR>     <STRONG>import</STRONG> org.aswing.graphics.SolidBrush;  <P>    </P> <P>     [SWF (width=600, height=500, frameRate=50)]<BR>     <STRONG>public</STRONG> <STRONG>class</STRONG> MouseTest <STRONG>extends</STRONG> Sprite { <BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> shapes:Array = <STRONG>new</STRONG> Array();//在定义的时候对 Array 赋初值是一个好习惯<BR>     <BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> pen:Pen = <STRONG>new</STRONG> Pen(ASColor.BLACK, 3);<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> brush:IBrush = <STRONG>new</STRONG> SolidBrush(ASColor.BLACK);<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> lastPoint:Point;<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> drawing:Boolean = <STRONG>false</STRONG>;<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> currentShape:Shape;<BR>        <BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> frame:JFrame;<BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> canvas:JPanel;<BR>        <BR>          <STRONG>private</STRONG> <STRONG>var</STRONG> blur:BlurFilter = <STRONG>new</STRONG> BlurFilter();<BR>        <BR>          <STRONG>public</STRONG> <STRONG>function</STRONG> MouseTest() {<BR>               <STRONG>super</STRONG>();<BR>   <BR>               <STRONG>this</STRONG>.addEventListener(Event.ADDED_TO_STAGE, <STRONG>this</STRONG>.init);<BR>          }<BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> init(e:Event) :<STRONG>void</STRONG> {<BR>               AsWingManager.initAsStandard(<STRONG>this</STRONG>);<BR>   <BR>               <STRONG>this</STRONG>.frame = <STRONG>new</STRONG> JFrame(<STRONG>this</STRONG>, &#8220;Mouse Test&#8221;);<BR>               <STRONG>this</STRONG>.frame.setSizeWH(540, 400);<BR>   <BR>               <STRONG>var</STRONG> c:Container = <STRONG>this</STRONG>.frame.getContentPane();<BR>   <BR>               <STRONG>this</STRONG>.canvas = <STRONG>new</STRONG> JPanel();<BR>               <STRONG>this</STRONG>.canvas.setOpaque(<STRONG>true</STRONG>);<BR>               <STRONG>this</STRONG>.canvas.setBackground(<STRONG>new</STRONG> ASColor(0xFFFFFF));<BR>               <STRONG>this</STRONG>.canvas.setBorder(<STRONG>new</STRONG> LineBorder());<BR>               c.append(<STRONG>this</STRONG>.canvas, BorderLayout.CENTER);<BR>   </P> <P>               <STRONG>var</STRONG> buttonPanel:JPanel = <STRONG>new</STRONG> JPanel(<STRONG>new</STRONG> FlowLayout());<BR>               c.append(buttonPanel, BorderLayout.WEST);<BR>   <BR>               <STRONG>var</STRONG> undoBtn:JButton = <STRONG>new</STRONG> JButton(&#8221;Undo&#8221;);<BR>               undoBtn.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.undo);<BR>               buttonPanel.append(undoBtn);<BR>   <BR>               <STRONG>this</STRONG>.configure();<BR>   <BR>               <STRONG>this</STRONG>.frame.show();<BR>          }<BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> configure() :<STRONG>void</STRONG> {   <BR>               <STRONG>this</STRONG>.canvas.addEventListener(MouseEvent.MOUSE_DOWN, <STRONG>this</STRONG>.onMouseDown);<BR>               <STRONG>this</STRONG>.canvas.addEventListener(MouseEvent.MOUSE_UP, <STRONG>this</STRONG>.onMouseUp);<BR>   <BR>               <STRONG>this</STRONG>.canvas.addEventListener(MouseEvent.MOUSE_MOVE, <STRONG>this</STRONG>.onMouseMove);<BR>   <BR>               <STRONG>this</STRONG>.frame.addEventListener(MouseEvent.ROLL_OUT, <STRONG>this</STRONG>.onRollOut);<BR>               <STRONG>this</STRONG>.frame.addEventListener(MouseEvent.ROLL_OVER, <STRONG>this</STRONG>.onRollOver);<BR>          }<BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> undo(e:MouseEvent) :<STRONG>void</STRONG> {<BR>               <STRONG>var</STRONG> shape:Shape = <STRONG>this</STRONG>.shapes.pop();<BR>      <BR>               if (shape != null) {<BR>                   <STRONG>this</STRONG>.canvas.removeChild(shape);<BR>               }<BR>          }<BR>  </P> <P>          <STRONG>private</STRONG> <STRONG>function</STRONG> onMouseDown(e:MouseEvent) :<STRONG>void</STRONG> {<BR>               <STRONG>this</STRONG>.currentShape = <STRONG>new</STRONG> Shape();<BR>               <STRONG>this</STRONG>.canvas.addChild(<STRONG>this</STRONG>.currentShape);<BR>      </P> <P>               <STRONG>this</STRONG>.lastPoint = <STRONG>new</STRONG> Point(e.localX, e.localY);<BR>               <STRONG>this</STRONG>.drawing = <STRONG>true</STRONG>;<BR>          }<BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> onMouseUp(e:MouseEvent) :<STRONG>void</STRONG> {<BR>               if (<STRONG>this</STRONG>.currentShape != null) {<BR>                   <STRONG>this</STRONG>.canvas.addChild(<STRONG>this</STRONG>.currentShape);<BR>                   <STRONG>this</STRONG>.shapes.push(<STRONG>this</STRONG>.currentShape);<BR>               }<BR>               <STRONG>this</STRONG>.drawing = <STRONG>false</STRONG>;<BR>          }<BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> onMouseMove(e:MouseEvent) :<STRONG>void</STRONG> {<BR>               if (<STRONG>this</STRONG>.drawing) {<BR>                   <STRONG>var</STRONG> point:Point = <STRONG>new</STRONG> Point(e.localX, e.localY);<BR>          <BR>                   <STRONG>var</STRONG> g:Graphics2D = <STRONG>new</STRONG> Graphics2D(<STRONG>this</STRONG>.currentShape.graphics);<BR>                   g.drawLine(<STRONG>this</STRONG>.pen, <STRONG>this</STRONG>.lastPoint.x, <STRONG>this</STRONG>.lastPoint.y, point.x, point.y);<BR>          <BR>                   <STRONG>this</STRONG>.lastPoint = point;<BR>               }<BR>          }<BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> onRollOut(e:MouseEvent) :<STRONG>void</STRONG> {</P> <P>              <STRONG>this</STRONG>.drawing = <STRONG>false</STRONG>;<BR>              <STRONG>this</STRONG>.frame.filters = [<STRONG>this</STRONG>.blur];<BR>          }<BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> onRollOver(e:MouseEvent) :<STRONG>void</STRONG> {      <BR>              <STRONG>this</STRONG>.frame.filters = [];<BR>          }<BR>     }<BR>}</P></BLOCKQUOTE> <P> </P> <P>    我们来逐行分析这个程序。</P> <P> </P> <P>    在定义类的时候，我们用了一个[SWF (width=600, height=500, frameRate=50)]标记，这个标记告诉编译器目标SWF的大小是800像素宽，500像素高，帧速是50帧/秒。</P> <P> </P> <P>  <HR>  <P> </P> <P><STRONG>笔记：</STRONG></P> <P>ActionScript3.0支持标记，除了SWF标记之外，常用的标记还有 [Event] 和 [Embed]，在本书的附录当中将会介绍这些常用的标记。</P> <P>  <HR>  <P> </P> <P> </P> <P>    在构造函数当中，我们侦听了一个ADDED_TO_STAGE事件，这个事件会在一个DisplayObject被添加到舞台上的时候被触发。和上一章的例子不同，从这一章开始，我们不再在构造函数当中直接做初始化，而是在ADDED_TO_STAGE事件的事件处理函数（init(Event):void {&#8230;}）当中做初始化。因为在一个Sprite创建的时候它也许还没有被添加到舞台，这时候，Sprite的stage引用还没有被设置，这时候做初始化有可能会造成一些计算上的错误。所以我们将会在Sprite被添加到舞台上之后再来进行初始化。</P> <P> </P> <P>  <HR>  <P> </P> <P><STRONG>注意！</STRONG></P> <P>在ADDED_TO_STAGE事件的处理函数当中进行初始化是一个良好的习惯，我们将建议你用这样的习惯。这会使你的程序有更好的兼容性。</P> <P>  <HR>  <P> </P> <P> </P> <P>    我们用一个JPanel作为画布。这是因为JPanel是Sprite的子类，我们可以像操作Sprite一样操作JPanel。JPanel默认是透明的，为了能够在上面绘画，我们把它设为不透明的，并且将背景颜色设为白色（0xFFFFFF）。如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>this</STRONG>.canvas.setOpaque(<STRONG>true</STRONG>);<BR><STRONG>this</STRONG>.canvas.setBackground(<STRONG>new</STRONG> ASColor(0xFFFFFF));</BLOCKQUOTE> <P> </P> <P>    为了让画布的边缘更加明显，我们给画布加了边框（Border）。setBorder方法继承自Component，所有的AsWing控件都有这个方法。AsWing支持很多种边框，LineBorder是线状边框。常用作组件边界的标识。默认的LineBorder是直角，黑色，宽度为1像素的矩形线框。如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <STRONG>this</STRONG>.canvas.setBorder(<STRONG>new</STRONG> LineBorder());</BLOCKQUOTE> <P>    </P> <P>    每一笔的绘制从鼠标按下开始，到鼠标离开画布或者鼠标弹起结束。在鼠标按下的时候，我们新建了一个Shape对象，将这个对象加入到画布的显示队列当中（addChild），如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>this</STRONG>.currentShape = <STRONG>new</STRONG> Shape();<BR><STRONG>this</STRONG>.canvas.addChild(<STRONG>this</STRONG>.currentShape);</BLOCKQUOTE> <P> </P> <P>    为了记录绘图的过程，我们把每一笔作为一个Shape对象添加到画布（这时候，JPanel应当被看作是一个Sprite）的最上层。同时把这个Shape的引用加入到一个Array当中。在Undo的时候，我们从这个Array当中取出最新的一笔（Shape）的引用，通过这个引用从画布上删除这一笔。如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">          <STRONG>private</STRONG> <STRONG>function</STRONG> undo(e:MouseEvent) :<STRONG>void</STRONG> {<BR>               <STRONG>var</STRONG> shape:Shape = <STRONG>this</STRONG>.shapes.pop();<BR>      <BR>               if (shape != null) {<BR>                   <STRONG>this</STRONG>.canvas.removeChild(shape);<BR>               }<BR>          }</BLOCKQUOTE> <P> </P> <P>  <HR>  <P> </P> <P>练习：</P> <P>1.给这个画图板添加更多功能，比如绘制矩形、绘制椭圆。</P> <P>2.你可以把整个绘制过程记录下来，以SVG保存。关于SVG的详细信息，可以参看SVG中国的文档<A href="http://www.chinasvg.com/">http://www.chinasvg.com/</A></P> <P>  <HR>  <P> </P> <P> </P> <P>        </P> <P><STRONG><FONT style="BACKGROUND-COLOR: #ff9900" size=4>API：flash.events.Event</FONT></STRONG></P> <UL> <LI> <DIV>Event.ADDED_TO_STAGE：</DIV></LI></UL> <P>        在对象被加入到舞台（Stage）的显示队列当中的时候触发这个事件。一般来说，不要在构造函数当中直接初始化，而是在ADDED_TO_STAGE事件的处理函数当中初始化，这样可以避免一些初始化错误，程序的兼容性更强。</P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: #ff9900" size=4>API：org.aswing.Component</FONT></STRONG></P> <UL> <LI> <DIV>    setOpaque(b:<STRONG>Boolean</STRONG>):<STRONG>void</STRONG> </DIV></LI></UL> <P>        组件背景是否不透明，false为透明，true为不透明    </P> <UL> <LI> <DIV>    setBackground(c:ASColor):void </DIV></LI></UL> <P>        设置组件的背景颜色，一般这个方法是和setOpaque配合使用的。只有背景是不透明的情况下才能看到背景颜色。</P> <UL> <LI> <DIV>    setBorder(b:Border):void </DIV></LI></UL> <P>        设置边框。</P> <P> </P> <P><FONT style="BACKGROUND-COLOR: #ff9900"><STRONG><FONT size=4>API：flash.events.MouseEvent</FONT></STRONG> </FONT></P> <UL> <LI> <DIV>MouseEvent.CLICK </DIV></LI></UL> <P>        常数，鼠标点击事件，一次鼠标点击事件=鼠标按下+鼠标弹起。所以，鼠标按下事件和鼠标弹起事件会和鼠标点击事件一起被触发。</P> <UL> <LI> <DIV>MouseEvent.MOUSE_DOWN </DIV></LI></UL> <P>        常数，鼠标按下事件，当鼠标在对象上方按下的时候触发。如果对象被</P> <UL> <LI> <DIV>MouseEvent.MOUSE_UP </DIV></LI></UL> <P>        常数，鼠标弹起事件，当鼠标在对象上方弹起的时候触发</P> <UL> <LI> <DIV>MouseEvent.MOUSE_MOVE </DIV></LI></UL> <P>        常数，鼠标移动事件，鼠标移动的时候触发。系统底层事件，两次事件之间的周期与帧速无关。</P> <UL> <LI> <DIV>MouseEvent.ROLL_OUT </DIV></LI></UL> <P>        常数，鼠标滑出对象事件，鼠标滑出对象的时候触发。</P> <UL> <LI> <DIV>MouseEvent.ROLL_OVER </DIV></LI></UL> <P>        常数，鼠标滑入对象事件，鼠标滑入对象进入对象上空区域的时候触发。</P> <UL> <LI> <DIV>localX:Number </DIV></LI></UL> <P>        属性，鼠标当前的X坐标，参考系是当前对象的局部坐标系。</P> <UL> <LI> <DIV>localX:Number </DIV></LI></UL> <P>        属性，鼠标当前的Y坐标，参考系是当前对象的局部坐标系。 </P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: #ff6600" size=4>API：flash.filters.BlurFilter</FONT></STRONG></P> <UL> <LI> <DIV>BlurFilter(blurX:Number=4.0, blurY:Number=4.0, auality:int=1.0) </DIV></LI></UL> <P>        构造函数，模糊滤镜。</P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: #ff6600" size=4>API：flash.display.DisplayObject</FONT></STRONG></P> <UL> <LI> <DIV>filters:Array </DIV></LI></UL> <P>        属性，可读可写，滤镜列表，显示对象将依次使用队列当中的滤镜。</P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: #ff6600" size=4>API：Array</FONT></STRONG></P> <UL> <LI> <DIV>pop() :Object </DIV></LI></UL> <P>        弹出队列尾端的元素（栈顶）。</P> <UL> <LI> <DIV>push(o:Object) :void </DIV></LI></UL> <P>        把一个元素加到队列的尾端（栈顶）</P></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=25</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=24</link>
		<comments>http://thiswind.flashseer.org/?p=24#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:33:18 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=24</guid>
		<description><![CDATA[AsWing入门教程 2.1 事件处理基础     作者：胡矿     著作权所有，请勿转载     www.flashseer.org    Google Doc http://docs.google.com/Doc?id=dnp8gdz_24ctmwvs         任何Flash的应用都会不断监视敲击键盘、单击鼠标等事件。Flash播放器报告给正在运行的程序。然后程序会决定如何响应这些事件。在Flash8以及以前的时代，可以在影片剪辑里面直接写onXXX的事件处理代码，但是到了ActionScript3.0，这样的操作已经被废止了，对事件的处理都用统一的添加监听器的方式来实现。        事件源（EventDispatcher）拥有自己的方法，允许我们向其注册事件监听器。当事件源产生某个事件时，事件源会向注册在那个事件上的所有监听器发送通知。        在ActionScript3.0当中，所有的事件对象都是从flash.events.Event派生出来的。当然，有的事件类型有自己的子类，比如KeyboardEvent和MouseEvent。AsWing扩展了Event类，定义了许多自己的事件基类org.aswing.event.AWEvent，所有的AsWing自定义事件类都派生自AWEvent类，比如AttachEvent、DragAndDropEvent、FocusKeyEvent等等。       [...]]]></description>
			<content:encoded><![CDATA[<p><P><STRONG>AsWing入门教程 2.1 事件处理基础</STRONG> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>作者：胡矿</STRONG> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>著作权所有，请勿转载</STRONG> </P> <P> </P> <P><STRONG></STRONG></P> <P><A href="http://www.flashseer.org"><STRONG>www.flashseer.org</STRONG></A> </P> <P> </P> <P>Google Doc</P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_24ctmwvs" target=_blank>http://docs.google.com/Doc?id=dnp8gdz_24ctmwvs</A></P> <P> </P> <P> </P> <P>    任何Flash的应用都会不断监视敲击键盘、单击鼠标等事件。Flash播放器报告给正在运行的程序。然后程序会决定如何响应这些事件。在Flash8以及以前的时代，可以在影片剪辑里面直接写onXXX的事件处理代码，但是到了ActionScript3.0，这样的操作已经被废止了，对事件的处理都用统一的添加监听器的方式来实现。 </P> <P> </P> <P>    事件源（EventDispatcher）拥有自己的方法，允许我们向其注册事件监听器。当事件源产生某个事件时，事件源会向注册在那个事件上的所有监听器发送通知。 </P> <P> </P> <P>    在ActionScript3.0当中，所有的事件对象都是从flash.events.Event派生出来的。当然，有的事件类型有自己的子类，比如KeyboardEvent和MouseEvent。AsWing扩展了Event类，定义了许多自己的事件基类org.aswing.event.AWEvent，所有的AsWing自定义事件类都派生自AWEvent类，比如AttachEvent、DragAndDropEvent、FocusKeyEvent等等。 </P> <P> </P> <P>    在ActionScript3.0当中，所有的事件源都是从flash.events.EventDispatcher派生出来的。Component类是EventDispatcher类的子类，所以AsWing组件都是事件源。例如，按钮会发送点击事件，而框架能够发送关闭窗口、最小化等窗口事件。 </P> <P> </P> <P>    AsWing的事件机制是对ActionScript3.0事件机制的扩展，所以，AsWing的事件处理和ActionScript3.0的事件处理是相容的。 </P> <P> </P> <P>    作为总结，下面给出AsWing当中事件处理机制的概览： </P> <UL> <LI> <DIV>一个监听器是一个方法。 </DIV> <LI> <DIV>一个事件源是一个扩展了EventDispatcher，能够注册监听器并且向它们发送事件对象的对象。 </DIV> <LI> <DIV>事件发生时，事件源会把事件对象发送给所有注册了这个事件的监听器，一个事件源可以发送多种不同的事件。 </DIV> <LI> <DIV>监听器随后使用事件对象中的信息来决定对事件的反应。 </DIV></LI></UL> <P> </P> <P>    可以使用下面模式的代码为事件源注册监听器： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> eventSourceObject.addEventListener(eventType, listener); </BLOCKQUOTE> <P> </P> <P>    例如： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> listener:Function = <STRONG>function</STRONG> (event:Event) :<STRONG>void</STRONG> {<BR>    &#8230;&#8230;<BR>};<BR>   <BR><STRONG>var</STRONG> button:JButton = <STRONG>new</STRONG> JButton(&#8221;OK&#8221;);<BR>button.addEventListener(MouseEvent.CLICK, listener);  </BLOCKQUOTE> <P> </P> <P>    现在，只要按钮中产生了一个“CLICK”事件，listener方法就会被调用。listener方法接受一个Event类型的参数，其中封装了CLICK事件的相关信息，比如事件所携带的数据，事件的引发者等等。 </P> <P> </P> <P>    只要用户按下按钮，JButton对象就会产生一个MouseEvent对象，并且调用listener(event)传递那个事件对象。你可以为一个事件源添加多个监听方法。那样，只要用户按下按钮，所有的监听方法都会被调用。 </P> <P> </P> <P> </P> <HR>  <P> </P> <P>笔记： </P> <P>在ActionScript当中，方法（函数）也是对象，可以作为参数传递。 </P> <P> </P> <HR>  <P> </P> <P> </P> <P>    图2.1-1给出了事件源、事件监听函数和事件对象的写作关系。 </P> <DIV id=img_022419534334039315 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_28fhwd3bgb"> </DIV> <P>（图2.1-1） </P> <P> </P> <P> </P> <HR>  <P> </P> <P><STRONG>注意：</STRONG> </P> <P>在本章中，我们重点关注AsWing的用户界面事件（如按钮点击和鼠标移动事件）的处理。然而，事件处理不仅限于用户界面。 </P> <P> </P> <HR>  <P> </P> <P> </P> <P> </P> <P>2.1.1 实例：处理按钮点击事件 </P> <P> </P> <P>    为了熟悉时间模型，我们使用一个响应按钮点击事件的简单例子来说明模型中所需了解的所有细节。在这个例子中，我们需要： </P> <UL> <LI>一个放置了3个按钮的面板  <LI>3个添加到按钮上，作为动作监听器的监听函数 </LI></UL> <P> </P> <P>    在这个例子中，当用户点击面板上的任何一个按钮时，相关联的监听函数就会收到一个表明按钮被按下的MouseEvent对象。例子中的监听函数随后会改变面板的背景颜色。 </P> <P> </P> <P>    在演示程序如何监听按钮点击事件之前，我们首先需要解释如何创建按钮，如何把他们添加到面板上。（有关GUI元素的详细内容，请参考第3章。） </P> <P> </P> <P>    可以通过在按钮的构造器中指定标签字符串、图标，或者二者都指定来创建一个按钮。下面是两个例子： </P> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> button:JButton = <STRONG>new</STRONG> JButton(&#8221;Yellow&#8221;);<BR><STRONG>var</STRONG> button:JButton = <STRONG>new</STRONG> JButton(&#8221;", <STRONG>new</STRONG> AssetIcon(asset)); // 用一个 asset 作为图标。 </BLOCKQUOTE> <P>    把按钮添加到面板需要调用一个称作append的方法（很好记）。append方法的参数为需要添加到容器的组件。例如： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><STRONG>var</STRONG> buttonPanel:JPanel = <STRONG>new</STRONG> JPanel(new FlowLayout());<BR>   <BR><STRONG>var</STRONG> yellowButton:JButton = <STRONG>new</STRONG> JButton(&#8221;Yellow&#8221;);<BR><STRONG>var</STRONG> blueButton:JButton = <STRONG>new</STRONG> JButton(&#8221;Blue&#8221;);<BR><STRONG>var</STRONG> redButton:JButton = <STRONG>new</STRONG> JButton(&#8221;Red&#8221;);<BR>   <BR>buttonPanel.append(yellowButton);<BR>buttonPanel.append(blueButton);<BR>buttonPanel.append(redButton);  </P> <P> </P>buttonPanel.setOpaque(<STRONG>true</STRONG>); </BLOCKQUOTE> <P> </P> <P>    图2.1.1-1显示了相应的效果。 </P> <P> </P> <DIV id=img_8540938297351839 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_29fgnd6cfs"> </DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">（图2.1.1-1） </DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"></DIV> <P>    在上面的代码中，我们给面板设置了布局管理器（LayoutManager)。组件在添加到面板将会被放在哪里一般是由布局管理器来负责。对于JFrame和JWindow这样的顶层框架来说，默认的布局管理器是边界布局（BorderLayout）。JPanel默认的布局管理器为FlowLayout，在例子当中，我们明确的传入一个FlowLayout实例，你也可以传入其他任何布局管理器实例，FlowLayout是一个很常用的布局管理器，它将组件按照格子的最佳大小绘制，并且从左向右，从上倒下依次顺序排列。关于布局管理器在第3章还会详细叙述。由于默认情况下面板是透明的，我们要对其设置背景颜色就必须让其可见。通过调用buttonPanel.setOpaque(true)让面板变得可见。 </P> <P> </P> <P>    现在我们已经布置好了按钮，下面我们要定义3个监听函数，并且分别设为每个按钮的监听函数： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>private</STRONG> <STRONG>function</STRONG> __onYellowClick(e:MouseEvent) :<STRONG>void</STRONG> {<BR>   <BR>}<BR>  <BR><STRONG>private</STRONG> <STRONG>function</STRONG> __onBlueClick(e:MouseEvent) :<STRONG>void</STRONG> {<BR>   <BR>}<BR>  <BR><STRONG>private</STRONG> <STRONG>function</STRONG> __onRedClick(e:MouseEvent) :<STRONG>void</STRONG> {<BR>   <BR>}  </BLOCKQUOTE> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">yellowButton.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.__onYellowClick);<BR>blueButton.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.__onBlueClick);<BR>redButton.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.__onRedClick); </BLOCKQUOTE> <P> </P> <HR>  <P> </P> <P><STRONG>笔记：</STRONG> </P> <P>因为在ActionScript当中，函数也是对象，所以监听函数即可以作为类当中的成员函数定义，也可以作为一个局部变量定义，如： </P> <P><STRONG>var</STRONG> listenr:Function = <STRONG>function</STRONG> (e:MouseEvent) :<STRONG>void</STRONG> { &#8230;&#8230; }; </P> <P> </P> <HR>  <P> </P> <P> </P> <P>    现在还遗留有一个问题，就是监听函数没有访问buttonPanel对象的权限。对于这个问题有很多种解决方法，在这里我们想通过我们的这种解决办法告诉大家如何使用事件对象。如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>private</STRONG> <STRONG>function</STRONG> __onYellowClick(event:MouseEvent) :<STRONG>void</STRONG> {<BR>   <STRONG>var</STRONG> button:JButton = event.target <STRONG>as</STRONG> JButton;<BR>   <STRONG>var</STRONG> panel:JPanel = button.parent;<BR>   panel&#8230;.<BR>}  </BLOCKQUOTE> <P>    这种情况非常常见。事件处理函数往往需要执行一些动作来影响其他的对象。我们当然可以把需要被影响到的对象设为类的成员变量，这样在整个类当中它都将是可以见的。但是如果我们无法看见这个对象的时候，我们通常可以通过事件的发出者来找到所需的对象。event.target就是指向事件的发出者。因为我们把按钮放到了面板当中，所以按钮的父组件（parent）就是面板，于是我们通过button.parent就能得到对面板的引用。 </P> <P> </P> <P>    例2.1.1-1包含了完整的程序。只要点击其中的按钮，相应的动作监听函数就会改变面板的背景颜色。 </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><STRONG>package</STRONG><BR>{<BR>     <STRONG>import</STRONG> flash.display.Sprite;<BR>     <STRONG>import</STRONG> flash.events.MouseEvent;<BR> <BR>     <STRONG>import</STRONG> org.aswing.ASColor;<BR>     <STRONG>import</STRONG> org.aswing.AsWingManager;<BR>     <STRONG>import</STRONG> org.aswing.Container;<BR>     <STRONG>import</STRONG> org.aswing.FlowLayout;<BR>     <STRONG>import</STRONG> org.aswing.JButton;<BR>     <STRONG>import</STRONG> org.aswing.JFrame;<BR>     <STRONG>import</STRONG> org.aswing.JPanel; </P> <P> </P> <P>     <STRONG>public</STRONG> <STRONG>class</STRONG> ButtonTest <STRONG>extends</STRONG> Sprite<BR>     {<BR>          <STRONG>public</STRONG> <STRONG>function</STRONG> ButtonTest()<BR>          {<BR>               <STRONG>super</STRONG>();<BR>   <BR>               AsWingManager.initAsStandard(<STRONG>this</STRONG>);<BR>   <BR>               <STRONG>var</STRONG> buttonPanel:JPanel = <STRONG>new</STRONG> JPanel(new FlowLayout());<BR>   <BR>               <STRONG>var</STRONG> yellowButton:JButton = <STRONG>new</STRONG> JButton(&#8221;Yellow&#8221;);<BR>               <STRONG>var</STRONG> blueButton:JButton = <STRONG>new</STRONG> JButton(&#8221;Blue&#8221;);<BR>               <STRONG>var</STRONG> redButton:JButton = <STRONG>new</STRONG> JButton(&#8221;Red&#8221;);<BR>   <BR>               buttonPanel.append(yellowButton);<BR>               buttonPanel.append(blueButton);<BR>               buttonPanel.append(redButton);<BR>   <BR>               buttonPanel.setOpaque(<STRONG>true</STRONG>);<BR>   <BR>               yellowButton.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.__onYellowClick);<BR>               blueButton.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.__onBlueClick);<BR>               redButton.addEventListener(MouseEvent.CLICK, <STRONG>this</STRONG>.__onRedClick); </P> <P>   <BR>               <STRONG>var</STRONG> frame:JFrame = <STRONG>new</STRONG> JFrame(<STRONG>this</STRONG>, &#8220;Button Test&#8221;);<BR>               frame.setSizeWH(300, 250);<BR>   <BR>   <BR>               <STRONG>var</STRONG> c:Container = frame.getContentPane();<BR>               c.append(buttonPanel);<BR>   <BR>               frame.show();<BR>          }<BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> __onYellowClick(e:MouseEvent) :<STRONG>void</STRONG> {<BR>              <STRONG>var</STRONG> button:JButton = e.target <STRONG>as</STRONG> JButton;<BR>              <STRONG>var</STRONG> panel:JPanel = button.parent <STRONG>as</STRONG> JPanel;<BR>   <BR>              panel.setBackground(ASColor.YELLOW);<BR>          }<BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> __onBlueClick(e:MouseEvent) :<STRONG>void</STRONG> {<BR>              <STRONG>var</STRONG> button:JButton = e.target <STRONG>as</STRONG> JButton;<BR>              <STRONG>var</STRONG> panel:JPanel = button.parent <STRONG>as</STRONG> JPanel;<BR>   <BR>              panel.setBackground(ASColor.BLUE);<BR>          }<BR>  <BR>          <STRONG>private</STRONG> <STRONG>function</STRONG> __onRedClick(e:MouseEvent) :<STRONG>void</STRONG> {<BR>              <STRONG>var</STRONG> button:JButton = e.target <STRONG>as</STRONG> JButton;<BR>              <STRONG>var</STRONG> panel:JPanel = button.parent <STRONG>as</STRONG> JPanel;<BR>   <BR>              panel.setBackground(ASColor.RED);<BR>          }<BR>     }<BR>} </P></BLOCKQUOTE> <P><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4><STRONG>API:org.aswing.JButton</STRONG></FONT></P> <UL> <LI> <DIV>JButton(text:String=&#8221;", icon:Icon=null)</DIV></LI></UL> <P>         构造一个JButton对象，参数text是JButton的标签字符串，是一个普通的纯文本字符串，不支持HTML标记。</P> <P>         icon是按钮的图标。</P> <P> </P> <P><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4><STRONG>API:org.aswing.Container</STRONG></FONT></P> <UL> <LI> <DIV>append(com:Component):void</DIV></LI></UL> <P>         把组件com添加到容器中。</P> <P> </P> <P><STRONG><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API:org.aswing.AssetIcon</FONT></STRONG></P> <UL> <LI> <DIV>AssetIcon(asset:DisplayObject=null)</DIV></LI></UL> <P>         构造一个AssetIcon对象。asset是一个普通的DispalayObject，可以是一个Sprite，一个MovieClip，也可以是一个嵌入swf当中的图形元素</P></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=24</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=23</link>
		<comments>http://thiswind.flashseer.org/?p=23#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:31:46 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=23</guid>
		<description><![CDATA[AsWing入门教程 第二章 事件处理   作者：胡矿   著作权所有，请勿转载   www.flashseer.org   Google Doc http://docs.google.com/Doc?id=dnp8gdz_25cpkccc         事件处理在ActionScript3.0编程当中非常重要，ActionScript3.0是基于事件的。本章将介绍AsWing当中的事件处理，下一章将会讲述如何整合使用AsWing提供的大多数组件，并全面讲述这些组件所产生的事件。
]]></description>
			<content:encoded><![CDATA[<p><P><B>AsWing入门教程 第二章 事件处理</B></P> <P> </P> <P><B>作者：胡矿</B></P> <P> </P> <P><B>著作权所有，请勿转载</B></P> <P> </P> <P><A href="http://www.flashseer.org"><B>www.flashseer.org</B></A></P> <P> </P> <P>Google Doc</P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_25cpkccc" target=_blank>http://docs.google.com/Doc?id=dnp8gdz_25cpkccc</A></P> <P> </P> <P> </P> <P>    事件处理在ActionScript3.0编程当中非常重要，ActionScript3.0是基于事件的。本章将介绍AsWing当中的事件处理，下一章将会讲述如何整合使用AsWing提供的大多数组件，并全面讲述这些组件所产生的事件。</P></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=23</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=22</link>
		<comments>http://thiswind.flashseer.org/?p=22#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:30:32 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=22</guid>
		<description><![CDATA[Aswing入门教程 1.6 颜色和填充    作者：胡矿，iiley，Bill       著作权所有，请勿转载      www.flashseer.org    Google Doc http://docs.google.com/Doc?id=dnp8gdz_16d63xzw           Graphics2D允许你利用刷子对象（Bursh）来进行颜色填充。如果要使用多种颜色来填充，那么你可以先选择一种颜色，进行填充；然后再选择另一种颜色，再进行填充。        ASColor类被用来定义颜色。org.aswing.ASColor类提供了代表16种常用颜色的常量值，如表1.6-1所示  表1.6-1      WHITE  BLACK [...]]]></description>
			<content:encoded><![CDATA[<p><DIV style="PADDING-RIGHT: 0pt; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><STRONG>Aswing入门教程 1.6 颜色和填充</STRONG> </DIV> <DIV style="PADDING-RIGHT: 0pt; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"></DIV> <P><STRONG></STRONG></P> <P><STRONG>作者：胡矿，iiley，Bill</STRONG> </P> <P> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>著作权所有，请勿转载</STRONG> </P> <P> </P> <P> </P> <P><A href="http://www.flashseer.org">www.flashseer.org</A> </P> <P> </P> <P>Google Doc</P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_16d63xzw" target=_blank>http://docs.google.com/Doc?id=dnp8gdz_16d63xzw</A></P> <P> </P> <P> </P> <P> </P> <P>    Graphics2D允许你利用刷子对象（Bursh）来进行颜色填充。如果要使用多种颜色来填充，那么你可以先选择一种颜色，进行填充；然后再选择另一种颜色，再进行填充。 </P> <P> </P> <P>    ASColor类被用来定义颜色。org.aswing.ASColor类提供了代表16种常用颜色的常量值，如表1.6-1所示 </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>表1.6-1</STRONG>  <P align=center> <TABLE cellSpacing=0 cellPadding=3 width="60%" border=0> <TBODY> <TR> <TD width="25%">WHITE </TD> <TD width="25%">BLACK </TD> <TD width="25%">HALO_ORANGE </TD> <TD width="25%">MAGENTA </TD></TR> <TR> <TD width="25%">LIGHT_GRAY </TD> <TD width="25%">RED </TD> <TD width="25%">YELLOW </TD> <TD width="25%">CYAN </TD></TR> <TR> <TD width="25%">GRAY </TD> <TD width="25%">PINK </TD> <TD width="25%">GREEN </TD> <TD width="25%">BLUE </TD></TR> <TR> <TD width="25%">DARK_GRAY </TD> <TD width="25%">ORANGE </TD> <TD width="25%">HALO_GREEN </TD> <TD width="25%">HALO_BLUE </TD></TR></TBODY></TABLE></P></BLOCKQUOTE> <P> </P> <P>    例如： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> brush:IBrush = <STRONG>new</STRONG> SolidBrush(ASColor.RED);<BR>g2.fillRectangle(brush, x, y, width, height); </BLOCKQUOTE> <P> </P> <P>    你可以通过创建ASColor对象来定制颜色，这时侯需要提供该颜色的红、绿、蓝颜色构成。三种颜色都是用0-255，即0&#215;00-0xFF（也就是一个字节）之间的整数表示的，ASColor的构造器如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P> ASColor (rgb:uint=0&#215;000000, alpha:Number=1) </P></BLOCKQUOTE> <P> </P> <P>    下面是个定制颜色的例子： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> brush:IBrush = <STRONG>new</STRONG> SolidBrush(ASColor.getASColor(0, 128, 128)); // <EM>灰暗的蓝绿色,也可以用<STRONG>new</STRONG> ASColor(0&#215;008888)构建这个颜色<BR></EM>g2.fillRectangle(brush, 10, 10, 100, 40); </BLOCKQUOTE> <P> </P> <P>    设置背景颜色的方法是使用Component类（JPanel类的祖先）中的setBackground方法。事实上，如果要看到背景颜色，你应该先将JPanel设为不透明。默认情况下，JPanel是透明的。对于AsWing的组件，你可以在运行当中随时改变其背景颜色。 </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> panel:JPanel = <STRONG>new</STRONG> JPanel();<BR>panel.setOpaque(<STRONG>true</STRONG>);<BR>panel.setBackground(ASColor.BLACK);<BR>contentPane.append(panel);</BLOCKQUOTE> <P> </P> <P>     LookAndFeel中通常都定义了组件的一些列默认颜色，这里称为系统颜色，可以通过UIManager.getColor(name)获取指定名称的颜色，比如UIManager.getColor(&#8221;window&#8221;)就会得到窗口背景的颜色，表1.6-2中是常用的系统颜色名称表。<BR></P> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>表1.6-2 系统颜色</STRONG>  <P> </P> <DIV> <TABLE cellSpacing=0 cellPadding=3 width="100%" border=0> <TBODY> <TR> <TD width="50%">activeCaption </TD> <TD width="50%">标题的背景颜色 </TD></TR> <TR> <TD width="50%">activeCaptionText </TD> <TD width="50%">标题的文本颜色 </TD></TR> <TR> <TD width="50%">activeCaptionBorder </TD> <TD width="50%">标题的边框颜色 </TD></TR> <TR> <TD width="50%">inactiveCaption </TD> <TD width="50%">非活动标题的背景颜色 </TD></TR> <TR> <TD width="50%">inactiveCaptionText </TD> <TD width="50%">非活动标题的文本颜色 </TD></TR> <TR> <TD width="50%">inactiveCaptionBorder </TD> <TD width="50%">非活动标题的边框颜色 </TD></TR> <TR> <TD width="50%">window </TD> <TD width="50%">窗口的背景颜色 </TD></TR> <TR> <TD width="50%">windowBorder </TD> <TD width="50%">窗口的边框颜色 </TD></TR> <TR> <TD width="50%">windowText </TD> <TD width="50%">窗口内文本的颜色 </TD></TR> <TR> <TD width="50%">menu </TD> <TD width="50%">菜单的背景颜色 </TD></TR> <TR> <TD width="50%">menuText </TD> <TD width="50%">菜单的文本颜色 </TD></TR> <TR> <TD width="50%">text </TD> <TD width="50%">文本的背景颜色 </TD></TR> <TR> <TD width="50%">textText </TD> <TD width="50%">文本的文本颜色 </TD></TR> <TR> <TD width="50%">textHighlight </TD> <TD width="50%">高亮文本的背景颜色 </TD></TR> <TR> <TD width="50%">textHightliteText </TD> <TD width="50%">高亮文本的文本颜色 </TD></TR> <TR> <TD width="50%">control </TD> <TD width="50%">空间的背景颜色 </TD></TR> <TR> <TD width="50%">controlText </TD> <TD width="50%">控件的文本颜色 </TD></TR> <TR> <TD width="50%">controlLiHighlight </TD> <TD width="50%">控件的轻高亮颜色 </TD></TR> <TR> <TD width="50%">controlHighlight </TD> <TD width="50%">控件的高亮颜色 </TD></TR> <TR> <TD width="50%">controlShadow </TD> <TD width="50%">控件的阴影颜色 </TD></TR> <TR> <TD width="50%">controlDkShadow </TD> <TD width="50%">控件的暗阴影颜色 </TD></TR> <TR> <TD width="50%">scrollbar </TD> <TD width="50%">滚动栏的背景颜色 </TD></TR></TBODY></TABLE></DIV></BLOCKQUOTE> <P> </P> <P><FONT size=4><FONT style="BACKGROUND-COLOR: #ff6600">API：org.aswing.ASColor </FONT></FONT></P> <UL> <LI> <DIV>ASColor (rgb:uint=0&#215;000000, alpha:Number=1) </DIV></LI></UL> <P>       创建一个颜色对象。 </P> <P>       参数：rgb，用RGB颜色空间表示的颜色值。 </P> <P>               alpha，颜色的透明度（0.0-1.0） </P> <P> </P> <UL> <LI> <DIV>getASColor(r:uint, g:uint, b:uint, a:Number=1):ASColor </DIV></LI></UL> <P>       根据R、G、B的值获取一个新的颜色对象。 </P> <P>       参数：r    红色值（0-255） </P> <P>               g   绿色值（0-255） </P> <P>               b   蓝色值（0-255） </P> <P>               a   alpha，颜色的透明度（0.0-1.0）        </P> <P> </P> <UL> <LI> <DIV>getRGBWith(rr:uint, gg:uint, bb:uint):uint </DIV></LI></UL> <P>       根据R、G、B获取某一个颜色的颜色值 </P> <P> </P> <P><FONT style="BACKGROUND-COLOR: #ff6600" size=4>API:org.aswing.Component</FONT> </P> <UL> <LI> <DIV>setBackground(c:ASColor):<STRONG>void</STRONG> </DIV></LI></UL> <P>       设置背景颜色。背景颜色只有在组件为不透明的时候才会被显示。 </P> <P>       参数：c   新的背景颜色。 </P> <P> </P> <UL> <LI> <DIV>setOpaque(b:Boolean):<STRONG>void</STRONG> </DIV></LI></UL> <P>       设置背景是否透明。 </P> <P>       参数：b  （<STRONG>true</STRONG>，<STRONG>false</STRONG>）。 </P> <P>                   true表示不透明 </P> <P>                   false表示透明 </P> <P> </P> <P> </P> <P> </P> <P><STRONG><FONT size=4>填充形状</FONT></STRONG> </P> <P><STRONG><FONT size=4>    </FONT></STRONG></P> <P><FONT size=4><STRONG>    </STRONG></FONT><FONT size=2>你可以用一个颜色来填充闭合形状（例如矩形或椭圆）的内部区域。只需用fill代替draw：</FONT> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><STRONG>var</STRONG> brush:IBrush = <STRONG>new</STRONG> SolidBrush(ASColor.RED); <EM>// 创建一只红色的纯色刷子<BR></EM>g.fillRectangle(brush, 10, 10, 100, 100); <EM>// 填充矩形</EM> </P></BLOCKQUOTE> <P> </P> <P><FONT size=4><STRONG>刷子对象</STRONG></FONT> </P> <P><STRONG><FONT size=4>    </FONT></STRONG></P> <P>    你可以用刷子对象对形状做各种填充。所有的刷子对象都实现IBrush接口。 </P> <P> </P> <P>    AsWing提供了3种刷子，他们分别是纯色刷子（SolidBrush）、过渡色刷子（GradientBrush）、位图刷子（BitmapBrush），下面我们分别介绍这三种刷子。 </P> <P> </P> <P>    纯色刷子，这是最常用的一种刷子，它用一种颜色来填充区域，如图1.6-1所示，如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><STRONG>var</STRONG> solidBrush:IBrush = <STRONG>new</STRONG> SolidBrush(ASColor.RED);<BR>g2.fillRectangle(solidBrush, bounds.x+10, bounds.y+10, 100, 100); </P></BLOCKQUOTE> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_21dnbdmdcn"> </DIV>（图1.6-1） </DIV> <P> </P> <P> </P> <P>    过度色刷子可以表现颜色过渡效果。创建过渡色刷子需要提供过渡类型、颜色渐变范围和透明度范围。过渡类型线性过渡（GradientBrush.LINEAR）和圆形过渡（GradientBrush.RADIAL）两种。 </P> <P> </P> <P>    线性过渡要先设定一个颜色列表，这个列表当中的颜色是将要参与渐变的各种关键颜色，如下是三个颜色的渐变色列表： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> colors:Array = [0&#215;000000, 0xFF0000, 0&#215;00FF00, 0&#215;0000FF, 0&#215;000000]; <EM>// 光谱黑-红-绿-蓝-黑，两端的黑色表示不可见波段</EM></BLOCKQUOTE> <P>     渐变色的透明度列表依次对应渐变色列表当中每一种关键颜色的透明度，在AS3.0当中，透明度不再用一个（0-100）的整数表示，而是改用一个（0-1）的小数表示，对于AS2程序员来说，这一点需要注意。如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> alphas:Array = [0, 1, 1, 1, 0]; <EM>// 两端的透明度为0，让填充区自然溶于背景当中 </EM></BLOCKQUOTE> <P>     比率列表当中的每一项表示对应的一种关键颜色的100%采样值距离在填充框当中的位置比例。其中的每一个项是一个（0&#215;00-0xFF）的数，填充的起点是0&#215;00，填充的中止位置是0xFF。如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> ratios:Array = [0&#215;00, 0&#215;3F, 0&#215;7E, 0xBD, 0xFF]; <EM>// 这5个数对 （0-0xFF）区间4等分，它们分别是： 0 * 0xFF/4, 1 * 0xFF/4, 2 * 0xFF/4, 3 * 0xFF/4, 4 * 0xFF/4</EM></BLOCKQUOTE> <P>     填充矩阵是一个矩阵（Matrix）对象。需要制定填充框的大小、填充的角度（默认为0度，即延x正方向从左到右填充）以及x、y方向的偏移量。一般来说，填充框的长和宽和要填充的图形的长宽相同，填充框的x,y偏移量分别就是要填充的图形的左上角定点的x,y坐标，如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> matrix:Matrix = <STRONG>new</STRONG> Matrix();<BR>matrix.createGradientBox(100, 100, 0, bounds.x+10, bounds.y+120); <EM>// 填充框的大小为 100×100 像素，填充方向为水平从左向右，偏移量是绘图区域的左边缘向右10像素</EM></BLOCKQUOTE> <P>     在颜色列表，透明度列表，比率列表以及填充矩阵都定义好之后，就可以开始填充了，如图1.6-2，如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> linear:IBrush = <STRONG>new</STRONG> GradientBrush(GradientBrush.LINEAR, colors, alphas, ratios, matrix);<BR>g.fillRectangle(linear, bounds.x+10, bounds.y+120, 100, 100);</BLOCKQUOTE> <P> </P> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_22dk3sdtm2"> </DIV> <P> （图1.6-2） </P> <P> </P> <P>  <BR></P> <P> </P> <P>    圆形填充（RADIAL）又叫做放射状填充，这种填充是从起点开始向周围所有方向填充，可以看到一圈一圈的等色线。 </P> <P> </P> <P>    圆形填充的操作和线性填充一样，只要在填充的时候将填充类型设为圆形填充（GradientBrush.RADIAL）即可，如图1.6-3所示，如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> linear:IBrush = <STRONG>new</STRONG> GradientBrush(GradientBrush.RADIAL, colors, alphas, ratios, matrix);<BR>g.fillRectangle(linear, bounds.x+10, bounds.y+230, 100, 100); </BLOCKQUOTE> <P> </P> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_23fpvr4kdr"> </DIV> <P> （图1.6-3） </P> <P><BR>  </P> <P>    例1.6-1是完整的示例代码，运行结果见图1.6-3 </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>例1.6-1</STRONG>  <P> </P> <P><STRONG>package</STRONG><BR>{<BR>    <STRONG>import</STRONG> flash.display.Sprite;<BR> <BR>    <STRONG>import</STRONG> org.aswing.ASColor;<BR>    <STRONG>import</STRONG> org.aswing.Component;<BR>    <STRONG>import</STRONG> org.aswing.Container;<BR>    <STRONG>import</STRONG> org.aswing.JFrame;<BR>    <STRONG>import</STRONG> org.aswing.graphics.Graphics2D;<BR>    <STRONG>import</STRONG> org.aswing.graphics.IBrush;<BR>    <STRONG>import</STRONG> org.aswing.graphics.SolidBrush; </P> <P><BR>    <STRONG>public</STRONG> <STRONG>class</STRONG> FillTest <STRONG>extends</STRONG> Sprite<BR>    {<BR>        <STRONG>public</STRONG> <STRONG>function</STRONG> FillTest()<BR>        {            <BR>            <STRONG>var</STRONG> frame:JFrame = <STRONG>new</STRONG> JFrame(this);<BR>            frame.setSizeWH(400, 370);<BR>   <BR>            <STRONG>var</STRONG> c:Container = frame.getContentPane();<BR>            c.setBackgroundDecorator(<STRONG>new</STRONG> MyCanvas()); </P> <P> </P> <P>            frame.show();<BR>        }<BR>  <BR>    }<BR>}<BR>    <STRONG>import</STRONG> org.aswing.GroundDecorator;<BR>    <STRONG>import</STRONG> flash.display.Shape;<BR>    <STRONG>import</STRONG> org.aswing.Component;<BR>    <STRONG>import</STRONG> org.aswing.graphics.Graphics2D;<BR>    <STRONG>import</STRONG> org.aswing.geom.IntRectangle;<BR>    <STRONG>import</STRONG> flash.display.DisplayObject;<BR>    <STRONG>import</STRONG> org.aswing.graphics.IBrush;<BR>    <STRONG>import</STRONG> org.aswing.graphics.SolidBrush;<BR>    <STRONG>import</STRONG> org.aswing.ASColor;<BR>    <STRONG>import</STRONG> org.aswing.graphics.GradientBrush;<BR> <BR>    <STRONG>import</STRONG> flash.geom.Matrix;<BR>  </P> <P><STRONG>class</STRONG> MyCanvas <STRONG>implements</STRONG> GroundDecorator {<BR> <BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> shape:Shape = <STRONG>new</STRONG> Shape();<BR> <BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> H_GAP:uint = 10;<BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> V_GAP:uint = 10;<BR> <BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> WIDTH:uint = 100;<BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> HEIGHT:uint = 100;<BR> <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> updateDecorator(com:Component, g:Graphics2D, bounds:IntRectangle):<STRONG>void</STRONG> {<BR>        <STRONG>var</STRONG> g2:Graphics2D = <STRONG>new</STRONG> Graphics2D(<STRONG>this</STRONG>.shape.graphics);<BR>        g2.clear();<BR>  <BR>        <STRONG>var</STRONG> rectBounds:IntRectangle = <STRONG>new</STRONG> IntRectangle();<BR>  <BR>        <EM>//fill solid rect</EM><BR>  <BR>        rectBounds.x = bounds.x + H_GAP;<BR>        rectBounds.y = bounds.y + V_GAP;<BR>        rectBounds.width = WIDTH;<BR>        rectBounds.height = HEIGHT;<BR>  <BR>        <STRONG>var</STRONG> solidBrush:IBrush = <STRONG>new</STRONG> SolidBrush(ASColor.RED);<BR>        g2.fillRectangle(solidBrush, rectBounds.x, rectBounds.y, rectBounds.width, rectBounds.height);<BR>  <BR>       <EM> //fill liner grandient rect<BR></EM>  <BR>        rectBounds.y += HEIGHT; // move shape rect<BR>        rectBounds.y += V_GAP;<BR>  <BR>        <STRONG>var</STRONG> colors:Array = [0&#215;000000, 0xFF0000, 0&#215;00FF00, 0&#215;0000FF, 0&#215;000000];<BR>        <STRONG>var</STRONG> alphas:Array = [0, 1, 1, 1, 0];<BR>        <STRONG>var</STRONG> ratios:Array = [0&#215;00, 0&#215;3F, 0&#215;7E, 0xBD, 0xFF];<BR>        <STRONG>var</STRONG> matrix:Matrix = <STRONG>new</STRONG> Matrix();<BR>  <BR>        matrix.createGradientBox(rectBounds.width, rectBounds.height, 0, rectBounds.x, rectBounds.y);<BR>        <STRONG>var</STRONG> linear:IBrush = <STRONG>new</STRONG> GradientBrush(GradientBrush.LINEAR, colors, alphas, ratios, matrix);<BR>  <BR>        g.fillRectangle(linear, rectBounds.x, rectBounds.y, rectBounds.width, rectBounds.height);<BR>  <BR>        <EM>//fill radial grandient</EM><BR>  <BR>        rectBounds.y += HEIGHT; // move shape rect<BR>        rectBounds.y += V_GAP;<BR>  <BR>        matrix.createGradientBox(rectBounds.width, rectBounds.height, 0, rectBounds.x, rectBounds.y);<BR>        <STRONG>var</STRONG> radial:IBrush = <STRONG>new</STRONG> GradientBrush(GradientBrush.RADIAL, colors, alphas, ratios, matrix);<BR>  <BR>        g.fillRectangle(radial, rectBounds.x, rectBounds.y, rectBounds.width, rectBounds.height);<BR>    }<BR> <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> getDisplay(c:Component):DisplayObject {<BR>        <STRONG>return</STRONG> <STRONG>this</STRONG>.shape;<BR>    }<BR>} </P></BLOCKQUOTE> <P>     </P> <P>    位图刷子可以让你用位图图像来做填充，如图1.6-4： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> brush:IBrush = <STRONG>new</STRONG> BitmapBrush(bm.bitmapData, null, false, false); <EM>// bm是一个Bitmap实例<BR></EM>g.fillRectangle(brush, bounds.x, bounds.y, bounds.width, bounds.height);</BLOCKQUOTE> <DIV style="PADDING-RIGHT: 0pt; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dgfxqns5_1g4jkhc56"><BR></DIV> <P style="PADDING-RIGHT: 0pt; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"> （图1.6-4） </P> <P style="PADDING-RIGHT: 0pt; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">   <BR></P> <P style="PADDING-RIGHT: 0pt; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">    BitmapBrush构造函数有4个参数,形式为 BitmapBrush(bitmap:BitmapData, matrix:Matrix = null, repeat:Boolean = <STRONG>true</STRONG>, smooth:Boolean = <STRONG>false</STRONG>), 第一个参数bitmap即是要绘制的位图图像数据；第二个参数可以使绘制的图像进行变形,比如缩放、旋转、位移等；第三个参数指绘制的时候，如果绘制区域比图像大，是否进行平铺，在有些情况这个参数很有用，比如一个砖墙的背景，可以通过平铺一个很小的砖块图像形成一面墙的效果；第四个参数指定是否进行平滑处理。这几个参数它们的意义跟flash.display.Graphics.beginBitmapFill(bitmap:BitmapData, matrix:Matrix = null, repeat:Boolean = <STRONG>true</STRONG>, smooth:Boolean = <STRONG>false</STRONG>)中对应参数的意义一样,可参阅Flash CS3或者FlexBuilder帮助文档相关内容. </P> <P style="PADDING-RIGHT: 0pt; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">    例1.6-2是完整的示例代码，窗口中放置了4个JPanel，他们用同一个位图图形，不同的参数进行绘制，用运行结果见图1.6-5 </P> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_26tg7xv2k5"></DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">（图1.6-5）</DIV> <BLOCKQUOTE dir=ltr style="PADDING-RIGHT: 0pt; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1em; MARGIN-RIGHT: 0px; PADDING-TOP: 1em; TEXT-ALIGN: left"><STRONG>例1.6-2</STRONG>  <P> </P> <P><STRONG></STRONG></P> <P><STRONG>package</STRONG>{<BR><BR><STRONG>import</STRONG> flash.display.Sprite;<BR><STRONG>import</STRONG> org.aswing.*;<BR><STRONG>import</STRONG> org.aswing.graphics.*;<BR><STRONG>import</STRONG> flash.geom.Matrix;<BR><BR><STRONG>public</STRONG> <STRONG>class</STRONG> FillTest2 <STRONG>extends</STRONG> Sprite{<BR>    <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> FillTest2(){<BR>        <STRONG>super</STRONG>();<BR>        AsWingManager.initAsStandard(<STRONG>this</STRONG>);<BR>        var frame:JFrame = new JFrame(<STRONG>this</STRONG>);<BR>        <BR>        <STRONG>var</STRONG> p1:JPanel = <STRONG>new</STRONG> JPanel();<BR>        p1.setBackgroundDecorator(<STRONG>new</STRONG> MyBitmapDecorator(null, <STRONG>false</STRONG>, <STRONG>false</STRONG>));<BR>        var p2:JPanel = <STRONG>new</STRONG> JPanel();<BR>        p2.setBackgroundDecorator(<STRONG>new</STRONG> MyBitmapDecorator(null, <STRONG>true</STRONG>, <STRONG>false</STRONG>));<BR>        <BR>        <STRONG>var</STRONG> p3:JPanel = <STRONG>new</STRONG> JPanel();<BR>        <STRONG>var</STRONG> matrix1:Matrix = <STRONG>new</STRONG> Matrix();<BR>        matrix1.scale(3, 3);<BR>        p3.setBackgroundDecorator(<STRONG>new</STRONG> MyBitmapDecorator(matrix1, <STRONG>false</STRONG>, <STRONG>false</STRONG>));<BR>        <BR>        <STRONG>var</STRONG> p4:JPanel = <STRONG>new</STRONG> JPanel();<BR>        <STRONG>var</STRONG> matrix2:Matrix = <STRONG>new</STRONG> Matrix();<BR>        matrix2.rotate(Math.PI/4);<BR>        p4.setBackgroundDecorator(<STRONG>new</STRONG> MyBitmapDecorator(matrix2, <STRONG>true</STRONG>, <STRONG>true</STRONG>));<BR>        <BR>        <STRONG>var</STRONG> pane:Container = <STRONG>new</STRONG> JPanel(<STRONG>new</STRONG> GridLayout(2, 2, 2, 2));<BR>        pane.appendAll(p1, p2, p3, p4);<BR>        frame.setContentPane(pane);<BR>        <BR>        frame.setSizeWH(500, 400);<BR>        frame.show();<BR>    }<BR>    <BR>}<BR>}<BR><BR><STRONG>import</STRONG> org.aswing.*;<BR><STRONG>import</STRONG> org.aswing.graphics.*;<BR><STRONG>import</STRONG> org.aswing.geom.*;<BR><STRONG>import</STRONG> flash.display.*;<BR><STRONG>import</STRONG> flash.geom.Matrix;<BR>    <BR><STRONG>class</STRONG> MyBitmapDecorator <STRONG>implements</STRONG> GroundDecorator{<BR>    <BR>    [<STRONG>Embed</STRONG>(source=&#8221;img1.jpg&#8221;)]<BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> imgClass:Class;<BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> matrix:Matrix;<BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> repeat:Boolean;<BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> smooth:Boolean;<BR>    <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> MyBitmapDecorator(matrix:Matrix = null, repeat:Boolean = <STRONG>true</STRONG>, smooth:Boolean = <STRONG>false</STRONG>){<BR>        <STRONG>this</STRONG>.matrix = matrix;<BR>        <STRONG>this</STRONG>.repeat = repeat;<BR>        <STRONG>this</STRONG>.smooth = smooth;<BR>    }<BR>    <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> updateDecorator(com:Component, g:Graphics2D, bounds:IntRectangle):<STRONG>void</STRONG> {<BR>        <STRONG>var</STRONG> bm:Bitmap = <STRONG>new</STRONG> imgClass() <STRONG>as</STRONG> Bitmap;<BR>        <STRONG>var</STRONG> brush:IBrush = <STRONG>new</STRONG> BitmapBrush(bm.bitmapData, matrix, repeat, smooth);<BR>        g.fillRectangle(brush, bounds.x, bounds.y, bounds.width, bounds.height);<BR>    }<BR>    <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> getDisplay(c:Component):DisplayObject{<BR>        <STRONG>return</STRONG> null;<BR>    }<BR>} </P></BLOCKQUOTE> <P><FONT style="BACKGROUND-COLOR: #ff6600" size=4>API:org.aswing.graphics.SolidBrush</FONT></P> <UL> <LI>SolidBrush(color:ASColor)</LI></UL> <P>       创建一把纯色刷子。</P> <P> </P> <P><FONT style="BACKGROUND-COLOR: #ff6600" size=4>API:org.aswing.graphics.GradientBrush</FONT></P> <UL> <LI>GradientBrush(fillType:String , </LI></UL> <P>                           colors:Array, </P> <P>                           alphas:Array, </P> <P>                           ratios:Array, </P> <P>                           matrix:Matrix) </P> <P>       创建一把过渡色填充刷子。</P> <P>       参数：</P> <P>             fillType，填充类型，有线性填充（GradientBrush.LINEAR）和放射填充（GradientBrush.RADIAL）</P> <P>             colors，参与过渡填充的关键颜色列表。颜色用一个（0&#215;000000-0xFFFFFF）之间的整数表示。</P> <P>             alphas，参与过渡填充的关键颜色的透明度的列表。透明度用一个（0-1）的小数表示。</P> <P>             ratios，参与过渡填充的关键色的位置比例。位置比例是一个（0&#215;00-0xFF）的小数，0&#215;00表示在填充的起点位置，0xFF表示在填充的结束位置。位置比例是一个比例，不是绝的像素位置。</P> <P>             matrix，填充矩阵，一般通过 matrix.matrix.createGradientBox(width, height, angle, x_offset, y_offset)方法来创建。</P> <P> </P> <P> </P> <P><FONT style="BACKGROUND-COLOR: #ff6600" size=4>API:flash.geom.Matrix</FONT></P> <UL> <LI>createGradientBox(width, height, angle, x_offset, y_offset):<STRONG>void</STRONG> </LI></UL> <P>       创建一个填充矩阵。</P> <P>       参数：</P> <P>             width，填充框的宽度，单位是像素。</P> <P>             height，填充框的高度，单位是像素。</P> <P>             angle，填充的角度，单位是弧度</P> <P>             x_offset，填充框左上角的x坐标</P> <P>             y_offset，填充框左上角的y坐标</P></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=22</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=21</link>
		<comments>http://thiswind.flashseer.org/?p=21#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:29:27 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=21</guid>
		<description><![CDATA[AsWing入门教程 1.5 2D图形       作者：胡矿      著作权所有，请勿转载       www.flashseer.org     Google Doc http://docs.google.com/Doc?id=dnp8gdz_13dn73pw           从ActionScript3.0发布开始，Flash也有了专门绘制基本图形的工具类Graphics，可以用它来绘制直线、矩形、椭圆等。但是这些绘制非常有限。        Graphics2D类封装很多图形操作，用Graphics2D绘制图形很方便，例如图1.5-1的图形。     （图1.5-1）  [...]]]></description>
			<content:encoded><![CDATA[<p><P><STRONG>AsWing入门教程 1.5 2D图形</STRONG> </P> <P> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>作者：胡矿</STRONG> </P> <P> </P> <P> </P> <P><STRONG>著作权所有，请勿转载</STRONG> </P> <P> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG><A href="http://www.flashseer.org/"><FONT color=#810081>www.flashseer.org</FONT></A></STRONG>  </P> <P> </P> <P>Google Doc</P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_13dn73pw" target=_blank>http://docs.google.com/Doc?id=dnp8gdz_13dn73pw</A></P> <P> </P> <P> </P> <P> </P> <P>    <FONT face=Verdana>从ActionScript3.0发布开始，Flash也有了专门绘制基本图形的工具类Graphics，可以用它来绘制直线、矩形、椭圆等。但是这些绘制非常有限。</FONT> </P> <P> </P> <P><FONT face=Verdana>    Graphics2D类封装很多图形操作，用Graphics2D绘制图形很方便，例如图1.5-1的图形。</FONT></P> <P> </P> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_15ddnwn6d9"></DIV> <P> （图1.5-1）</P> <P> </P> <P><FONT face=Verdana>    如果你要得到一个Graphics2D对象，你只需要将一个普通的Graphics对象封装进去就行了，如：</FONT> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><FONT face=Verdana> <STRONG>protected</STRONG> <STRONG>function</STRONG> paint(bounds:IntRectangle) :<STRONG>void</STRONG> {<BR>     <STRONG>super</STRONG>.paint(bounds);<BR>  <BR>     <STRONG>var</STRONG> g2:Graphics2D = <STRONG>new</STRONG> Graphics2D(<STRONG>this</STRONG>.graphics);</FONT> </P> <P><FONT face=Verdana>     &#8230;&#8230;<BR> }</FONT>  </P></BLOCKQUOTE> <P> </P> <P>    面板类（JPanel）是容器类（Container）的子类，它们从Component类继承了setBackgroundDecorator方法。setBackgroundDecorator方法允许用一个实现GroundDecorator接口的类作为JPanel的背景。JPanel默认的背景是透明的，就是它什么都不显示。我们可以通过扩展GroundDecorator类来改变JPanel的背景，对于图形编程我们推荐采用这种方式。</P> <P> </P> <P>    背景装饰器接口（GroundDecorator）是装饰器接口（Decorator）的子类，从Decorator接口当中继承了getDisplay方法，并且GroundDecorator接口还声明了updateDecorator方法，所以实现GroundDecorator就需要实现这两个方法，如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P> <STRONG>class</STRONG> MyBackground <STRONG>implements</STRONG> GroundDecorator {</P> <P> </P> <P>    <STRONG>private</STRONG> <STRONG>var</STRONG> shape:Shape;</P> <P> </P> <P>    <STRONG>public</STRONG> <STRONG>function</STRONG> MyBackground() {</P> <P>          <STRONG>this</STRONG>.<STRONG>shape</STRONG> = <STRONG>new</STRONG> Shape();</P> <P>    }<BR> <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> updateDecorator(c:Component, g:Graphics2D, bounds:IntRectangle) : <STRONG>void</STRONG>{<BR>         shape.graphics.clear();<BR>         g = <STRONG>new</STRONG> Graphics2D(shape.graphics);    </P> <P> </P> <P>         &#8230;&#8230;</P> <P>   }<BR> <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> getDisplay(c:Component) : DisplayObject{<BR>        <STRONG>return</STRONG> <STRONG>this</STRONG>.shape;<BR>    }<BR>}</P></BLOCKQUOTE> <P> </P> <P>    你可以直接用updateDecorator方法的Graphics2D参数进行绘制，但是我们建议新建一个Shape对象并且在新建的Shape对象上进行绘制，AsWing将通过getDisplay方法获取这个Shape对象，并且把它放置到容器当中。</P> <P> </P> <P>    扩展了GroundDecorator之后需要将扩展的对象作为JPanel的背景面板修饰器（BackgroundDecorator）。如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P> <STRONG>var</STRONG> ground:GroundDecorator = &#8230;&#8230;</P> <P> panel.setBackgroundDecorator(ground);</P></BLOCKQUOTE> <P>    容器组件（Container）包含一个内容窗格和一个背景窗格，如图1.5-1所示。在上一节当中，我们直接在内容窗格当中绘制，但是由于内容窗格同时也会放置其它组件，所以我们推荐在背景窗格上绘制。</P> <P> </P> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_14fssgr7dk"></DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">（图1.5-1）</DIV> <P> </P> <P>    用Graphics2D绘制形状是很容易的。在绘制之前需要先创建一只笔（Pen）或者一把刷子（Brush），笔用来绘制线条，刷子则可以用来填充颜色。</P> <P> </P> <P>    构造一只笔（Pen）需要提供颜色和线条的粗细，如下代码定义了一支颜色为黑色，线条为2像素粗的笔：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P> var pen:Pen = new Pen(ASColor.BLACK, 2);</P></BLOCKQUOTE> <P> </P> <P> </P> <P> </P> <HR>  <P> </P> <P><STRONG>笔记：</STRONG></P> <P>在AsWing当中，颜色被封装到ASColor对象当中，ASColor定义了一些常用的颜色的颜色对象，比如ASColor.WHITE、ASColor.BLACK，如果需要定义特定的颜色，可以通过静态方法ASColor.getASColor来获取所需颜色的颜色对象，比如要得到红色（Red=0xFF，Green=0&#215;0，Blue=0&#215;0），可以用如下代码：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P>var red:ASColor = ASColor.getASColor(0xFF, 0&#215;0, 0&#215;0); </P> <P> </P></BLOCKQUOTE> <P>关于颜色的操作，在下一节还会继续介绍。</P> <P> </P> <HR>  <P> </P> <P> </P> <P> </P> <P>    若要绘制一条直线，需要提供两个端点的坐标(x1,y1)和(x2,y2)，如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P> g.drawLine(pen, x1, y1, x2, y2);</P></BLOCKQUOTE> <P> </P> <P>    若要绘制一个矩形，需要提供左上角断点的坐标，以及矩形的宽度和高度，语法如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P> g.drawRectangle(pen, x, y, width, height);</P></BLOCKQUOTE> <P> </P> <P>    对于椭圆的描述是通过它的外切矩形。所以若要绘制一个椭圆，需要提供其外切矩形左上角顶点的位置和这个外接矩形的长度、宽度，语法如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P dir=ltr style="MARGIN-RIGHT: 0px">g.drawEllipse(pen, x, y, width, height);</P></BLOCKQUOTE> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px">     绘制圆形的时候，需要提供的是圆心的坐标和圆形的半径，如下：</P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P dir=ltr style="MARGIN-RIGHT: 0px">g.drawCircle(pen, x, y, radius); </P></BLOCKQUOTE> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px">     例1-4中的程序绘制了：</P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">一个矩形；</DIV> <LI> <DIV style="MARGIN-RIGHT: 0px">该矩形的内接椭圆；</DIV> <LI> <DIV style="MARGIN-RIGHT: 0px">该矩形的一条对角线；</DIV> <LI> <DIV style="MARGIN-RIGHT: 0px">一个和该矩形同心的圆。</DIV> <LI> <DIV style="MARGIN-RIGHT: 0px">所有图形都绘制在框架的中央，如果框架改变大小，图形也会随之改变</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px"> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P style="MARGIN-RIGHT: 0px"><STRONG>例1-4 DrawTest.as</STRONG></P> <P style="MARGIN-RIGHT: 0px"><STRONG></STRONG> </P> <P style="MARGIN-RIGHT: 0px"><STRONG>package</STRONG><BR>{<BR>    <STRONG>import</STRONG> flash.display.Sprite;<BR>    <BR>    <STRONG>import</STRONG> org.aswing.AsWingManager;<BR>    <STRONG>import</STRONG> org.aswing.Container;<BR>    <STRONG>import</STRONG> org.aswing.GroundDecorator;<BR>    <STRONG>import</STRONG> org.aswing.JFrame;<BR>    <STRONG>import</STRONG> org.aswing.JPanel;</P> <P style="MARGIN-RIGHT: 0px">    <STRONG>public</STRONG> <STRONG>class</STRONG> DrawTest <STRONG>extends</STRONG> Sprite<BR>    {<BR>        <STRONG>public</STRONG> <STRONG>function</STRONG> DrawTest() {<BR>            <BR>            AsWingManager.initAsStandard(<STRONG>this</STRONG>);<BR>            <BR>            <STRONG>var</STRONG> frame:JFrame = <STRONG>new</STRONG> JFrame();<BR>            frame.setTitle(&#8221;Draw test&#8221;);<BR>            frame.setSizeWH(DEFAULT_WIDTH, DEFAULT_HEIGHT);<BR>            <BR>            <STRONG>var</STRONG> panel:JPanel = <STRONG>new</STRONG> JPanel();<BR>            <STRONG>var</STRONG> ground:GroundDecorator = <STRONG>new</STRONG> MyGroundDecorator();<BR>            panel.setBackgroundDecorator(ground);<BR>            <BR>            <STRONG>var</STRONG> contentPane:Container = frame.getContentPane();<BR>            contentPane.append(panel);<BR>            <BR>            frame.show();<BR>        }<BR>        <BR>        <STRONG>public</STRONG> <STRONG>static</STRONG> <STRONG>const</STRONG> DEFAULT_WIDTH:uint = 400;<BR>        <STRONG>public</STRONG> <STRONG>static</STRONG> <STRONG>const</STRONG> DEFAULT_HEIGHT:uint = 300;<BR>    }<BR>}</P> <P style="MARGIN-RIGHT: 0px"> </P> <P style="MARGIN-RIGHT: 0px"><STRONG>import</STRONG> flash.display.Shape;<BR><STRONG>import</STRONG> flash.display.DisplayObject;<BR>    <BR><STRONG>import</STRONG> org.aswing.GroundDecorator;<BR><STRONG>import</STRONG> org.aswing.Component;<BR><STRONG>import</STRONG> org.aswing.graphics.Graphics2D;<BR><STRONG>import</STRONG> org.aswing.geom.IntRectangle;<BR><STRONG>import</STRONG> org.aswing.graphics.Pen;<BR><STRONG>import</STRONG> org.aswing.ASColor;<BR>    </P> <P style="MARGIN-RIGHT: 0px"><STRONG>class</STRONG> MyGroundDecorator <STRONG>implements</STRONG> GroundDecorator {<BR>    <BR>    <STRONG>private</STRONG> <STRONG>var</STRONG> shape:Shape;<BR>    <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> MyGroundDecorator() {<BR>        <STRONG>this</STRONG>.shape = <STRONG>new</STRONG> Shape();<BR>    }<BR>    <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> updateDecorator(com:Component, g:Graphics2D, bounds:IntRectangle) : <STRONG>void</STRONG> {<BR>        g = <STRONG>new</STRONG> Graphics2D(shape.graphics);</P> <P style="MARGIN-RIGHT: 0px"> </P> <P style="MARGIN-RIGHT: 0px">        <EM><FONT color=#009900>//clear before draw<BR></FONT></EM>        <BR>        g.clear();<BR>        <BR>        <EM><FONT color=#009900>//create a pen for drawing</FONT></EM><BR>        <BR>        <STRONG>var</STRONG> pen:Pen = <STRONG>new</STRONG> Pen(ASColor.BLACK);<BR>        <BR>        <EM><FONT color=#33ff33><FONT color=#009900>//draw line</FONT><BR></FONT></EM>        <BR>        <STRONG>var</STRONG> x1:uint = bounds.x+100;<BR>        <STRONG>var</STRONG> y1:uint = bounds.y+50;<BR>        <BR>        <STRONG>var</STRONG> x2:uint = bounds.x + bounds.width - 100;<BR>        <STRONG>var</STRONG> y2:uint = bounds.y + bounds.height - 50;<BR>        <BR>        g.drawLine(pen, x1, y1, x2, y2);<BR>        <BR>        <FONT color=#009900><EM>//draw rectangle 1<BR></EM></FONT>        <BR>        <STRONG>var</STRONG> width:uint = x2 - x1;<BR>        <STRONG>var</STRONG> height:uint = y2 - y1;<BR>        <BR>        g.drawRectangle(pen, x1, y1, width, height);<BR>        <BR>        <EM><FONT color=#009900>//draw ellipse<BR></FONT></EM>        <BR>        g.drawEllipse(pen, x1, y1, width, height);<BR>        <BR>        <EM><FONT color=#009900>//draw circle</FONT></EM><BR>        <BR>        <STRONG>var</STRONG> x:uint = bounds.x + Math.round(bounds.width/2);<BR>        <STRONG>var</STRONG> y:uint = bounds.y + Math.round(bounds.height/2);<BR>        <STRONG>var</STRONG> radius:uint = Math.round(Math.min(bounds.width, bounds.height)/2);<BR>        <BR>        g.drawCircle(pen, x, y, radius);<BR>    }<BR>    <BR>    <STRONG>public</STRONG> <STRONG>function</STRONG> getDisplay(c:Component) : DisplayObject {<BR>        <STRONG>return</STRONG> <STRONG>this</STRONG>.shape;<BR>    }<BR>}</P></BLOCKQUOTE> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API：org.aswing.GroundDecorator</FONT></P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">updateDecorator(c:Component, g:Graphics2D, bounds:IntRectangle) : <STRONG>void</STRONG></DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">更新装饰器的显示，这个方法将会在显示区域发上改变的适合哦自动调用，比如框架的尺寸发生改变的时候，还有第一次显示的时候。</P> <P style="MARGIN-RIGHT: 0px">参数c是被装饰的组件的引用，也就是这个装饰器的所有者。</P> <P style="MARGIN-RIGHT: 0px">g是c上面的2D图形对象（Graphics2D），通过g可以直接在c上绘制，不过我们推荐还是用自己创建的DisplayObject来绘制而不是直接用所有者组件的图形对象。</P> <P style="MARGIN-RIGHT: 0px">bounds是绘制的区域信息。你应该只在这个区域当中绘制。这个区域的大小不是固定的，在每次调用这个方法的时候，AsWing都会根据当前的显示布局重新调整bounds的值，所以你的程序应该根据bounds来设置大小</P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">getDisplay(c:Component) : DisplayObject </DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">获取DisplayObject类型的对象，如果你在绘制当中使用了自己新建的DisplayObject类型的对象，那么你应该在这里返回你的对象。</P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API：org.aswing.Container</FONT></P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">setBackgroundDecorator(bg:GroundDecorator):void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">设置背景装饰器，这个绘制发生在Container的背景层上。</P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API：org.aswing.graphics.Pen</FONT></P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">Pen(color:ASColor,thickness:uint)</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">构造一支画笔。画笔是绘制线条的工具。</P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API：org.aswing.ASColor</FONT></P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">ASColor (rgb:uint=0&#215;000000, alpha:Number=1)</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">构造一个颜色对象。参数rgb是一个6位的16进制数，分别代表了Red、Green、Blue三个通道的数值。参数alpha是一个0到1之间的小数，控制颜色的透明度。0为完全透明，1为完全不透明。</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">getASColor(r:uint, g:uint, b:uint, alpha:Number=1):ASColor</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">利用三个通道的数值和透明多获取一个颜色对象</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">getASColorWithHLS(h:Number, l:Number, s:Number, alpha:Number=1):ASColor</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px"> 通过HLS的三个通道和透明度获取一个颜色对象</P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" color=#000000 size=4>API：org.aswing.graphics.Graphics2D</FONT></P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">clear():void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">清除已经绘制成的图像内容。</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">drawCircle(pen:IPen, centerX:Number, centerY:Number, radius:Number):void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">绘制一个圆形。</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">drawEllipse(pen:IPen, x:Number, y:Number, width:Number, height:Number):void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">绘制一个椭圆，椭圆的外切矩形的左上角定点位于(x,y)，外切矩形的宽度为width，外切矩形的高度为height</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">drawLine(p:IPen, x1:Number, y1:Number, x2:Number, y2:Number):void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px"> 绘制一条直线段，(x1,y1)和(x2,y2)分别是线段的两个端点</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">drawPolygon(pen:Pen, points:Array):void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">绘制一个多边形。points为顶点数组，数组当中的对象为的顶点对象，结构为{x:x0, y:y0}。多边形是封口的，最后一个定点和第一个定点之间会自动绘制连线。</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">drawPolyline(p:IPen, points:Array):void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">绘制一个不封口的多边形边。points为顶点数组，和drawPolygon一样，区别只是DrawPolyline不会自动封口。</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">drawRectangle(pen:IPen, x:Number, y:Number, width:Number, height:Number):void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">绘制一个矩形。矩形的左上角定点位于(x,y)，宽度为width，高度为height</P> <P style="MARGIN-RIGHT: 0px"> </P> <UL dir=ltr> <LI> <DIV style="MARGIN-RIGHT: 0px">drawRoundRect(pen:IPen, x:Number, y:Number, width:Number, height:Number, radius:Number):void</DIV></LI></UL> <P style="MARGIN-RIGHT: 0px">绘制一个圆角矩形。矩形的外切矩形左上角顶点位于(x,y)，宽度为width，高度为height。矩形的圆角的曲率半径为radius。圆角和边相切。</P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P> <P dir=ltr style="MARGIN-RIGHT: 0px"> </P></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=21</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=20</link>
		<comments>http://thiswind.flashseer.org/?p=20#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:28:18 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=20</guid>
		<description><![CDATA[AsWing入门教程 1.4 在面板中显示信息     作者：胡矿    著作权所有，请勿转载    www.flashseer.org    Google doc http://docs.google.com/Doc?id=dnp8gdz_63n8m3f          本节，我们将讲述如何在框架中显示信息。我们不再像刚开始学习时那样用 trace 将字符串输出到输出面板当中，而是要把它显示在一个框架里面，如图1.4-1所示。    （图1.4-1）     把信息直接输出到框架上是可能的，但是这么做被认为是不好的编程行为。在AsWing当中，框架实际上被设计为组件的容器，可以容纳菜单或者其他的用户界面元素。通常情况下，你需要在另一个组件上绘制信息，然后把它添加到框架中，这个组件称为面板。        JFrame 的结构并不是很复杂，图1.4-2说明了一个JFrame的组成。    [...]]]></description>
			<content:encoded><![CDATA[<p><P><STRONG>AsWing入门教程 1.4 在面板中显示信息</STRONG> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>作者：胡矿</STRONG> </P> <P> </P> <P><STRONG>著作权所有，请勿转载</STRONG></P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG><A href="http://www.flashseer.org">www.flashseer.org</A></STRONG> </P> <P> </P> <P>Google doc</P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_63n8m3f" target=_blank>http://docs.google.com/Doc?id=dnp8gdz_63n8m3f</A></P> <P> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>   </STRONG> 本节，我们将讲述如何在框架中显示信息。我们不再像刚开始学习时那样用 trace 将字符串输出到输出面板当中，而是要把它显示在一个框架里面，如图1.4-1所示。</P> <P> </P> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_12g57bw7ft"></DIV> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left">（图1.4-1）</DIV> <P>    把信息直接输出到框架上是可能的，但是这么做被认为是不好的编程行为。在AsWing当中，框架实际上被设计为组件的容器，可以容纳菜单或者其他的用户界面元素。通常情况下，你需要在另一个组件上绘制信息，然后把它添加到框架中，这个组件称为面板。 </P> <P> </P> <P>    JFrame 的结构并不是很复杂，图1.4-2说明了一个JFrame的组成。 </P> <P> </P> <DIV style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 1em; PADDING-TOP: 1em; TEXT-ALIGN: left"><IMG src="http://docs.google.com/File?id=dnp8gdz_9f6dd5pf9"> </DIV> <P>（图1.4-2） </P> <P> </P> <P>    你可以看到，JFrame 当中安排了两个窗格，上面的是标题栏（Title Bar），下面的是内容窗格（Content Pane）。程序员最关心的是内容窗格，当设计一个框架时，组件会被添加到内容窗格中，使用的代码类似于： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><FONT color=#000000><STRONG>var</STRONG> </FONT>contentPane:Container = frame.getContentPane();<BR><FONT color=#000000><STRONG>var</STRONG> </FONT>c:Component = &#8230;&#8230;;<BR>contentPane.append(c); </P></BLOCKQUOTE> <P> </P> <P>    你不能直接往一个JFrame当中添加组件，而是应该向框架的内容窗格中放置。通过frame.getContentPane()可以得到框架的内容窗格。 </P> <P> </P> <HR>  <P> </P> <P><STRONG>  笔记：</STRONG> </P> <P> 框架的内容窗格（Content Pane）是一个容器组件（Container）。Container是各种AsWing组件的容器，任何组件要可以显示，都必须添加到一个可以显示容器当中。 关于Container组件在后面的章节还会做详细的介绍。</P> <P> </P> <HR>  <P> </P> <P> </P> <P>    在我们的例子中，我们想把一个面板添加到内容窗格中。面板是用JPanel类实现的。这种用户界面元素具有两个有用的特性： </P> <UL> <LI> <DIV>有一个能在上面进行绘制的表面 </DIV> <LI> <DIV>本身也是一个容器 </DIV></LI></UL> <P> </P> <P>    因此，它们还能容纳按钮、滑动条登其它界面元素。 </P> <P> </P> <P>    不过，把一个普通的JPanel添加到内容窗格当中是没有什么意义的——它什么都不能做。要使它变得有意义，必须使用继承来创建一个新类，然后通过覆盖或者添方法的手段来获得所需的额外功能。 </P> <P> </P> <P>    特别是，为了能在面板当中进行绘制，你需要： </P> <UL> <LI> <DIV>定义一个扩展（extends）JPanel的新类 </DIV> <LI> <DIV>覆盖paint方法 </DIV></LI></UL> <P> </P> <P>    paint方法实际上定义在Component中，这个类是所有AsWing组件的父类。该方法将在组件会绘制的时候被调用。你可以在该方法中调用JPanel的graphics对象进行绘制。graphics对象储存了一个用于绘制矢量图形的设置集合（比如颜色，填充方式，透明度）。paint方法的IntRectangle类型参数指出了绘制的范围，你应该确保绘制只发生在这个范围之内。在组件的大小发生变化的时候，这个范围会随着改变，在范围内绘制可以保证你的图形总是正确地显示。 </P> <P> </P> <P> </P> <HR>  <P> </P> <P><STRONG>笔记：</STRONG> </P> <P>JPanel当中的Graphics对象继承自ActionScript3.0核心类当中的Sprite类。所有的AsWing组件都是Sprite类的子类，但是你应该只是在特定的方法当中获取graphics对象进行绘制。 </P> <P> </P> <HR>  <P> </P> <P> </P> <P>    下面的代码演示了如何创建一个可以在上面进行绘制的面板： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><STRONG><FONT color=#000000>class</FONT></STRONG> HelloWorldPanel <STRONG><FONT color=#000000>extends</FONT></STRONG> JPanel { </P> <P>   <STRONG>public</STRONG> <STRONG>function</STRONG> HelloWorldPanel () { </P> <P>   }<BR>  </P> <P>   <STRONG>override</STRONG> <STRONG>protected</STRONG> <STRONG>function</STRONG> paint(b:IntRectangle) : <STRONG>void</STRONG>{ </P> <P>      <STRONG>super</STRONG>.paint(b);<BR>   </P> <P>      <STRONG>var</STRONG> g:Graphics = <STRONG>this</STRONG>.graphics; </P> <P>      &#8230;&#8230; // 在这里写绘制代码 </P> <P>   }<BR>} </P> <P> </P></BLOCKQUOTE> <P>     </P> <P>    只要窗口需要重绘，不管是因为什么原因，事件处理器都会通知组件。它会引起所有需要重绘的组件中的paint方法被执行。 </P> <P> </P> <P>    绝不要自己调用paint方法。只要你的应用程序需要重新绘制，该方法就会自动调用。你不应该干涉这个过程。 </P> <P> </P> <P>    哪些类型的动作触发了这个自动过程呢？例如，用户缩放窗口或者改变组件的显示相关的属性比如背景色、字体等会引起重绘。如果一个组件的外观和自己的状态相关，那么组件状态发生变化时，也会被要求重绘，比如按钮的由弹起状态变为按下状态。（当然，当窗口首次显示时，它也需要处理那些指定如何绘制、在哪里绘制初始元素的代码。如果你的图形尺寸是是依赖于paint方法的IntRectangle类型参数的，那么在窗口尺寸发生变化的时候，图形的尺寸也会随之调整以适应窗口。 </P> <P> </P> <P> </P> <HR>  <P> </P> <P><STRONG>笔记：</STRONG> </P> <P>如果需要强制性重绘屏幕，那么可以调用repaint方法。这个方法会让屏幕立即重新绘制以显示新的内容。 </P> <P> </P> <HR>  <P> </P> <P> </P> <P>    在上面的代码段中可以看到，paint方法只有一个IntRectangle类型的参数。这个参数是一个矩形区域，它给定了绘制的范围。虽然AsWing并没有强制你只能在这个区域内绘制，但是你应该保证自己绘制的图形不要超出这个区域的边界。Graphics对象对屏幕的度量单位是像素。坐标(0,0)代表了你正绘制其表面的组件的左上角。 </P> <P> </P> <P>    显示一个圆形是最基本的绘制之一。Graphics类有一个drawCircle方法，其语法如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">g.beginFill(color);   <P>g.drawCircle(x, y, radius); </P></BLOCKQUOTE> <P>    在绘制之前，需要先设置颜色，否则Graphics对象将会用最后一次设置的颜色来进行绘制。颜色值是一个6位的16进制整数，每两位分别代表一RGB的个颜色通道。比如，0xFF0000就代表红色（R=FF，G=00，B=00）。我们的例子当中显示的圆形是黑色的，它的颜色代码就是0&#215;000000。</P> <P> </P> <P>    在我们的例子中，我们需要在窗口的正中央绘制一个圆形。这个圆形正好内切框架的边缘。现在，我们的paint方法如下所示： </P> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><STRONG><FONT color=#000000>class</FONT></STRONG> HelloWorldPanel <STRONG><FONT color=#000000>extends</FONT></STRONG> JPanel { </P> <P>   <STRONG>public</STRONG> <STRONG>function</STRONG> HelloWorldPanel () { </P> <P>   }<BR>  </P> <P>   <STRONG>override</STRONG> <STRONG>protected</STRONG> <STRONG>function</STRONG> paint(bounds:IntRectangle) : <STRONG>void</STRONG>{ </P> <P>     <STRONG>super</STRONG>.paint(bounds);<BR>  <BR>     <STRONG>var</STRONG> x:uint = bounds.x + Math.round(bounds.width/2);<BR>     <STRONG>var</STRONG> y:uint = bounds.y + Math.round(bounds.height/2);<BR>     <STRONG>var</STRONG> radius:uint = Math.max(10, Math.round(Math.min(bounds.width, bounds.height)/2));<BR>  <BR>     <STRONG>var</STRONG> graphics:Graphics = <STRONG>this</STRONG>.graphics;<BR>     graphics.beginFill(0&#215;000000);<BR>     graphics.drawCircle(x, y, radius);</P> <P> </P> <P>   } </P> <P>} </P></BLOCKQUOTE> <P> </P> <P>    通过bounds可以获得绘制的区域大小，bounds会在框架大小发生变化的时候被重新设置以保证每次paint方法被调用的时候bounds都能正确反映绘制区域的大小，绘制的位置和尺寸应该根据bounds来计算。 </P> <P> </P> <HR>  <P><STRONG>注意！</STRONG> </P> <P>由于HelloWordPanel扩展了JPanel类，而JPanel类在paint方法中有自己的操作。为了确保超类完成自己的那份工作，我们必须在进行我们的绘制之前先调用<STRONG>super</STRONG>.paint方法。 </P> <P> </P> <HR>  <P> </P> <P> </P> <P> </P> <HR>  <P> </P> <P><STRONG>笔记：</STRONG> </P> <P>如果你的程序有大量的绘制工作，那么你应该用另外一种方式来进行绘制，我们将在后面介绍Graphics2D的时候介绍这种方式。 </P> <P> </P> <HR>  <P> </P> <P> </P> <P>    例1-3给出了完整的代码 </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"> <P><STRONG>例1-3 HelloWorld.as</STRONG> </P> <P><STRONG>package</STRONG><BR>{<BR>    <STRONG>import</STRONG> flash.display.Sprite;<BR> <BR>    <STRONG>import</STRONG> org.aswing.AsWingManager;<BR>    <STRONG>import</STRONG> org.aswing.Container;<BR>    <STRONG>import</STRONG> org.aswing.JFrame;</P> <P> </P> <P>    <STRONG>public</STRONG> <STRONG>class</STRONG> HelloWorld <STRONG>extends</STRONG> Sprite<BR>    {<BR>        <STRONG>public</STRONG> <STRONG>function</STRONG> HelloWorld()<BR>        {<BR>            AsWingManager.initAsStandard(<STRONG>this</STRONG>);<BR>   <BR>            <STRONG>var</STRONG> frame:JFrame = <STRONG>new</STRONG> JFrame();<BR>            frame.setTitle(&#8221;Hello World!&#8221;);<BR>            frame.setSizeWH(DEFAULT_WIDTH, DEFAULT_HEIGHT);<BR>   <BR>            <STRONG>var</STRONG> contentPane:Container = frame.getContentPane();<BR>   <BR>            <STRONG>var</STRONG> hello:HelloWorldPanel = new HelloWorldPanel();<BR>            contentPane.append(hello);<BR>   <BR>            frame.show();<BR>   <BR>        }<BR>  <BR>  <BR>        <STRONG>public</STRONG> <STRONG>static</STRONG> <STRONG>const</STRONG> DEFAULT_WIDTH:uint = 400;<BR>        <STRONG>public</STRONG> <STRONG>static</STRONG> <STRONG>const</STRONG> DEFAULT_HEIGHT:uint = 300;<BR>    }<BR>}</P> <P> </P> <P><STRONG>import</STRONG> flash.display.Graphics;</P> <P> </P> <P><STRONG>import</STRONG> org.aswing.JPanel;<BR><STRONG>import</STRONG> org.aswing.geom.IntRectangle; </P> <P> </P> <P><STRONG>class</STRONG> HelloWorldPanel <STRONG>extends</STRONG> JPanel {<BR>    <STRONG>override</STRONG> <STRONG>protected</STRONG> <STRONG>function</STRONG> paint(bounds:IntRectangle) :<STRONG>void</STRONG> {<BR>        <STRONG>super</STRONG>.paint(bounds);<BR>  <BR>        <STRONG>var</STRONG> x:uint = bounds.x + Math.round(bounds.width/2);<BR>        <STRONG>var</STRONG> y:uint = bounds.y + Math.round(bounds.height/2);<BR>        <STRONG>var</STRONG> radius:uint = Math.max(10, Math.round(Math.min(bounds.width, bounds.height)/2));<BR>  <BR>        <STRONG>var</STRONG> graphics:Graphics = <STRONG>this</STRONG>.graphics;<BR>        graphics.beginFill(0&#215;000000);<BR>        graphics.drawCircle(x, y, radius);<BR>    }<BR>}</P></BLOCKQUOTE> <P><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" color=#000000>API：<STRONG>org.aswing.JFrame</STRONG></FONT> </P> <UL> <LI><STRONG>getContentPane():Container</STRONG> </LI></UL> <P>          返回JFrame内容窗格对象，该对象是一个容器组件对象（Container） </P> <P> </P> <P><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" color=#000000>API：<STRONG>org.aswing.Component</STRONG></FONT> </P> <UL> <LI><STRONG>repaint():void</STRONG> </LI></UL> <P>          “尽可能快地” 重新绘制组件 </P> <UL> <LI><STRONG>paint(b:IntRectangle):void</STRONG> </LI></UL> <P>          需要覆盖该方法来说明你的组件应如何绘制。参数b给定了一个绘制的范围，你应该保证绘制在这个范围内进行。 </P></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=20</wfw:commentRss>
		</item>
		<item>
		<title></title>
		<link>http://thiswind.flashseer.org/?p=19</link>
		<comments>http://thiswind.flashseer.org/?p=19#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:27:19 +0000</pubDate>
		<dc:creator>thiswind</dc:creator>
		
		<category><![CDATA[AsWing 教程]]></category>

		<guid isPermaLink="false">http://thiswind.flashseer.org/?p=19</guid>
		<description><![CDATA[ AsWing 入门教程 1.3 给框架定位     作者：胡矿     著作权所有，请勿转载     www.flashseer.org    Google doc http://docs.google.com/Doc?id=dnp8gdz_3gdr96f         JFrame类本身只有几个用来改变框架外观的类。当然，通过继承，JFrame从不同的超类中（比如JWindow, Component）继承来很多用于处理框架的大小和位置的方法：    dispose方法——关闭窗口并且回收用语创建窗口的任何资源；   setIcon方法——用一个Icon对象设置窗口的图标，比如AttachIcon 和 LoadIcon；   setTitle——改变标题栏当中的文字；   setResizable——使用 Boolean 参数来决定框架大小是否能被用户改变。 setClosable——使用 Boolean [...]]]></description>
			<content:encoded><![CDATA[<p><P> <STRONG><SPAN class=smalltxt><SPAN class=bold>AsWing 入门教程 1.3 给框架定位</SPAN></SPAN></STRONG> </P> <P> </P> <P><STRONG><SPAN class=smalltxt><SPAN class=bold></SPAN></SPAN></STRONG></P> <P><STRONG>作者：胡矿</STRONG> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG>著作权所有，请勿转载</STRONG> </P> <P> </P> <P><STRONG></STRONG></P> <P><STRONG><A href="http://www.flashseer.org">www.flashseer.org</A></STRONG> </P> <P> </P> <P>Google doc</P> <P><A class=tabcontent id=publishedDocumentUrl href="http://docs.google.com/Doc?id=dnp8gdz_3gdr96f" target=_blank>http://docs.google.com/Doc?id=dnp8gdz_3gdr96f</A></P> <P> </P> <P> </P> <P><SPAN style="FONT-SIZE: 12px">    JFrame类本身只有几个用来改变框架外观的类。当然，通过继承，JFrame从不同的超类中（比如JWindow, Component）继承来很多用于处理框架的大小和位置的方法：</SPAN> </P><SPAN style="FONT-SIZE: 12px"> <UL> <LI> <DIV>dispose方法——关闭窗口并且回收用语创建窗口的任何资源； </DIV> <LI> <DIV>setIcon方法——用一个Icon对象设置窗口的图标，比如AttachIcon 和 LoadIcon； </DIV> <LI> <DIV>setTitle——改变标题栏当中的文字； </DIV> <LI> <DIV>setResizable——使用 Boolean 参数来决定框架大小是否能被用户改变。</DIV> <LI>setClosable<SPAN style="FONT-SIZE: 12px">——</SPAN><SPAN style="FONT-SIZE: 12px">使用 Boolean 参数来决定框架是否能被用户关闭。</SPAN></LI></UL> <P> </P> <P>    图1.3-1当中给出了JFrame类的继承链。</P> <P><IMG src="http://www.flashseer.org/bbs/attachments/zbwxLjI=_xmXv4Ht58qtg.jpg" border=0></P> <P>（图1.3-1）<BR><BR>    需要对 Component 类和 JWindow 类（它是JFrame类的父亲）进行仔细研究，在它们中能够找到缩放和改造框架的方法。例如，用于显示框架的show方法定义在JPopup类中。再例如，Component类中的setLocationXY方法是重定位一个组件的几种方法之一。如果你调用： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">setLocationXY(x, y);</BLOCKQUOTE> <P> </P> <P>    那么左上角被放到水平x像素、垂直y像素处（(0,0)是屏幕左上角）。与此类似，Component中的setComBoundsXYWH方法允许你在一次操作中设定一个组件（特别是JFrame）的大小和位置，其形式为：</P> <P> </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">setComBoundsXYWH(x, y, width, height) ;</BLOCKQUOTE><SPAN style="FONT-SIZE: 12px"> <P><BR></P> <P> </P> <HR>  <P> </P> <P><STRONG>笔记：</STRONG><BR>对于框架，setLocationXY 和 setBoundsXYWH 中的坐标均是相对于整个屏幕的。在后面的章节还会看到，对于容器内的组件，其坐标是相对与容器的。 </P> <HR>  <P> </P> <P> </P> <P>    要记住：如果不明确定义框架的大小，那么其默认大小是0乘0像素。为了是例子程序尽可能简单，我们把框架设成能为大多数显示设置所能够接受的程度。 </P> <P> </P> <HR> <STRONG>提醒：<BR></STRONG>本节所介绍的API是使框架具有正确观感的重要方法。这些方法的一部分定义在JFrame类当中；另一部分则定义在JFrame的各个父类当中。所以，你需要通过API文档来查找是否有完成特定目的的方法。遗憾的是，研究SDK文档相当费力。对于子类来说，API文档只是说明其覆盖的方法。例如，show对于JFrame类型的对象来说是可用的，但是由于它是从JPopup类继承而来，所以JFrame文档没有说明它。如果你认为应该有一个方法能够达到目的，但你使用的这个类的文档中却没有说明它，那么你应该在该类的超类API文档中查找这个方法。每个API页的顶部都有超链接到它的超类API页，并且每页在新方法和覆盖方法的总结下面都有一个继承的方法列表。<BR> <HR>  <P> </P> <P> </P> <P>    在本节末尾，我们给出一个简单的例子，以说明窗口操作。在例子中，那个可以关闭的框架具有如下特点：<BR>      1.其面积是整个舞台得四分之一<BR>      2.显示在舞台的中央 </P> <P><BR>    比如，舞台的大小是800×600像素，那么框架的大小就是400×300，它的左上角定位在(200, 150)。使用下面的步骤，可以得到舞台的大小。代码如下： </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>var</STRONG> bounds:IntRectangle = AsWingUtils.getVisibleMaximizedBounds();<BR><STRONG>var</STRONG> stageWidth:uint = bounds.width;<BR><STRONG>var</STRONG> stageHeight:uint = bounds.height;</BLOCKQUOTE> <P>   完整的代码见例1.2。 </P> <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><STRONG>例1.2</STRONG>  <P> </P> <P><STRONG>package</STRONG><BR>{<BR>        <STRONG>import</STRONG> flash.display.Sprite;<BR>        <STRONG>import</STRONG> org.aswing.JFrame;<BR>        <STRONG>import</STRONG> org.aswing.AsWingManager;<BR><BR>        <STRONG>public</STRONG> <STRONG>class</STRONG> CenteredFrameTest <STRONG>extends</STRONG> Sprite<BR>        {<BR>                <STRONG>public</STRONG> <STRONG>function</STRONG> CenteredFrameTest()<BR>                {<BR>                        AsWingManager.initAsStandard(this);<BR>                       <BR>                        <STRONG>var</STRONG> frame:MyCenteredFrame = <STRONG>new</STRONG> MyCenteredFrame();<BR>                        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);<BR>                        frame.show();<BR>                }<BR>               <BR>        }<BR>}<BR>        <STRONG>import</STRONG> org.aswing.JFrame;<BR>        <STRONG>import</STRONG> org.aswing.geom.IntRectangle;<BR>        <STRONG>import</STRONG> org.aswing.AsWingUtils;<BR>        <STRONG>import</STRONG> org.aswing.LoadIcon;<BR>       <BR><STRONG>class</STRONG> MyCenteredFrame <STRONG>extends</STRONG> JFrame<BR>{<BR>        <STRONG>public</STRONG> <STRONG>function</STRONG> MyCenteredFrame()<BR>        {<BR>                //获取舞台可视区域的大小<BR>                <STRONG>var</STRONG> bounds:IntRectangle = AsWingUtils.getVisibleMaximizedBounds();<BR>                <STRONG>var</STRONG> stageWidth:uint = bounds.width;<BR>                <STRONG>var</STRONG> stageHeight:uint = bounds.height;<BR>               <BR>                //把框架放在中心<BR>                <STRONG>this</STRONG>.setSizeWH(stageWidth/2, stageHeight/2);<BR>                <STRONG>this</STRONG>.setLocationXY(stageWidth/4, stageHeight/4);<BR>               <BR>                //设置标题和图标<BR>                <STRONG>var</STRONG> icon:LoadIcon = <STRONG>new</STRONG> LoadIcon(&#8221;icon.png&#8221;);<BR>                <STRONG>this</STRONG>.setIcon(icon);<BR>                <STRONG>this</STRONG>.setTitle(&#8221;置于正中的框架&#8221;);<BR>        }<BR>} </P></BLOCKQUOTE> <P><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API:org.aswing.Component</FONT></SPAN></SPAN></P><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">isVisible() : Boolean<BR>检查组件是否可见。组件初始化为可见的，但顶层组件，如JFrame，除外。</FONT></LI></UL> <P> </P> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">setVisible(b:Boolean) : <STRONG>void</STRONG><BR>根据b的取值来决定是显示组件还是隐藏组件。</FONT></LI></UL> <P> </P> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">isShowing() : Boolean<BR>检查组件是否正显示在舞台上。若是，它必须是可见的并且它所有的父元件也是可见的。</FONT></LI></UL> <P> </P> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">isEnabled() : Boolean<BR>检查组件是否被激活。被激活的组件能够接受键盘和鼠标的输入。组件初始为被激活。</FONT></LI></UL> <P> </P> <UL> <LI>setEnabled(Boolean b) : <STRONG>void</STRONG><BR>激活或者禁用一个组件。</LI></UL> <P> </P> <UL> <LI>getLocation() : IntPoint<BR>返回该组件左上角坐标值，该值是相对于包围容器的左上角而言。（一个 IntPoint 对象 p 封装了一个x坐标和一个y坐标，访问它们使用p.x和p.y）</LI></UL> <P> </P> <UL> <LI>getGlobalLocation() : IntPoint<BR>返回该组件的左上角坐标值，该值使用的是舞台坐标。</LI></UL> <P> </P> <UL> <LI>setComBounds(b:IntRectangle):<STRONG>void</STRONG><BR>setComBoundsXYWH(<SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px">x:int, y:int, w:int, h:int</SPAN></SPAN></SPAN></SPAN>):void</LI></UL></SPAN></SPAN></SPAN></SPAN> <DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px">移动组件并且设置组件的大小。（一个IntRectangle 对象 rect 封装了x坐标、y坐标以及宽度width和高度height，访问它们使用 rect.x 、rect.y、rect.width、rect.height）</SPAN></SPAN></SPAN></SPAN></DIV><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"> <P> </P> <UL> <LI>setLocationXY(x:int, y:int):<STRONG>void</STRONG><BR>setLocation(newPos:IntPoint):<STRONG>void</STRONG><BR>把组件移动到新位置。如果该组件不是顶层组件，那么x和y坐标（或者p.x和p.y）使用该容器的坐标；如果该组件是顶层组件（例如JFrame），那么坐标使用舞台坐标。</LI></UL> <P> </P> <UL> <LI>getSize(rv:IntDimension=null):IntDimension<BR>得到组件的当前大小。</LI></UL> <P> </P> <UL> <LI>setSize(newSize:IntDimension):<STRONG>void</STRONG><BR>setSizeWH(w:int, h:int):<STRONG>void</STRONG><BR>重设组件大小为指定的高度和宽度。</LI></UL> <P><BR><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API:org.aswing.JWindow</FONT></P> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">toFront():<STRONG>void</STRONG><BR>把该窗口显示到任何其它窗口的前面</FONT></LI></UL> <P> </P> <UL> <LI>toBack():<STRONG>void</STRONG><BR>把该窗口移动到窗口栈的最后面，并且相应地重新排列所有可见窗口。<BR></LI></UL> <P><BR><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API:org.aswing.JFrame</FONT></P> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">setResizable(b:Boolean):<STRONG>void</STRONG><BR>决定用户是否可以缩放框架</FONT></LI></UL><FONT style="BACKGROUND-COLOR: rgb(255,255,255)"><BR></FONT> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">setClosable(b:Boolean):<SPAN style="FONT-WEIGHT: bold">void</SPAN></FONT></LI></UL></SPAN></SPAN></SPAN></SPAN> <DIV style="MARGIN-LEFT: 40px">决定用户是否可以关闭框架<BR></DIV><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"> <P><FONT style="BACKGROUND-COLOR: rgb(255,255,255)"></FONT> </P> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">setTitle(t:String):void<BR>把框架的标题栏中的文字设为字符串t。</FONT></LI></UL> <P><FONT style="BACKGROUND-COLOR: rgb(255,255,255)"></FONT> </P> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">setIcon(ico:Icon):<STRONG>void</STRONG><BR>参数 ico，需要用作框架图标的图像，比如 AttachIcon 或者 LoadIcon</FONT></LI></UL> <P><FONT style="BACKGROUND-COLOR: rgb(255,255,255)"></FONT> </P> <UL> <LI><FONT style="BACKGROUND-COLOR: rgb(255,255,255)">getState():int<BR>setState(s:int, programmatic:Boolean=true):<STRONG>void</STRONG><BR>获取或者设置框架的状态。状态是如下值之一：</FONT></LI></UL> <P>               JFrame.NORMAL<BR>               JFrame.ICONIFIED;<BR>               JFrame.MAXIMIZED<BR>               JFrame.MAXIMIZED_HORIZ<BR>               JFrame.MAXIMIZED_VERT</P> <P><BR><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API:org.aswing.AsWingManager</FONT></P> <UL> <LI>initAsStandard(root:DisplayObjectContainer):<STRONG>void</STRONG><BR>用一个可显示容器来对AsWing做全局初始化，这个方法必须在任何别的AsWing方法之前被调用，并且root的stage属性不为<SPAN style="FONT-WEIGHT: bold">null</SPAN>。</LI></UL> <P><BR><BR><BR><BR><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API:org.aswing.AsWingUtils</FONT></P> <UL> <LI>getVisibleMaximizedBounds(dis:DisplayObject=null):IntRectangle<BR>获取当前舞台的可显示区域大小</LI></UL> <P><BR><BR><FONT style="BACKGROUND-COLOR: rgb(255,102,0)" size=4>API:org.aswing.LoadIcon</FONT></P><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"> <UL><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-SIZE: 12px"> <LI>构造函数<BR>LoadIcon(url:*, width:Number=-1, height:Number=-1, scale:Boolean=<STRONG>false</STRONG>, context:LoaderContext=null)<BR>参数：<BR>url ：要加载的图表图片的 url， 图标图片加载之后就不能再更改<BR>height：图标的高度<BR>width：图标的宽度<BR>scale：决定是否根据图标的高度和宽度来缩放图片，默认为不缩放，不过建议设为缩放</LI></SPAN> <LI>  <LI> <LI> <LI><BR><BR>  </LI></SPAN> <LI> <LI> <LI> <LI></LI></SPAN> <LI> <LI> <LI> <LI></LI></UL></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN></p>
]]></content:encoded>
			<wfw:commentRss>http://thiswind.flashseer.org/?feed=rss2&amp;p=19</wfw:commentRss>
		</item>
	</channel>
</rss>
