<?xml version="1.0" encoding="GBK" ?>
<rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dcterms="http://purl.org/dc/terms/">
 <channel>
  	  <title><![CDATA[成都游戏学院手游老师]]></title>
	  <link>http://luotingzhang.blog.163.com</link>
	  <description><![CDATA[ ]]></description>
	  <language>zh-CN</language>
	  <pubDate>Fri, 4 Jul 2008 08:16:35 +0800</pubDate>
	  <lastBuildDate>Fri, 4 Jul 2008 08:16:35 +0800</lastBuildDate>
	  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
	  <generator><![CDATA[NetEase Space]]></generator>
	  <managingEditor><![CDATA[luotingzhang]]></managingEditor>
	  <webMaster><![CDATA[luotingzhang]]></webMaster>
		  <ttl>120</ttl>
	  <image>
	  	<title><![CDATA[成都游戏学院手游老师]]></title>
	  	<url>http://luotingzhang.blog.163.com/style/common/user_default.gif</url>
	  	<link>http://luotingzhang.blog.163.com</link>
	  </image>
  <item>
  	<title><![CDATA[Symbian领跑四大手机操作系统]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/7047054320086383226322</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT><BR></P>
<P>Symbian OS <BR>&nbsp; <BR>Symbian一个手机巨头联盟的代名词，该公司成立于1987年，是摩托罗拉、西门子、诺基亚等几家大型移动通讯设备商共同出资组建的一个合资公司，专门研发手机操作系统。而Symbian操作系统的前身是EPOC，而EPOC是 Electronic Piece of Cheese取第一个字母而来的，其原意为"使用电子产品时可以像吃乳酪一样简单"，这就是它在设计时所坚持的理念。 </P>
<P>Symbian操作系统在智能移动终端上拥有强大的应用程序以及通信能力，这都要归功于它有一个非常健全的核心--强大的对象导向系统、企业用标准通信传输协议以及完美的sun java语言。Symbian认为无线通讯装置除了要提供声音沟通的功能外，同时也应具有其它种沟通方式，如触笔、键盘等。在硬件设计上，它可以提供许多不同风格的外型，像使用真实或虚拟的键盘，在软件功能上可以容纳许多功能，包括和他人互相分享信息、浏览网页、传输、接收电子信件、传真以及个人生活行程管理等。此外，Symbian操作系统在扩展性方面为制造商预留了多种接口，而且EPOC 操作系统还可以细分成三种类型：Pearl／Quartz／Crystal，分别对应普通手机、智能手机、Hand Held PC场合的应用。 </P>
<P>Symbian分两个主要的智能平台，一个是适于单手操作的S60，代表产品是诺基亚7650、3650；另一个是双手操作的S80，代表产品是诺基亚的9210，主要针对商务用户。此外Symbian上还有另一个平台UIQ，以笔操作为主，代表产品是索尼爱立信的P802。 </P>
<P>到2004年6 月30日，10家被授权公司开发的基于Symbian 操作系统的手机和相关产品有34款准备进入市场。（2003年第二季度： 26 款手机、9 家被授权公司）。 </P>
<P>7月初，Symbian 新的股权变化,结构如下： </P>
<P>爱立信所持股份由17.5％变为15.6％；索尼爱立信由1.5 ％变为13.1％；诺基亚由32.2％变为47.9％；松下由7.9 变为10.5％；Psion 由31.1％变为0.0 ％；三星由5.0 ％变为4.5 ％，西门子由4.8 ％变为8.4 ％。摩托罗拉年初卖掉了所持Symbian股份。 <BR>*****************************************************************************************************************************</P>
<P>Windows Mobile </P>
<P>Windows Mobile包括Pocket PC、SmartPhone以及Pocket PC Phone三大平台体系，Windows Mobile具有录音、音视频播放等多媒体功能。由于大多数机型支持彩色显示，因此耗电量极大，并且由于Windows CE本身对于资源的巨大吞噬作用，所以在能耗方面Windows CE系统要明显逊于Palm操作系统。在功能扩展方面微软比较倾向于集成使用，大多具有标准的CF卡插槽，用户可以根据自己的需要增加内存、加装摄像头、调制解调器等设备。 </P>
<P>针对中国用户而言，Windows Moblie的中文操作系统已经比较成熟。目前，摩托罗拉、ARM、ST、NeoMagic、德州仪器、Intel、三星、现代都已经生产了针对该系统的处理器；有数十家制造商生产基于Smartphone和Pocket PC技术的移动设备，有35家运营商分销这些产品。欧洲、东亚和加拿大已经销售Smartphone智能手机。 <BR>*****************************************************************************************************************************</P>
<P>Palm OS </P>
<P>Palm OS是Palm公司的是一种32位的嵌入式操作系统，它的操作界面采用触控式，差不多所有的控制选项都排列在屏幕上，使用触控笔便可进行所有操作。作为一套极具开放性的系统，开发商向用户免费提供Palm操作系统的开发工具，允许用户利用该工具在Palm操作系统的基础上编写、修改相关软件，使支持Palm的应用程序丰富多彩、应有尽有。 </P>
<P>Palm操作系统最明显的优势还在于其本身是一套专门为掌上电脑编写的操作系统，在编写时充分考虑到了掌上电脑内存相对较小的情况，所以Palm操作系统本身所占的内存极小，基于Palm操作系统编写的应用程序所占的空间也很小，通常只有几十KB，所以基于Palm操作系统的掌上电脑虽然只有几兆内存却可以运行众多的应用程序。Palm在其它方面还存在一些不足，Palm操作系统本身不具有录音、MP3播放功能等，如果你需要使用这些功能，就需要另外加入第三方软件或硬件设备方可实现。对于中国用户而言，另一个不足之处在于Palm操作系统起初在中国销售的产品仍然要使用中文外挂平台，有相当部分依然是以英文界面为主，在一定程度上影响了基于Palm操作系统的产品在中国市场的大面积进入。其代表性的产品有Palm m505、Palm m500、Palm III等。 <BR>*****************************************************************************************************************************</P>
<P>Linux OS </P>
<P>Linux凭借其自由、免费、开放源代码的优势，经过来自互联网、遍布全球的程序员的努力，再加上IBM、Sun等计算机巨头的支持，Linux在手机操作系统市场中异军突起，尤其是在众多知名厂商宣布支持Linux手机操作系统之后，Linux的发展将不容忽视。 </P>
<P>由于Linux具有源代码开放、软件授权费用低、应用开发人才资源丰富等优点，便于开发个人和行业应用。这一特点非常重要，因为丰富的应用是智能手机的优越性体现和关键卖点所在。从应用开发的角度看，由于Linux的源代码是开放的，有利于独立软件开发商（ISV）开发出硬件利用效率高、功能更强大的应用软件，也方便行业用户开发自己的安全、可控认证系统。特别是当智能手机大量用作行业应用的移动终端时，使用Linux便于实施系统一体化的安全策略。 </P>
<P>嵌入式Linux系统的典型代表是摩托罗拉在智能手机A 760。 <BR>***************************************************************************************************************************** <BR></P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/7047054320086383226322</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/7047054320086383226322</guid>
    <pubDate>Thu, 3 Jul 2008 08:32:26 +0800</pubDate>
    <dcterms:modified>2008-07-03T08:33:49+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[手机游戏开发的特点]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/7047054320086383145692</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P></P><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic"></SPAN></PRE>一 移动游戏开发与传统游戏开发的区别 
<P>　　移动游戏开发与传统游戏开发区别在许多方面： </P>
<P>　　1、开发团队的大小 </P>
<P>　　传统的PC和控制台游戏一般需要12到30人的开发团队。因为大部分移动游戏规模比控制台游戏小，所以一般情况下只需要3到5人的团队开发，有的时候甚至设计者和编程者是同一个人。 </P>
<P>　　2、预算 </P>
<P>　　传统游戏的预算在一百万美元到五百万美元之间。大部分移动游戏的预算则通常少于一百万美元。实际上，移动电话有限的显示能力和对应用程序大小的限制使得不可能象传统游戏那样投入大量的财力物力。从某种意义上来说，这也算是一个优点。 </P>
<P>　　3、开发周期 </P>
<P>　　传统的游戏一般要开发两到三年。而大部分移动游戏几月之内就能开发完毕。换句话说，只要有一个小型开发团队和一个小的预算，你就可以开发并推广一个专业品质的移动游戏。因此，对于许多在传统游戏领域遇到挫折的开发者来说，移动游戏开发有很强的吸引力。 </P>
<P>　　4、网络设备 </P>
<P>　　移动游戏可能不同于我们之前看到的任何游戏：它受载体因素的限制，但是支持网络并且可多人游戏。用于PC的调制解调器也只是8年前才大范围应用；控制台游戏只不过现在才能上网。移动电话的特性决定它是一种网络设备。即使它们的处理能力使人想起以前的老式计算机技术，但是它们的网络性能却更加出众。 </P>
<P>　　5、开放标准 </P>
<P>　　控制台游戏开发需要从控制台游戏厂商取得授权和支持，需要支付给他们"平台使用费"。在无线应用程序世界（如同在PC游戏开发中一样），你可以免费的开发任何款式的游戏，而不要支付Nokia、Sun或其他平台提供商一分钱。此外，这些移动游戏开发平台标准可以向开发者发布、开放并可免费取得。 </P>
<P>　　6、部署 </P>
<P>　　传统的游戏主要是在软件市场上购买。而移动游戏主要是由用户从移动门户网站下载并安装。在有些情况下，它们是通过无线网络下载的。有些手机允许你下载一个应用程序到计算机中，然后通过数据线传送到手机中。 </P>
<P>　　因此，移动游戏的销售渠道是非常不同的。用户一般通过移动运营商的游戏菜单、手机厂商预装在手机中的游戏菜单或者无线应用程序门户网站上找到移动游戏。 <BR>*****************************************************************************************************************************</P>
<P>二 载体的优点 </P>
<P>　　1、庞大的潜在用户群 </P>
<P>　　现在全球超过十亿部移动电话正在被使用，并且这个数目正在逐渐增加。在除美国之外的每个发达国家，拥有手机的人数比拥有计算机的人数更多。虽然那些手机只有一小部份是支持Java的手机，但是这个数目正在快速地提高并且在几年内Java手机将要成为行业标准。移动游戏潜在的市场比其它任何平台，比如Playstation和GameBoy都要大。 </P>
<P>　　2、便携性 </P>
<P>　　GameBoy比任何其他控制台游戏卖出的嗟囊桓鲈蚓褪牵罕阈浴Ｈ嗣强梢运媸彼娴赝嫠茄≡竦挠蜗贰Ｓ胂衷诘挠蜗房刂铺ɑ蛘吒鋈说缒韵啾龋只赡懿皇且桓龊玫挠蜗飞璞福侨嗣腔旧鲜撬媸彼婵潭及阉谴谏肀摺Ｔ谒抢肟业氖焙蚧蛘呦胪娴氖焙颍⒄哂Ω梦翘峁┖猛娴挠蜗贰?</P>
<P>　　3、支持网络 </P>
<P>　　因为移动电话是网络设备，所以可以实现多人游戏，虽然有某些限制因素。 <BR>*****************************************************************************************************************************</P>
<P>三 载体的缺点 </P>
<P>　　1、屏幕小 </P>
<P>　　你面对的是小型的屏幕。虽然屏幕分辩率持续提高，并且彩屏即将成为标准，但是屏幕尺寸还是一直很小，因为没有人乐意拿着砖块一样大的手机。 </P>
<P>　　还有一个相关的问题：不同的手机的屏幕大小是不同的。比如说Nokia Series 60平台设备就提供了和Nokia 5100这样的Series 40设备不同的屏幕尺寸。虽然各个厂商已经标准化它们产品的屏幕尺寸以避免分割市场，但是开发者仍然需要为不同的电话优化他们的游戏--你肯定想使用特定的手机上所有可用的屏幕空间。 </P>
<P>　　2、有限的颜色和声音支持 </P>
<P>　　大部分使用者手中的手机仍然是黑白的，虽然现在出售的支持Java的手机大部分都是彩屏手机。在这些手机中12bit彩色非常流行。 </P>
<P>　　即使手机本来就有声音设备，但是应用程序播放声音的能力却非常有限。J2ME规范根本不需要硬件厂商支持声音，虽然基本的Java手机允许使用一些声音并且MIDI支持正在成为标准。通常，手机中只有一个语音或者一个声道可用。 </P>
<P>　　3、应用程序大小限制 </P>
<P>　　大部分的Java手机只有很少的内存空间用于运行MIDlet。此外，对MIDlet的大小始终有一个限制。实际的限制取决于手机设备和移动运营商的规定。 </P>
<P>　　在这样的限制条件下设计开发移动游戏固然是非常困难的，但是我们要知道，第一台家用电脑只有64 KB内存，但是仍然有人热衷于在其上开发游戏软件。在一些智能手机上内存的限制就少一些，比如Nokia 3650甚至可以运行几兆字节的应用程序。 </P>
<P>　　4、高等待时间 </P>
<P>　　等待时间----机器发出请求和接到响应之间所花费的时间----在计算机上是以微秒计算；在有线因特网上是以毫秒计算；而在无线网络则要以秒计算。 </P>
<P>　　等待时间是网络游戏中一直存在的一个问题，开发者们总是在努力消除它带来的问题。无线网络等待时间非常长，这就不可能有效的开发多人快速动作移动游戏。然而基于回合制的多人游戏是相当可行的，我们在后面的文章中将讨论如何使用各种方法来处理这个问题。 </P>
<P>　　虽然移动运营商总是在努力增加移动电话可用的带宽，但是他们却没有把降低等待时间当成首要解决的问题，因为它对于别的应用程序并不重要。 </P>
<P>　　还有一种特殊情况：使用蓝牙技术或其他无线局域网技术的手机可以和附近的蓝牙设备使用因特网等待时间（一般200-400毫秒）通讯。这样，使用像Nokia 3650这样的智能手机，你就可以和附近的移动用户一起玩多人快速动作游戏了。 </P>
<P>　　5、可中断性是关键 </P>
<P>　　当用户接听电话的时候，手机都会中断进行中的游戏。游戏程序必须能够暂停并且继续，而且不会造成游戏问题（例如，游戏者在打电话的时候老怪仍然在移动，打死玩家扮演的角色，导致输掉游戏）并且不会造成内存溢出。这需要在编程的时候多注意，Nokia提供了技术文档帮助J2ME和Symbian C++开发者了解并解决这个问题。<BR></P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/7047054320086383145692</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/7047054320086383145692</guid>
    <pubDate>Thu, 3 Jul 2008 08:31:45 +0800</pubDate>
    <dcterms:modified>2008-07-03T08:33:13+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[用Canvas创建高级菜单]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/704705432008524113216244</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P>这个案例描述了如何使用低级UI API，也就是Canvas来创建高级菜单，界面如下图所示：</P>
<P><A title=Image:Inactive_menu.png href="http://www.j2medev.com/Article/UploadFiles/200803/20080306085211603.png"><IMG height=342 alt=Image:Inactive_menu.png src="http://www.j2medev.com/Article/UploadFiles/200803/20080306085213184.png" width=258 border=0></A> <A title=Image:Active_menu.png href="http://www.j2medev.com/Article/UploadFiles/200803/20080306085215857.png"><IMG height=339 alt=Image:Active_menu.png src="http://www.j2medev.com/Article/UploadFiles/200803/20080306085218419.png" width=258 border=0></A> </P>
<P>本案例共包括三个类，MIDlet,GUI和Menu。其中，当MIDlet启动后，Options和exit会绘制在屏幕</P>
<P>的底部，当用户按左或者右软键的时候，程序会根据键值响应不同的动作。例如选择options后，</P>
<P>会弹出menu供用户选择。</P>
<P>GUI类持有一个Menu对象，并且根据用户的输入来改变Menu的状态，然后重新绘制屏幕。事实上menu只有两种状态，一种是激活的，一种是非激活的。只有激活状态下，菜单才会显示出来，并且可以接受用户的上下左右选择，当用户选择了Menu里面的选项之后，GUI会把用户的选择显示在屏幕上。代码如下所示：</P>
<H3><SPAN>Midlet.java</SPAN></H3><PRE><SPAN style="COLOR: #a1a100">import javax.microedition.midlet.*;</SPAN>
<SPAN style="COLOR: #a1a100">import javax.microedition.lcdui.*;</SPAN>
&nbsp;
&nbsp;
&nbsp;
<SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">class</SPAN> Midlet <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">extends</SPAN> MIDlet <SPAN style="COLOR: #66cc66">{</SPAN>
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> Display display;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> GUI gui;
    
    
    
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="COLOR: #993333">void</SPAN> startApp<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
&nbsp;
        display = Display.<SPAN style="COLOR: #006600">getDisplay</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        
        <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>display == <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">null</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
            
            destroyApp<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">false</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
            
        <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if display is not allocated</SPAN>
        
&nbsp;
        gui = <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">new</SPAN> GUI<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        display.<SPAN style="COLOR: #006600">setCurrent</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>gui<SPAN style="COLOR: #66cc66">)</SPAN>;
        
        gui.<SPAN style="COLOR: #006600">start</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end startApp</SPAN>
    
    
    
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="COLOR: #993333">void</SPAN> pauseApp<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end pauseApp</SPAN>
    
    
    
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="COLOR: #993333">void</SPAN> destroyApp<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #993333">boolean</SPAN> unconditional<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        display.<SPAN style="COLOR: #006600">setCurrent</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">null</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        notifyDestroyed<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end destroyApp</SPAN>
    
<SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end Midlet.java</SPAN></PRE>
<P><BR></P><A></A>
<H3><SPAN>GUI.java</SPAN></H3>
<P><BR></P><PRE><SPAN style="COLOR: #a1a100">import java.util.*;</SPAN>
&nbsp;
<SPAN style="COLOR: #a1a100">import javax.microedition.lcdui.*;</SPAN>
<SPAN style="COLOR: #a1a100">import javax.microedition.lcdui.game.*;</SPAN>
&nbsp;
<SPAN style="COLOR: #a1a100">import javax.microedition.media.*;</SPAN>
<SPAN style="COLOR: #a1a100">import javax.microedition.media.control.*;</SPAN>
&nbsp;
<SPAN style="COLOR: #a1a100">import java.io.*;</SPAN>
<SPAN style="COLOR: #a1a100">import javax.microedition.io.*;</SPAN>
<SPAN style="COLOR: #a1a100">import javax.microedition.io.file.*;</SPAN>
&nbsp;
&nbsp;
&nbsp;
<SPAN style="FONT-WEIGHT: bold; COLOR: #000000">class</SPAN> GUI <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">extends</SPAN> GameCanvas <SPAN style="COLOR: #66cc66">{</SPAN>
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> Midlet midlet;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> Display display;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AGraphics+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Graphics</U></SPAN></A> g;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A> font;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <SPAN style="COLOR: #993333">int</SPAN> width = <SPAN style="COLOR: #cc66cc">0</SPAN>;
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <SPAN style="COLOR: #993333">int</SPAN> height = <SPAN style="COLOR: #cc66cc">0</SPAN>;
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMenu+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Menu</U></SPAN></A> menu;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A> leftOption;
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A> rightOption;
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A><SPAN style="COLOR: #66cc66">[</SPAN><SPAN style="COLOR: #66cc66">]</SPAN> menuOptions;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <SPAN style="COLOR: #993333">int</SPAN> currentlySelectedIndex = <SPAN style="COLOR: #cc66cc">0</SPAN>;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <SPAN style="COLOR: #993333">boolean</SPAN> menuIsActive = <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">false</SPAN>;
    
    
    
    
    <SPAN style="COLOR: #808080; FONT-STYLE: italic">/**
     * Creates a new instance of GUI.
     */</SPAN>
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> GUI<SPAN style="COLOR: #66cc66">(</SPAN>Midlet midlet<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">super</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">false</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>.<SPAN style="COLOR: #006600">midlet</SPAN> = midlet;
        
        font = <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">getFont</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">FACE_SYSTEM</SPAN>, </PRE><PRE><A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">STYLE_PLAIN</SPAN>, <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">SIZE_SMALL</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        setFullScreenMode<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">true</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        width = getWidth<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        height = getHeight<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        
        g = getGraphics<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        
        leftOption = <SPAN style="COLOR: #ff0000">"Options"</SPAN>;</PRE><PRE> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// will be displayed only when menu is not active</SPAN>
        rightOption = <SPAN style="COLOR: #ff0000">"Exit"</SPAN>; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// will be displayed only when menu is not active</SPAN>
        menuOptions = <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">new</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A><SPAN style="COLOR: #66cc66">[</SPAN><SPAN style="COLOR: #66cc66">]</SPAN> </PRE><PRE><SPAN style="COLOR: #66cc66">{</SPAN><SPAN style="COLOR: #ff0000">"Option #1"</SPAN>, <SPAN style="COLOR: #ff0000">"Option #2"</SPAN>, <SPAN style="COLOR: #ff0000">"Option #3"</SPAN>, <SPAN style="COLOR: #ff0000">"Option #4"</SPAN><SPAN style="COLOR: #66cc66">}</SPAN>;
        
        menu = <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">new</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMenu+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Menu</U></SPAN></A><SPAN style="COLOR: #66cc66">(</SPAN>leftOption, rightOption, menuOptions<SPAN style="COLOR: #66cc66">)</SPAN>;
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end constructor</SPAN>
    
    
    
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="COLOR: #993333">void</SPAN> start<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        clearScreen<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        menu.<SPAN style="COLOR: #006600">drawInactiveMenu</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>, g<SPAN style="COLOR: #66cc66">)</SPAN>;
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end start</SPAN>
    
    
    
    
    <SPAN style="COLOR: #808080; FONT-STYLE: italic">// softkey codes may vary from phone to phone</SPAN>
    <SPAN style="COLOR: #808080; FONT-STYLE: italic">// -6 and -7 values are OK on Nokia phones</SPAN>
&nbsp;
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <SPAN style="COLOR: #993333">int</SPAN> LEFT_SOFTKEY_CODE = <SPAN style="COLOR: #cc66cc">-6</SPAN>; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// </SPAN><SPAN style="COLOR: #808080; FONT-STYLE: italic">check it for your phone model</SPAN>
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <SPAN style="COLOR: #993333">int</SPAN> RIGHT_SOFTKEY_CODE = <SPAN style="COLOR: #cc66cc">-7</SPAN>; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// check it for your phone model</SPAN>
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">protected</SPAN> <SPAN style="COLOR: #993333">void</SPAN> keyPressed<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #993333">int</SPAN> keyCode<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// work with menu according to its current state</SPAN>
        
        <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>menuIsActive<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw active menu</SPAN>
            
            
            <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>keyCode == RIGHT_SOFTKEY_CODE<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
                
                <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw inactive menu again</SPAN>
                
                clearScreen<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
                menu.<SPAN style="COLOR: #006600">drawInactiveMenu</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>, g<SPAN style="COLOR: #66cc66">)</SPAN>;
                
                menuIsActive = <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">false</SPAN>;
                
            <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if "Cancel" was pressed on active menu</SPAN>
            
            
            <SPAN style="COLOR: #808080; FONT-STYLE: italic">// otherwise check navigation</SPAN>
            
            keyCode = getGameAction<SPAN style="COLOR: #66cc66">(</SPAN>keyCode<SPAN style="COLOR: #66cc66">)</SPAN>;
&nbsp;
            
            <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>keyCode == UP<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
&nbsp;
                currentlySelectedIndex--;
&nbsp;
                <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>currentlySelectedIndex &lt; <SPAN style="COLOR: #cc66cc">0</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
&nbsp;
                    currentlySelectedIndex = <SPAN style="COLOR: #cc66cc">0</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// stay within limits</SPAN>
&nbsp;
                <SPAN style="COLOR: #66cc66">}</SPAN>
                
                clearScreen<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
                menu.<SPAN style="COLOR: #006600">drawActiveMenu</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>, g, currentlySelectedIndex<SPAN style="COLOR: #66cc66">)</SPAN>; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// repaint active menu</SPAN>
&nbsp;
            <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if UP button was pressed</SPAN>
            <SPAN style="COLOR: #b1b100">else</SPAN> <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>keyCode == DOWN<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
&nbsp;
                currentlySelectedIndex++;
&nbsp;
                <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>currentlySelectedIndex &gt;= menuOptions.<SPAN style="COLOR: #006600">length</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
&nbsp;
                    currentlySelectedIndex = menuOptions.<SPAN style="COLOR: #006600">length</SPAN> - <SPAN style="COLOR: #cc66cc">1</SPAN>; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// stay within limits</SPAN>
                
                <SPAN style="COLOR: #66cc66">}</SPAN>
                
                clearScreen<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
                menu.<SPAN style="COLOR: #006600">drawActiveMenu</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>, g, currentlySelectedIndex<SPAN style="COLOR: #66cc66">)</SPAN>;</PRE><PRE> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// repaint active menu</SPAN>
&nbsp;
            <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if DOWN button was pressed</SPAN>
            <SPAN style="COLOR: #b1b100">else</SPAN> <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>keyCode == FIRE<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
                
                <SPAN style="COLOR: #808080; FONT-STYLE: italic">// menu option is selected</SPAN>
                
                <SPAN style="COLOR: #808080; FONT-STYLE: italic">// simply draw selected option</SPAN>
                
                clearScreen<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
                g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0x000000<SPAN style="COLOR: #66cc66">)</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// black</SPAN>
                
                g.<SPAN style="COLOR: #006600">drawString</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #ff0000">"["</SPAN> +</PRE><PRE> menuOptions<SPAN style="COLOR: #66cc66">[</SPAN>currentlySelectedIndex<SPAN style="COLOR: #66cc66">]</SPAN> + </PRE><PRE><SPAN style="COLOR: #ff0000">"] was selected"</SPAN>, <SPAN style="COLOR: #cc66cc">10</SPAN>, <SPAN style="COLOR: #cc66cc">15</SPAN>, g.<SPAN style="COLOR: #006600">LEFT</SPAN> | g.<SPAN style="COLOR: #006600">TOP</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
                menu.<SPAN style="COLOR: #006600">drawInactiveMenu</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>, g<SPAN style="COLOR: #66cc66">)</SPAN>;
                
                menuIsActive = <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">false</SPAN>;
                
            <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if FIRE button was pressed</SPAN>
            
            
        <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if menu is active</SPAN>
        <SPAN style="COLOR: #b1b100">else</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw inactive menu</SPAN>
            
            <SPAN style="COLOR: #808080; FONT-STYLE: italic">// check if the "Options" or "Exit" buttons were pressed</SPAN>
            
            <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>keyCode == LEFT_SOFTKEY_CODE<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// "Options" pressed</SPAN>
                
                clearScreen<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
                currentlySelectedIndex = <SPAN style="COLOR: #cc66cc">0</SPAN>;
                
                menu.<SPAN style="COLOR: #006600">drawActiveMenu</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>, g, currentlySelectedIndex<SPAN style="COLOR: #66cc66">)</SPAN>; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// activate menu</SPAN>
                
                menuIsActive = <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">true</SPAN>;
                
            <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if "Options" was pressed</SPAN>
            <SPAN style="COLOR: #b1b100">else</SPAN> <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>keyCode == RIGHT_SOFTKEY_CODE<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
                
                exitGUI<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
            <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if "Exit" was pressed</SPAN>
            
        <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if menu is not active</SPAN>
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end keyPressed</SPAN>
    
    
    
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="COLOR: #993333">void</SPAN> exitGUI<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        midlet.<SPAN style="COLOR: #006600">destroyApp</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">false</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        midlet.<SPAN style="COLOR: #006600">notifyDestroyed</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end exitGUI</SPAN>
    
    
    
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="COLOR: #993333">void</SPAN> clearScreen<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0xffffff<SPAN style="COLOR: #66cc66">)</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// white</SPAN>
        g.<SPAN style="COLOR: #006600">fillRect</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #cc66cc">0</SPAN>, <SPAN style="COLOR: #cc66cc">0</SPAN>, width, height<SPAN style="COLOR: #66cc66">)</SPAN>;
        flushGraphics<SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end clearScreen</SPAN>
    
<SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end GUI.java</SPAN></PRE>
<P><BR></P><A></A>
<H3><SPAN>Menu.java</SPAN></H3>
<P><BR></P><PRE><SPAN style="COLOR: #a1a100">import java.util.*;</SPAN>
&nbsp;
<SPAN style="COLOR: #a1a100">import javax.microedition.lcdui.*;</SPAN>
<SPAN style="COLOR: #a1a100">import javax.microedition.lcdui.game.*;</SPAN>
&nbsp;
&nbsp;
&nbsp;
<SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">class</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMenu+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Menu</U></SPAN></A> <SPAN style="COLOR: #66cc66">{</SPAN>
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A> leftOption; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// will be displayed when menu is inactive</SPAN>
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A> rightOption; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// will be displayed when menu is inactive</SPAN>
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A> cancelOption = <SPAN style="COLOR: #ff0000">"Cancel"</SPAN>; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// also may be "Back" or something else</SPAN>
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A><SPAN style="COLOR: #66cc66">[</SPAN><SPAN style="COLOR: #66cc66">]</SPAN> menuOptions;
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">private</SPAN> <SPAN style="COLOR: #993333">int</SPAN> padding = <SPAN style="COLOR: #cc66cc">5</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// just like in CSS</SPAN>
&nbsp;
&nbsp;
    
    
    
    <SPAN style="COLOR: #808080; FONT-STYLE: italic">/**
     * Creates a new instance of Menu.
     */</SPAN>
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMenu+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Menu</U></SPAN></A><SPAN style="COLOR: #66cc66">(</SPAN><A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A> leftOption, <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A> rightOption, <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>String</U></SPAN></A><SPAN style="COLOR: #66cc66">[</SPAN><SPAN style="COLOR: #66cc66">]</SPAN> </PRE><PRE>menuOptions<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>.<SPAN style="COLOR: #006600">leftOption</SPAN> = leftOption;
        <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>.<SPAN style="COLOR: #006600">rightOption</SPAN> = rightOption;
        <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">this</SPAN>.<SPAN style="COLOR: #006600">menuOptions</SPAN> = menuOptions;
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end constructor</SPAN>
    
    
    
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="COLOR: #993333">void</SPAN> drawInactiveMenu<SPAN style="COLOR: #66cc66">(</SPAN>GameCanvas canvas, <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AGraphics+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Graphics</U></SPAN></A> g<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// create inactive menu font</SPAN>
        
        <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A> font = <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">getFont</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">FACE_SYSTEM</SPAN>, <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">STYLE_BOLD</SPAN>,</PRE><PRE> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">SIZE_MEDIUM</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        <SPAN style="COLOR: #993333">int</SPAN> fontHeight = font.<SPAN style="COLOR: #006600">getHeight</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// clear inactive menu background</SPAN>
        
        <SPAN style="COLOR: #993333">int</SPAN> width = canvas.<SPAN style="COLOR: #006600">getWidth</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        <SPAN style="COLOR: #993333">int</SPAN> height = canvas.<SPAN style="COLOR: #006600">getHeight</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0xcccccc<SPAN style="COLOR: #66cc66">)</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// grey color</SPAN>
        g.<SPAN style="COLOR: #006600">fillRect</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #cc66cc">0</SPAN>, height - fontHeight - <SPAN style="COLOR: #cc66cc">2</SPAN> * padding, width, height<SPAN style="COLOR: #66cc66">)</SPAN>;
        
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw left and right menu options</SPAN>
        
        g.<SPAN style="COLOR: #006600">setFont</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>font<SPAN style="COLOR: #66cc66">)</SPAN>;
        g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0x000000<SPAN style="COLOR: #66cc66">)</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// black</SPAN>
        
        g.<SPAN style="COLOR: #006600">drawString</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>leftOption, padding, height - padding, </PRE><PRE>g.<SPAN style="COLOR: #006600">LEFT</SPAN> | g.<SPAN style="COLOR: #006600">BOTTOM</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        g.<SPAN style="COLOR: #006600">drawString</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>rightOption, width - padding, height - padding, </PRE><PRE>g.<SPAN style="COLOR: #006600">RIGHT</SPAN> | g.<SPAN style="COLOR: #006600">BOTTOM</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        canvas.<SPAN style="COLOR: #006600">flushGraphics</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end drawInactiveMenu</SPAN>
    
    
    
    
    
    
    <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">public</SPAN> <SPAN style="COLOR: #993333">void</SPAN> drawActiveMenu<SPAN style="COLOR: #66cc66">(</SPAN>GameCanvas canvas,</PRE><PRE> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AGraphics+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Graphics</U></SPAN></A> g, <SPAN style="COLOR: #993333">int</SPAN> selectedOptionIndex<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// create active menu font</SPAN>
        
        <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A> font = <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">getFont</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">FACE_SYSTEM</SPAN>,</PRE><PRE> <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">STYLE_BOLD</SPAN>, <A href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFont+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><SPAN style="FONT-WEIGHT: bold; COLOR: #aaaadd"><U>Font</U></SPAN></A>.<SPAN style="COLOR: #006600">SIZE_MEDIUM</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        <SPAN style="COLOR: #993333">int</SPAN> fontHeight = font.<SPAN style="COLOR: #006600">getHeight</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// clear menu bar background</SPAN>
        
        <SPAN style="COLOR: #993333">int</SPAN> width = canvas.<SPAN style="COLOR: #006600">getWidth</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        <SPAN style="COLOR: #993333">int</SPAN> height = canvas.<SPAN style="COLOR: #006600">getHeight</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0xcccccc<SPAN style="COLOR: #66cc66">)</SPAN>;
        g.<SPAN style="COLOR: #006600">fillRect</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #cc66cc">0</SPAN>, height - fontHeight - <SPAN style="COLOR: #cc66cc">2</SPAN> * padding, </PRE><PRE>width, height<SPAN style="COLOR: #66cc66">)</SPAN>;
        
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw default menu bar options</SPAN>
        
        g.<SPAN style="COLOR: #006600">setFont</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>font<SPAN style="COLOR: #66cc66">)</SPAN>;
        g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0x000000<SPAN style="COLOR: #66cc66">)</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// black</SPAN>
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw "Cancel" option</SPAN>
        g.<SPAN style="COLOR: #006600">drawString</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>cancelOption, width - padding, height - padding,</PRE><PRE> g.<SPAN style="COLOR: #006600">RIGHT</SPAN> | g.<SPAN style="COLOR: #006600">BOTTOM</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        canvas.<SPAN style="COLOR: #006600">flushGraphics</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
        
        
        <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw menu options</SPAN>
        
        <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>menuOptions&nbsp;!= <SPAN style="FONT-WEIGHT: bold; COLOR: #000000">null</SPAN><SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
            
            <SPAN style="COLOR: #808080; FONT-STYLE: italic">// check out the max width of a menu</SPAN></PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic"> (for the specified menu font)</SPAN>
            
            <SPAN style="COLOR: #993333">int</SPAN> menuMaxWidth = <SPAN style="COLOR: #cc66cc">0</SPAN>;
            <SPAN style="COLOR: #993333">int</SPAN> menuMaxHeight = <SPAN style="COLOR: #cc66cc">0</SPAN>;
            <SPAN style="COLOR: #993333">int</SPAN> currentWidth = <SPAN style="COLOR: #cc66cc">0</SPAN>;
&nbsp;
            <SPAN style="COLOR: #808080; FONT-STYLE: italic">// we'll simply check each option and find the</SPAN></PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic"> maximal width</SPAN>
&nbsp;
            <SPAN style="COLOR: #b1b100">for</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #993333">int</SPAN> i = <SPAN style="COLOR: #cc66cc">0</SPAN>; i &lt; menuOptions.<SPAN style="COLOR: #006600">length</SPAN>; i++<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
                
                currentWidth = font.<SPAN style="COLOR: #006600">stringWidth</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>menuOptions<SPAN style="COLOR: #66cc66">[</SPAN>i<SPAN style="COLOR: #66cc66">]</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
                <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>currentWidth &gt; menuMaxWidth<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
                    menuMaxWidth = currentWidth; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// update</SPAN>
                <SPAN style="COLOR: #66cc66">}</SPAN>
                
                menuMaxHeight += fontHeight + padding; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// for a current menu option</SPAN>
                
            <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end for each menu option</SPAN>
            
            
            menuMaxWidth += <SPAN style="COLOR: #cc66cc">2</SPAN> * padding; </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// padding from left and right</SPAN>
            
            
            <SPAN style="COLOR: #808080; FONT-STYLE: italic">// now we know the bounds of active menu</SPAN>
            
            
            <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw active menu's background</SPAN>
            
            g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0xcccccc<SPAN style="COLOR: #66cc66">)</SPAN>;
            g.<SPAN style="COLOR: #006600">fillRect</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #cc66cc">0</SPAN>, <SPAN style="COLOR: #808080; FONT-STYLE: italic">// x</SPAN>
                       height - fontHeight - </PRE><PRE><SPAN style="COLOR: #cc66cc">2</SPAN> * padding - menuMaxHeight, <SPAN style="COLOR: #808080; FONT-STYLE: italic">// y</SPAN>
                       menuMaxWidth,
                       menuMaxHeight<SPAN style="COLOR: #66cc66">)</SPAN>;
            
            
            <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw menu options (from up to bottom)</SPAN>
            
            g.<SPAN style="COLOR: #006600">setFont</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>font<SPAN style="COLOR: #66cc66">)</SPAN>;
            
            <SPAN style="COLOR: #993333">int</SPAN> menuOptionX = padding;
            <SPAN style="COLOR: #993333">int</SPAN> menuOptionY = height - fontHeight - <SPAN style="COLOR: #cc66cc">2</SPAN> * padding</PRE><PRE> - menuMaxHeight + padding;
            
            <SPAN style="COLOR: #b1b100">for</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #993333">int</SPAN> i = <SPAN style="COLOR: #cc66cc">0</SPAN>; i &lt; menuOptions.<SPAN style="COLOR: #006600">length</SPAN>; i++<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN>
                
                <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #66cc66">(</SPAN>i&nbsp;!= selectedOptionIndex<SPAN style="COLOR: #66cc66">)</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN> </PRE><PRE><SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw unselected menu option</SPAN>
                    
                    g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0x000000<SPAN style="COLOR: #66cc66">)</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// black</SPAN>
                    
                <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if draw unselected menu option</SPAN>
                <SPAN style="COLOR: #b1b100">else</SPAN> <SPAN style="COLOR: #66cc66">{</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// draw selected menu option</SPAN>
                    
                         g.<SPAN style="COLOR: #006600">setColor</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>0x0000ff<SPAN style="COLOR: #66cc66">)</SPAN>; <SPAN style="COLOR: #808080; FONT-STYLE: italic">// blue</SPAN>
                    
                <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if draw selected menu option</SPAN>
                
                
                g.<SPAN style="COLOR: #006600">drawString</SPAN><SPAN style="COLOR: #66cc66">(</SPAN>menuOptions<SPAN style="COLOR: #66cc66">[</SPAN>i<SPAN style="COLOR: #66cc66">]</SPAN>, menuOptionX, </PRE><PRE>menuOptionY, g.<SPAN style="COLOR: #006600">LEFT</SPAN> | g.<SPAN style="COLOR: #006600">TOP</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
                
                menuOptionY += padding + fontHeight;
                
            <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end for each menu option</SPAN>
            
            
            canvas.<SPAN style="COLOR: #006600">flushGraphics</SPAN><SPAN style="COLOR: #66cc66">(</SPAN><SPAN style="COLOR: #66cc66">)</SPAN>;
            
            
        <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end if menu options were specified</SPAN>
        
    <SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end drawActiveMenu</SPAN>
    
<SPAN style="COLOR: #66cc66">}</SPAN> <SPAN style="COLOR: #808080; FONT-STYLE: italic">// end Menu.java</SPAN></PRE></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/704705432008524113216244</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/704705432008524113216244</guid>
    <pubDate>Tue, 24 Jun 2008 11:32:16 +0800</pubDate>
    <dcterms:modified>2008-06-24T11:34:06+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[将Image对像转换为png格式的byte数组 ]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/704705432008524113053644</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 由于J2me中没有ImageIO 导致一些功能无法在J2me上实现.导致大家无法实现一些功能.如绘图软件.大头贴.截屏等.貌私新版的jsr什么什么实现了将Image 输出为png or jpg但估计等到这种手机上市的时候以是几年后的事情了.其实在j2me上将Image 对象转成png格式的数组并不是不可能的任务.</P>
<P>原理上说</P>
<P>1 获取Image 的ARGB数据</P>
<P>2将ARGB转换成PNG存储用的的RGBA格式</P>
<P>3RGBA格式的数据还要做些小的处理每行后面加一个byte 0</P>
<P>4用LZ77方法将RGBA格式的数组压缩</P>
<P>5附加正确格式PNG24文件头输出即可包括IHEAD IHDR IEND IDAT外壳</P>
<P>但是使用LZ77压缩手机上无论时间或空间都是不太能接受的</P>
<P>还好LZ77 存在一钟无压缩的压缩方法本文的方法就是使用无压缩的方式搞定数据压缩的</P>
<P>参阅了jzlib 代码</P>
<P>使用300行左右的代码搞定了将Image对象转png</P>
<P>import javax.microedition.lcdui.*;<BR>import java.io.*;<BR>import javax.microedition.io.file.FileConnection;<BR>import javax.microedition.io.Connector;</P>
<P>public class CGame extends Canvas {<BR>&nbsp;&nbsp;&nbsp; //Image2Bytes by AnderLu<BR>&nbsp;&nbsp;&nbsp; //生成的byte[]数组可直接用于外部存储为.png格式的图片文件看图软件可直接打开<BR>&nbsp;&nbsp;&nbsp; public static int IDATPOS;<BR>&nbsp;&nbsp;&nbsp; public static byte[] HEADChunk = {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte) 0x89, (byte) 0x50,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte) 0x4E, (byte) 0x47,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ex.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte data[] = Image2Bytes(img);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.img = Image.createImage(data, 0, data.length);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; saveFile("<A href="file:///e:/a.png" target=_blank><U><FONT color=#0000ff>file:///e:/a.png</FONT></U></A>", data);<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; protected void paint(Graphics g) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.setColor(0xffffff);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.fillRect(0, 0, 240, 320);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.drawImage(img, 0, 0, 0);<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; /**保存文件<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @path:路径<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @fileData:文件数据<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return: 0:出现异常,1:保存成功<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int saveFile(String path, byte[] fileData) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileConnection fc = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fc = (FileConnection) Connector.open(path, Connector.READ_WRITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!fc.exists()) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fc.create();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OutputStream os = fc.openOutputStream();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.write(fileData);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.flush();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fc.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IOException ex) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ex.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; public byte[] Image2Bytes(Image img) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int w = img.getWidth();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int h = img.getHeight();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int offset = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte buffer[] = new byte[(w * 4 + 1) * h + offset];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getImageBufferForImageARGB8888(img, buffer, w, h, offset);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.gc();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ByteArrayOutputStream baos = new ByteArrayOutputStream();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataOutputStream dout = new DataOutputStream(baos);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WritePng(dout, w, h, buffer, null, false, offset);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] data = baos.toByteArray();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writeCRC(data, 8); //更新IHDR CRC<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writeCRC(data, 33); //更新PLTE CRC<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writeCRC(data, IDATPOS); //更新IDAT CRC<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buffer = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.gc();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return data;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IOException ex) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ex.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; public static void writeCRC(byte[] data, int chunkpos) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int chunklen = ((data[chunkpos] &amp; 0xFF) &lt;&lt; 24)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | ((data[chunkpos + 1] &amp; 0xFF) &lt;&lt; 16)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | ((data[chunkpos + 2] &amp; 0xFF) &lt;&lt; 8)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | (data[chunkpos + 3] &amp; 0xFF);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int sum = CRCChecksum(data, chunkpos + 4, 4 + chunklen) ^ 0xffffffff;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int val = sum;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int pos = chunkpos + 8 + chunklen;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[pos] = (byte) ((val &amp; 0xFF000000) &gt;&gt; 24);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[pos + 1] = (byte) ((val &amp; 0xFF0000) &gt;&gt; 16);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[pos + 2] = (byte) ((val &amp; 0xFF00) &gt;&gt; 8);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[pos + 3] = (byte) (val &amp; 0xFF);<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; public static int[] crc_table; //CRC 表<BR>&nbsp;&nbsp;&nbsp; public static int CRCChecksum(byte[] buf, int off, int len) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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int index, int len) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int BASE = 65521;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int NMAX = 5552;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //TODO remove this function at all<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (buf == null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1L;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long s1 = adler &amp; 0xffff;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long s2 = (adler &gt;&gt; 16) &amp; 0xffff;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int k;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (len &gt; 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = len &lt; NMAX ? len : NMAX;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len -= k;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (k &gt;= 16) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s1 += buf[index++] &amp; 0xff;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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p; s1 %= BASE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s2 %= BASE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (s2 &lt;&lt; 16) | s1;<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; public static void WritePng(DataOutputStream output, int width, int height,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] buffer, byte[] colors,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean Transparent, int offset) throws<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IOException {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int adler = (int) adler32(1l, buffer, offset, buffer.length - offset);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] lenNlen = { //压缩块的LEN和NLEN信息<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte) 0,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte) 0xfa, (byte) 0x7e,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte) 0x05, (byte) 0x81<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IDATPOS = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.write(HEADChunk);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IDATPOS += HEADChunk.length;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //写IHDR<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(13); //len<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(1229472850); //IHDR type code<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(width); //写宽度<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(height); //写高度<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeByte(8); //1Bitdepth<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (colors == null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeByte(6); //2ColorType<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeByte(3); //2ColorType<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeByte(0); //3CompressionMethod<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeByte(0); //4Filter method<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeByte(0); //5Interlace method<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(0); //写crc<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IDATPOS += 25;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //写PLTE<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (colors != null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(colors.length); //len<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(1347179589); //type code<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.write(colors); //data<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(0); //crc<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IDATPOS += colors.length + 12;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //写TRNS<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (Transparent) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.write(tRNSChunk);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IDATPOS += tRNSChunk.length;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //写IDAT<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] dpixels = buffer;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int bufferlen = dpixels.length - offset;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int blocklen = 32506;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int blocknum = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((dpixels.length % blocklen) == 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; blocknum = bufferlen / blocklen;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; blocknum = (bufferlen / blocklen) + 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int IDATChunkLen = (bufferlen + 6 + blocknum * 5);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(IDATChunkLen); //len<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(1229209940); //idat type code<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeShort((short) 0x78da); //78da<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; blocknum; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int off = i * blocklen;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int len = bufferlen - off;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (len &gt;= blocklen) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len = blocklen;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lenNlen[0] = (byte) 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lenNlen[0] = (byte) 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int msb = (len &amp; 0xff);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int lsb = (len &gt;&gt;&gt; 8);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lenNlen[1] = (byte) msb;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lenNlen[2] = (byte) lsb;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lenNlen[3] = (byte) (msb ^ 0xff);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lenNlen[4] = (byte) (lsb ^ 0xff);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.write(lenNlen);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.write(dpixels, off + offset, len);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(adler); //IDAT adler<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.writeInt(0); //IDAT crc<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.write(IENDChunk);<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; public static void getImageBufferForImageARGB8888(Image img, byte[] rawByte,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int w, int h, int off) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int n = off;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int[] raw = new int[w];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; h; j++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; img.getRGB(raw, 0, w, 0, j, w, 1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; raw.length; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int ARGB = raw[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int a = (ARGB &amp; 0xff000000) &gt;&gt; 24;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int r = (ARGB &amp; 0xff0000) &gt;&gt; 16;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int g = (ARGB &amp; 0xff00) &gt;&gt; 8;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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;&nbsp;&nbsp;&nbsp;&nbsp; raw = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.gc();<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR>附件zip可改名为jar在手机上直接执行</P>
<P>内附有源代码</P>
<P>需要手机支持JSR75有E盘(就是存储卡)生成的文件为a.png</P>
<P>可使用任意的看图软件打开</P>
<P>N73,6270测试成功</P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/704705432008524113053644</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/704705432008524113053644</guid>
    <pubDate>Tue, 24 Jun 2008 11:30:53 +0800</pubDate>
    <dcterms:modified>2008-06-24T11:33:24+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[java对象转为java String的几种常用方法剖析 ]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/70470543200851782139929</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在java项目的实际开发和应用中，常常需要用到将对象转为String这一基本功能。本文将对常用的转换方法进行一个总结。常用的方法有Object#toString()，（String）要转换的对象，String.valueOf(Object)等。下面对这些方法一一进行分析。</P>
<P>方法1：采用 Object#toString()方法<BR>请看下面的例子：</P>
<P>Object object = getObject();</P>
<P>System.out.println(object.toString());</P>
<P>在这种使用方法中，因为java.lang.Object类里已有public方法.toString()，所以对任何严格意义上的java对象都可以调用此方法。但在使用时要注意，必须保证object不是null值，否则将抛出NullPointerException异常。采用这种方法时，通常派生类会覆盖Object里的toString（）方法。</P>
<P>方法2：采用类型转换（String）object方法<BR>这是标准的类型转换，将object转成String类型的值。使用这种方法时，需要注意的是类型必须能转成String类型。因此最好用instanceof做个类型检查，以判断是否可以转换。否则容易抛出CalssCastException异常。此外，需特别小心的是因定义为Object 类型的对象在转成String时语法检查并不会报错，这将可能导致潜在的错误存在。这时要格外小心。如：</P>
<P>Object obj = new Integer(100);</P>
<P>String strVal = (String)obj;</P>
<P>在运行时将会出错，因为将Integer类型强制转换为String类型，无法通过。但是，</P>
<P>Integer obj = new Integer(100);</P>
<P>String strVal = (String)obj;</P>
<P>如是格式代码，将会报语法错误。</P>
<P>此外，因null值可以强制转换为任何java类类型，(String)null也是合法的。</P>
<P>方法3：采用String.valueOf(Object)<BR>String.valueOf(Object)的基础是Object#toString()。但它与Object#toString()又有所不同。在前面方法1的分析中提到，使用后者时需保证不为null。但采用第三种方法时，将不用担心object是否为null值这一问题。为了便于说明问题，我们来分析一下相关的源代码。Jdk里String# valueOf(Object)源码如下：</P>
<P>/**</P>
<P>* Returns the string representation of the Object argument.</P>
<P>*</P>
<P>* @param obj an Object.</P>
<P>* @return if the argument is null, then a string equal to</P>
<P>* "null"; otherwise, the value of</P>
<P>* obj.toString() is returned.</P>
<P>* @see java.lang.Object#toString()</P>
<P>*/</P>
<P>public static String valueOf(Object obj) {</P>
<P>return (obj == null) ? "null" : obj.toString();</P>
<P>}</P>
<P>从上面的源码可以很清晰的看出null值不用担心的理由。但是，这也恰恰给了我们隐患。我们应当注意到，当object为null时，String.valueOf（object）的值是字符串”null”，而不是null！！！在使用过程中切记要注意。试想一下，如果我们用 if(String.valueOf（object）==null){System.out.println(“传入的值是null！”);}这样的语句将可能会发生什么问题。再想一下，向控制台输出时，在视觉上如下语句在执行的结果上有什么不同：</P>
<P>System.out.println(String.valueOf(null));</P>
<P>System.out.println(null);</P>
<P>我们看到的输出将是一模一样的东西：null，但它们意义相同吗？</P>
<P>&nbsp;</P>
<P>以上是对object对象转换为String的一些总结。</P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/70470543200851782139929</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/70470543200851782139929</guid>
    <pubDate>Tue, 17 Jun 2008 08:21:39 +0800</pubDate>
    <dcterms:modified>2008-06-17T08:22:54+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[Java socket功能的一个小应用 ]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/7047054320085178205141</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sun公司java语言的的推出可以说是一个划时代的语言革命，这种语言同时能适应应用软件领域和网络开发领域，其优秀的网络开发功能更是让人惊叹，利用java你可以轻松的实现在其他的语言中要花很多代码和时间才能实现的功能：你可以很轻松的用java开发一个同Windows的那个notebook一样的程序，甚至你可以朝开发word进军，你可以很轻松的实现网络通信，你可以在网络中展现漂亮的图像效果（这种效果你是使用gif.jpg以及flash始终做不到的），你可以使用java开发Webserver和中间件(参考<A href="http://www.apusic.com">www.apusic.com</A>)实现负载均衡等功能；你也可以在开发你的邮件系统；可以生成功能非常强大的java组件运行于asp中，比vb、vc和Borland C++ builder开发相应的组件来的容易，而且功能也强大的多。java的种种精彩不是笔者可以在这里完全可以介绍完的，但是下面笔者给出的一段不超过20行的代码可以让你感觉到java这种语言的真正强大.。你如果能灵活的运用java，我相信，你一定可以做得比我更精彩！ok!let's begain! <BR>本文利用Java的socket通信功能实现了下面两种作用： <BR>1、可以查询internet中domain和ip的（具体的用法见后文）； <BR>2、可以在局域网中通过机器名查询相应的ip和通过ip查询相应的机器名； <BR>原理：下面的代码使用socket利用了系统查询了相应的internet上的dns解析和局域网中dns解析来获得相应的数据资料； <BR>//代码开始 <BR>/* <BR>本代码只是提供测试，不提供完全的实际应用的功能，但是你可以利用下面的思路开发你自己的相应的程序，请注意本文提供的socket通信在internet上查询domain（或者ip）的功能不完全正确，因为本文使用的socket访问方式不是访问的相应的domain数据库，所以不能实现精确的domain（或者ip）的查询，但是一切在internet上能被访问到的domain（或者ip）都可以都可以被查询到相应的ip(或者domain),如果需要你也可以使用本文所提及socket访问相应的domain数据库得到完全正确的domain查询功能！在run下面的程序以前，请确定你已经安装了jdk1.3以及配置了相关的参数。 <BR>*/ <BR>import java.net.*; <BR>class search <BR>{ <BR>public static void main(String[] args) <BR>{ <BR>try <BR>{ <BR>InetAddress address=InetAddress.getByName(args[0]); <BR>//获得你要查询的ip或者domain <BR>System.out.println(address); <BR>//输出查询结果 <BR>} <BR>catch (Exception e) <BR>{ <BR>//错误提示 <BR>System.out.println("对不起，你查询的站点或者IP未被程序找到，可能原因如下"); <BR>System.out.println("1,该站点尚未注册或者该站点尚未启用"); <BR>System.out.println("2.你没有连接到网络"); <BR>System.out.println("3.该ip地址可能已被DHCP解析，但是尚未指向相应的站点"); <BR>} <BR>} <BR>} <BR>//代码结束 <BR>把上面的代码save为search.java <BR>好了进入dos模式使用"javac search.java"编译search.java为search.class,然后，我们就可以开始使用我们的代码来测试了！ <BR>1。如果你要查询internet中domain,我们以<A href="http://www.sina.com/">www.sina.com</A>为例你可以输入:"java search <A href="http://www.sina.com/">www.sina.com</A>" 你可以得到下面的结果:"<A href="http://www.sina.com/206.204.114.57">www.sina.com/206.204.114.57</A>",返回给你的数据中就包含了<A href="http://www.sina.com/">www.sina.com</A>的ip地址；如果你再输入:"java search 206.204.114.57"你返回的结果为"<A href="http://www.sina.com/206.204.114.57">www.sina.com/206.204.114.57</A>"，同样返回的结果包含了sina的domain,但是需要注意的如果你的ip解析了多个domain的话可能不能准确的通过ip反查domain,而且有的站点为了访问的方便使用了不同的ip,即一个站点有几个ip,这样你返回的只是第一个ip地址，其他还有一些问题就留待你自己去发现了，前面说过本文只是给你提供一种思路; <BR>2.在局域网中通过机器名反查ip：这点我相信是很有用的，嘿嘿，具体的使用方法和前面的差不多，但是你发现有了这个东西你可以很轻松的搞到任何人的相对应的机器名和ip地址，不要乱用哟！当然你也可以通过ip反查机器名；但是同样有点不是问题的问题：如果该机器不存在的话是不会报错的，而是以一种前后都是ip的形式返回结果，好了，其他的我也不多说了，你就慢慢的用吧！ <BR>上面给出的代码可能对于不习惯java的朋友来说不是很好操作，但是你可以把它改成applet在浏览器中运行! <BR>本文介绍的java功能只是java网络功能部分中很小的一个部分，java强大的功能从本文提供的代码只可以体现很小很小的一个部分，如果你的E文够好，建议你多访问一下java.sun.com多学习一下java.！ <BR>备注：本人环境Win2000 professional+jdk1.3 </P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/7047054320085178205141</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/7047054320085178205141</guid>
    <pubDate>Tue, 17 Jun 2008 08:20:51 +0800</pubDate>
    <dcterms:modified>2008-06-17T08:22:16+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[JAVA基础知识精华总结]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/704705432008511995469</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P>1、 对象的初始化 <BR>(1) 非静态对象的初始化 <BR>在创建对象时，对象所在类的所有数据成员会首先进行初始化。 <BR>基本类型：int型，初始化为0。 <BR>如果为对象：这些对象会按顺序初始化。 <BR>※在所有类成员初始化完成之后，才调用本类的构造方法创建对象。 <BR>构造方法的作用就是初始化。 <BR>(2) 静态对象的初始化 <BR>程序中主类的静态变量会在main方法执行前初始化。 <BR>不仅第一次创建对象时，类中的所有静态变量都初始化，并且第一次访问某类（注意此时 <BR>未创建此类对象）的静态对象时，所有的静态变量也要按它们在类中的顺序初始化。 <BR>2、 继承时，对象的初始化过程 <BR>(1) 主类的超类由高到低按顺序初始化静态成员，无论静态成员是否为private。 <BR>(2) 主类静态成员的初始化。 <BR>(3) 主类的超类由高到低进行默认构造方法的调用。注意，在调用每一个超类的默认构造 <BR>方法前，先进行对此超类进行非静态对象的初始化。 <BR>(4) 主类非静态成员的初始化。 <BR>(5) 调用主类的构造方法。 <BR>3、 关于构造方法 <BR>(1) 类可以没有构造方法，但如果有多个构造方法，就应该要有默认的构造方法，否则在继承此类时，需要在子类中显式调用父类的某一个非默认的构造方法了。 <BR>(2) 在一个构造方法中，只能调用一次其他的构造方法，并且调用构造方法的语句必须是 <BR>第一条语句。 <BR>4、 有关public、private和protected <BR>(1) 无public修饰的类，可以被其他类访问的条件是：a.两个类在同一文件中，b.两个类 <BR>在同一文件夹中，c.两个类在同一软件包中。 <BR>(2) protected：继承类和同一软件包的类可访问。 <BR>(3) 如果构造方法为private，那么在其他类中不能创建该类的对象。 <BR>5、 抽象类 <BR>(1) 抽象类不能创建对象。 <BR>(2) 如果一个类中一个方法为抽象方法，则这个类必须为abstract抽象类。 <BR>(3) 继承抽象类的类在类中必须实现抽象类中的抽象方法。 <BR>(4) 抽象类中可以有抽象方法，也可有非抽象方法。抽象方法不能为private。 <BR>(5) 间接继承抽象类的类可以不给出抽象方法的定义。 <BR>6、 final关键字 <BR>(1) 一个对象是常量，不代表不能转变对象的成员，仍可以其成员进行操作。 <BR>(2) 常量在使用前必须赋值，但除了在声明的同时初始化外，就只能在构造方法中初始化 <BR>。 <BR>(3) final修饰的方法不能被重置（在子类中不能出现同名方法）。 <BR>(4) 如果声明一个类为final，则所有的方法均为final，无论其是否被final修饰，但数据 <BR>成员可为final也可不是。 <BR>7、 接口interface （用implements来实现接口） <BR>(1) 接口中的所有数据均为 static和final即静态常量。尽管可以不用这两个关键字修饰 <BR>，但必须给常量赋初值。 <BR>(2) 接口中的方法均为public，在实现接口类中，实现方法必须可public关键字。 <BR>(3) 如果使用public来修饰接口，则接口必须与文件名相同。 <BR>8、 多重继承 <BR>(1) 一个类继承了一个类和接口，那么必须将类写在前面，接口写在后面，接口之间用逗 <BR>号分隔。 <BR>(2) 接口之间可多重继承，注意使用关键字extends。 <BR>(3) 一个类虽只实现了一个接口，但不仅要实现这个接口的所有方法，还要实现这个接口 <BR>继承的接口的方法，接口中的所有方法均须在类中实现。 <BR>9、 接口的嵌入 <BR>(1) 接口嵌入类中，可以使用private修饰。此时，接口只能在所在的类中实现，其他类不 <BR>能访问。 <BR>(2) 嵌入接口中的接口一定要为public。 <BR>10、类的嵌入 <BR>(1) 类可以嵌入另一个类中，但不能嵌入接口中。 <BR>(2) 在静态方法或其他方法中，不能直接创建内部类对象柰ü侄卫慈〉谩?<BR>手段有两种： <BR>class A { <BR>class B {} <BR>B getB() { <BR>B b = new B(); <BR>return b; <BR>} <BR>} <BR>static void m() { <BR>A a = new A(); <BR>A.B ab = a.getB(); // 或者是 A.B ab = a.new B(); <BR>} <BR>(3) 一个类继承了另一个类的内部类，因为超类是内部类，而内部类的构造方法不能自动 <BR>被调用，这样就需要在子类的构造方法中明确的调用超类的构造方法。 <BR>接上例： <BR>class C extends A.B { <BR>C() { <BR>new A().super(); // 这一句就实现了对内部类构造方法的调用。 <BR>} <BR>} <BR>构造方法也可这样写： <BR>C(A a) { <BR>a.super(); <BR>} // 使用这个构造方法创建对象，要写成C c = new C(a); a是A的对象。 <BR>11、异常类 <BR>JAVA中除了RunTimeException 类，其他异常均须捕获或抛出。<BR></P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/704705432008511995469</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/704705432008511995469</guid>
    <pubDate>Wed, 11 Jun 2008 09:09:54 +0800</pubDate>
    <dcterms:modified>2008-06-11T09:11:01+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[新手入门:写Java程序的三十个基本规则]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/7047054320085119658950</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P>(1) 类名首字母应该大写。字段、方法以及对象(句柄)的首字母应小写。对于所有标识符，其中包含的所有单词都应紧靠在一起，而且大写中间单词的首字母。例如： </P>
<P>　　ThisIsAClassName</P>
<P>　　thisIsMethodOrFieldName</P>
<P>　　若在定义中出现了常数初始化字符，则大写static final基本类型标识符中的所有字母。这样便可标志出它们属于编译期的常数。</P>
<P>　　Java包(Package)属于一种特殊情况：它们全都是小写字母，即便中间的单词亦是如此。对于域名扩展名称，如com，org，net或者edu等，全部都应小写(这也是Java 1.1和Java 1.2的区别之一)。</P>
<P>　　(2) 为了常规用途而创建一个类时，请采取“经典形式”，并包含对下述元素的定义：</P>
<P>　　equals()</P>
<P>　　hashCode()</P>
<P>　　toString()</P>
<P>　　clone()(implement Cloneable)</P>
<P>　　implement Serializable</P>
<P>　　(3) 对于自己创建的每一个类，都考虑置入一个main()，其中包含了用于测试那个类的代码。为使用一个项目中的类，我们没必要删除测试代码。若进行了任何形式的改动，可方便地返回测试。这些代码也可作为如何使用类的一个示例使用。</P>
<P>　　(4) 应将方法设计成简要的、功能性单元，用它描述和实现一个不连续的类接口部分。理想情况下，方法应简明扼要。若长度很大，可考虑通过某种方式将其分割成较短的几个方法。这样做也便于类内代码的重复使用(有些时候，方法必须非常大，但它们仍应只做同样的一件事情)。 (5) 设计一个类时，请设身处地为客户程序员考虑一下(类的使用方法应该是非常明确的)。然后，再设身处地为管理代码的人考虑一下(预计有可能进行哪些形式的修改，想想用什么方法可把它们变得更简单)。</P>
<P>　　(6) 使类尽可能短小精悍，而且只解决一个特定的问题。下面是对类设计的一些建议：</P>
<P>　　■一个复杂的开关语句：考虑采用“多形”机制</P>
<P>　　■数量众多的方法涉及到类型差别极大的操作：考虑用几个类来分别实现</P>
<P>　　■许多成员变量在特征上有很大的差别：考虑使用几个类 。</P>
<P>　　(7) 让一切东西都尽可能地“私有”——private。可使库的某一部分“公共化”(一个方法、类或者一个字段等等)，就永远不能把它拿出。若强行拿出，就可能破坏其他人现有的代码，使他们不得不重新编写和设计。若只公布自己必须公布的，就可放心大胆地改变其他任何东西。在多线程环境中，隐私是特别重要的一个因素——只有private字段才能在非同步使用的情况下受到保护。</P>
<P>　　(8) 谨惕“巨大对象综合症”。对一些习惯于顺序编程思维、且初涉OOP领域的新手，往往喜欢先写一个顺序执行的程序，再把它嵌入一个或两个巨大的对象里。根据编程原理，对象表达的应该是应用程序的概念，而非应用程序本身。</P>
<P>　　(9) 若不得已进行一些不太雅观的编程，至少应该把那些代码置于一个类的内部。</P>
<P>　　(10) 任何时候只要发现类与类之间结合得非常紧密，就需要考虑是否采用内部类，从而改善编码及维护工作(参见第14章14.1.2小节的“用内部类改进代码”)。</P>
<P>　　(11) 尽可能细致地加上注释，并用javadoc注释文档语法生成自己的程序文档。</P>
<P>　　(12) 避免使用“魔术数字”，这些数字很难与代码很好地配合。如以后需要修改它，无疑会成为一场噩梦，因为根本不知道“100”到底是指“数组大小”还是“其他全然不同的东西”。所以，我们应创建一个常数，并为其使用具有说服力的描述性名称，并在整个程序中都采用常数标识符。这样可使程序更易理解以及更易维护。</P>
<P>　　(13) 涉及构建器和异常的时候，通常希望重新丢弃在构建器中捕获的任何异常——如果它造成了那个对象的创建失败。这样一来，调用者就不会以为那个对象已正确地创建，从而盲目地继续。</P>
<P>　　(14) 当客户程序员用完对象以后，若你的类要求进行任何清除工作，可考虑将清除代码置于一个良好定义的方法里，采用类似于cleanup()这样的名字，明确表明自己的用途。除此以外，可在类内放置一个boolean(布尔)标记，指出对象是否已被清除。在类的finalize()方法里，请确定对象已被清除，并已丢弃了从RuntimeException继承的一个类(如果还没有的话)，从而指出一个编程错误。在采取象这样的方案之前，请确定finalize ()能够在自己的系统中工作(可能需要调用System.runFinalizersonExit(true)，从而确保这一行为)。</P>
<P>　　(15) 在一个特定的作用域内，若一个对象必须清除(非由垃圾收集机制处理)，请采用下述方法：初始化对象;若成功，则立即进入一个含有finally从句的try块，开始清除工作。</P>
<P>　　(16) 若在初始化过程中需要覆盖(取消)finalize()，请记住调用super.finalize()(若Object属于我们的直接超类，则无此必要)。在对finalize()进行覆盖的过程中，对super.finalize()的调用应属于最后一个行动，而不应是第一个行动，这样可确保在需要基础类组件的时候它们依然有效。</P>
<P>　　(17) 创建大小固定的对象集合时，请将它们传输至一个数组(若准备从一个方法里返回这个集合，更应如此操作)。这样一来，我们就可享受到数组在编译期进行类型检查的好处。此外，为使用它们，数组的接收者也许并不需要将对象“造型”到数组里。</P>
<P>　　(18) 尽量使用interfaces，不要使用abstract类。若已知某样东西准备成为一个基础类，那么第一个选择应是将其变成一个interface(接口)。只有在不得不使用方法定义或者成员变量的时候，才需要将其变成一个abstract(抽象)类。接口主要描述了客户希望做什么事情，而一个类则致力于(或允许)具体的实施细节。</P>
<P>　　(19) 在构建器内部，只进行那些将对象设为正确状态所需的工作。尽可能地避免调用其他方法，因为那些方法可能被其他人覆盖或取消，从而在构建过程中产生不可预知的结果(参见第7章的详细说明)。</P>
<P>　　(20) 对象不应只是简单地容纳一些数据;它们的行为也应得到良好的定义。</P>
<P>　　(21) 在现成类的基础上创建新类时，请首先选择“新建”或“创作”。只有自己的设计要求必须继承时，才应考虑这方面的问题。若在本来允许新建的场合使用了继承，则整个设计会变得没有必要地复杂。</P>
<P>　　(22) 用继承及方法覆盖来表示行为间的差异，而用字段表示状态间的区别。一个非常极端的例子是通过对不同类的继承来表示颜色，这是绝对应该避免的：应直接使用一个“颜色”字段。</P>
<P>　　(23) 为避免编程时遇到麻烦，请保证在自己类路径指到的任何地方，每个名字都仅对应一个类。否则，编译器可能先找到同名的另一个类，并报告出错消息。若怀疑自己碰到了类路径问题，请试试在类路径的每一个起点，搜索一下同名的.class文件。</P>
<P>　　(24) 在Java 1.1 AWT中使用事件“适配器”时，特别容易碰到一个陷阱。若覆盖了某个适配器方法，同时拼写方法没有特别讲究，最后的结果就是新添加一个方法，而不是覆盖现成方法。然而，由于这样做是完全合法的，所以不会从编译器或运行期系统获得任何出错提示——只不过代码的工作就变得不正常了。</P>
<P>　　(25) 用合理的设计方案消除“伪功能”。也就是说，假若只需要创建类的一个对象，就不要提前限制自己使用应用程序，并加上一条“只生成其中一个”注释。请考虑将其封装成一个“独生子”的形式。若在主程序里有大量散乱的代码，用于创建自己的对象，请考虑采纳一种创造性的方案，将些代码封装起来。</P>
<P>　　(26) 警惕“分析瘫痪”。请记住，无论如何都要提前了解整个项目的状况，再去考察其中的细节。由于把握了全局，可快速认识自己未知的一些因素，防止在考察细节的时候陷入“死逻辑”中。</P>
<P>　　(27) 警惕“过早优化”。首先让它运行起来，再考虑变得更快——但只有在自己必须这样做、而且经证实在某部分代码中的确存在一个性能瓶颈的时候，才应进行优化。除非用专门的工具分析瓶颈，否则很有可能是在浪费自己的时间。性能提升的隐含代价是自己的代码变得难于理解，而且难于维护。</P>
<P>　　(28) 请记住，阅读代码的时间比写代码的时间多得多。思路清晰的设计可获得易于理解的程序，但注释、细致的解释以及一些示例往往具有不可估量的价值。无论对你自己，还是对后来的人，它们都是相当重要的。如对此仍有怀疑，那么请试想自己试图从联机Java文档里找出有用信息时碰到的挫折，这样或许能将你说服。</P>
<P>　　(29) 如认为自己已进行了良好的分析、设计或者实施，那么请稍微更换一下思维角度。试试邀请一些外来人士——并不一定是专家，但可以是来自本公司其他部门的人。请他们用完全新鲜的眼光考察你的工作，看看是否能找出你一度熟视无睹的问题。采取这种方式，往往能在最适合修改的阶段找出一些关键性的问题，避免产品发行后再解决问题而造成的金钱及精力方面的损失。</P>
<P>　　(30) 良好的设计能带来最大的回报。简言之，对于一个特定的问题，通常会花较长的时间才能找到一种最恰当的解决方案。但一旦找到了正确的方法，以后的工作就轻松多了，再也不用经历数小时、数天或者数月的痛苦挣扎。我们的努力工作会带来最大的回报(甚至无可估量)。而且由于自己倾注了大量心血，最终获得一个出色的设计方案，成功的快感也是令人心动的。坚持抵制草草完工的诱惑——那样做往往得不偿失。<BR></P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/7047054320085119658950</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/7047054320085119658950</guid>
    <pubDate>Wed, 11 Jun 2008 09:06:58 +0800</pubDate>
    <dcterms:modified>2008-06-11T09:12:12+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[在Java中处理日志记录]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/704705432008532641580</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P>在JDK1.4中提供了一个日志记录包：java.util.logging，它可以对程序中的日志记录进行相当复杂的控制。例如：通过它可以指定日志的级别和日志的位置(控制台、文件、套接字、内存缓冲区)，也可以创建子记录器，通过它可以用程序控制的方式来指定想记录的内容，也可以使用配置文件来指定，而不需要去改动程序。和Log4j相比，它更简单更轻量级，在对日志输出的要求不是很复杂时，它无疑是更好的选择。</P>
<P>首先我们获得一个LogManager类的一个实例：</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>LogManager lMgr = LogManager.getLogManager(); </P>
<P>&nbsp;</P>
<P><BR>然后我们创建记录器，并且把它添加到当前的管理器：</P>
<P><BR>String thisName = “Logpkg”;</P>
<P>Logger log = Logger.getLogger(thisName);</P>
<P>lMgr.addLogger(log);</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>如果我们没有指定日志文件存放的位置，则按照jre/lib目录下logging.properties文件中指定的内容，默认是ConsoleHandler意味着日志信息在控制台显示。</P>
<P>在程序中我们可以按照级别发布日志信息，共有7个级别：SERVER(最高值)，WARNING，INFO，CONFIG，FINE，FINER，FINEST(最低值)，和OFF(不记录)。</P>
<P><BR>log.server(“error”); </P>
<P>&nbsp;</P>
<P><BR>我们可以设置记录器的记录级别，以忽略低于WARNING级别的消息，只有用server和warning记录的信息才能输出。</P>
<P><BR>log.setLevel(Level.WARNING);</P>
<P>log.info(“This message is info”);//这条信息会被忽略，不会被输出</P>
<P>log.warning(“This message is warning”);//这条信息会输出</P>
<P>&nbsp;</P>
<P>Logger的名字可以是简单的文字，也可以是逗号分隔的复合名字，一个逗号左边名字相同但是逗号右边具有附加名字的记录器是原型记录器的子记录器，例如一个叫Logpkg的记录器和一个叫Logpkg.Test1的记录器。记录器和它的子记录器可以是不同的级别，在整个程序中可以只记录SERVER信息，对于一个可疑的部分，则可以记录其他更多的事件。</P>
<P>如果想要同时把日志输出到文件和控制台，可以创建一个FileHandler并把它添加到记录器。</P>
<P><BR>fh = new FileHandler(“runtime.log”);</P>
<P>log.addHandler(fh);</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>如果没有特别指定，文件格式默认的是XML。</P>
<P>通过配置文件来控制日志记录：</P>
<P><BR>FileInputStream fi = new FileInputStream(new File(logging.properties));</P>
<P>lMgr.readConfiguration(fi);</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>这样做的好处是不需要改变或重新编译程序，就可以改变日志记录的状态。</P>
<P>配置文件的格式：</P>
<P><BR>handlers = java.util.logging.FileHandler,java.util.logging.ConsoleHandler</P>
<P>.Level = INFO</P>
<P>java.util.logging.ConsoleHandler.pattern = runtime.log</P>
<P>java.util.logging.ConsoleHandler.limit = 50000</P>
<P>java.util.logging.ConsoleHandler.count = 2</P>
<P>java.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter</P>
<P>java.util.logging.ConsoleHandler.level = WARNING</P>
<P>java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter<BR></P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/704705432008532641580</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/704705432008532641580</guid>
    <pubDate>Tue, 3 Jun 2008 14:06:41 +0800</pubDate>
    <dcterms:modified>2008-06-03T14:07:43+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[“网络蚂蚁”的Java实现]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/704705432008532111965</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT></P>
<P>“网络蚂蚁”、Falshget 等许多多线程下载软件都是网友的必备工具，利用这些工具可以快速从服务器上下载比较大的文件，这些工具的工作特性是把服务器端的文件分成几个段，每个段分别、同时进行下载。编写这类程序，第一、必须对HTTP协议有较为充分的了解；第二、有效使用多线程编程手段在软件上实现。 </P>
<P>HTTP 协议的简介 </P>
<P>HTTP协议是一种超文本传输协议（Hypertext Transfer Protocol），工作于网络应用层，自１９９０年起广泛应用于WWW 的全球信息服务，HTTP协议的详细说明可以在网上查阅RFC2518、RFC2616等文档。 </P>
<P>HTTP 协议老的标准是HTTP/1.0，目前最通用的标准是HTTP/1.1。HTTP/1.1是在HTTP/1.0基础上的升级，增加了一些功能，全面兼容HTTP/1.0。HTTP/1.0不支持文件断点续传，如果服务器使用HTTP/1.0，“网络蚂蚁”的任何多线程下载程序都只能按单线程下载；好在目前的Web服务器绝大多数都采用了HTTP/1.1，所以，下面将基于HTTP/1.1进行介绍。 </P>
<P>HTTP协议的相关重要命令 </P>
<P>基于HTTP的浏览器浏览网页、下载文件时，工作原理类似客户机/服务器模式：浏览器向Web服务器发出一个HTTP请求行；Web服务器在收到有效的请求后，返回一个状态行或多个响应标题、一个空白行和相关文档。根据这一工作原理，下载程序必须实现向服务器发送请求和获取服务器响应状态的功能。 </P>
<P>1.向服务器发送 GET请求命令 </P>
<P>一个HTTP请求由一个请求行、可选数目的请求标题、一个空白行，以及在POST情况下的一些额外的数据组成。请求行的格式是： </P>
<P>请求方法　URI　 HTTP/版本号 </P>
<P>GET 命令是浏览器常用的文档请求方法，在程序中间使用 </P>
<P>GET　URI　 HTTP/1.1 </P>
<P>向Web服务器发送请求行（行号3），Java 代码如下： </P>
<P>．．．． </P>
<P>clientSocket = new Socket(host, port);//打开要下载文件服务器的Socket </P>
<P>outStream = new PrintStream(clientSocket.getOutputStream()); </P>
<P>．．．． </P>
<P>outStream.println(“GET”+uri+“ HTTP/1.1”); </P>
<P>outStream.println(“Host:”+host); </P>
<P>outStream.println(“Accept:*/* ”); </P>
<P>outStream.println(“Referer:”); </P>
<P>outStream.println(); </P>
<P>．．．． </P>
<P>注：第4行给出URL中的主机名和端口号， </P>
<P>第5行说明客户端接收所有MIME类型， </P>
<P>第7行方送一个空白行，表明请求行结束。 </P>
<P>2.获取服务器响应状态 </P>
<P>在发送HTTP请求行以后，程序就可以读取服务器的响应状态了。HTTP响应状态行包括：HTTP 状态码和一些HTTP响应标题。 </P>
<P>1) HTTP状态码 </P>
<P>HTTP状态码格式是 HTTP/版本信息的数字表示。状态码例子如下： </P>
<P>HTTP/1.0 200 OK // 表示服务器支持HTTP/1.0 协议，成功 </P>
<P>HTTP/1.1 200 OK // 表示服务器支持HTTP/1.1 协议，成功 </P>
<P>HTTP/1.0 404 Not Found // 表示服务器支持HTTP/1.0 协议，访问文件没有找到 </P>
<P>在程序中间，如果读到“HTTP/1.1 200 OK”这样的字符串，表明欲下载文件存在、该服务器支持断点续传，可以使用多线程下载。如果读到“HTTP/1.0 200 OK”这样的字符串，表明欲下载文件存在、但该服务器不支持断点续传，只可以使用单线程下载。 </P>
<P>．．．．． </P>
<P>while ((line=inStream.readLine()) != null)　//将服务器响应状态读到line </P>
<P>．．．．．．．． </P>
<P>if(line.substring(0,7).equals(“HTTP/1.”) )　//判断是否支持HTTP/1.1 </P>
<P>{ if(line.charAt(7)==‘0’) </P>
<P>{ </P>
<P>System.out.println(“server use http/1.0”); </P>
<P>threadcount=1; </P>
<P>} </P>
<P>if(!(line.substring(9,12)).equals(“200”)) //判断请求是否成功 </P>
<P>{ System.out.println(“ERROR:”+line); </P>
<P>return false; </P>
<P>} </P>
<P>} </P>
<P>2) 读取重要的响应标题，获得要下载文档的文件长度 </P>
<P>如果HTTP状态码表明访问成功，服务器会回送一些标题行，我们最关注的是Content-Length　这一行，比如，如果服务器回送“Content-Length:1000”，表明请求文件的长度是１０００字节，所以读取这一行信息，可以得到文件的长度信息： </P>
<P>．．．． </P>
<P>if(line.substring(0,15).equals(“Content-Length:”) ) </P>
<P>{ filelength=Long.parseLong(line.substring(15).trim()); </P>
<P>System.out.println(“file length:” +filelength); </P>
<P>} </P>
<P>．．．．．． </P>
<P>向服务器发送断点续传请求 </P>
<P>如上所述，如果服务器支持HTTP/1.1，再次向服务器发送GET请求： </P>
<P>．．．．． </P>
<P>outStream.println(“GET ”+uri+“HTTP/1.1 ”); </P>
<P>outStream.println(“Host:”+host); </P>
<P>outStream.println(“Accept:*/* ”); </P>
<P>outStream.println(“RANGE:bytes=”+(fileblocklength)*thisthreadid+“-”); </P>
<P>outStream.println(); </P>
<P>．．．．． </P>
<P>第４行是关键，“RANGE:bytes=”是HTTP/1.1新增内容，HTTP/1.0每次传送文件都是从文件头开始，即０字节处开始，“RANGE:bytes=XXXX”表示要求服务器从文件XXXX字节处开始传送，这就是我们平时所说的断点续传！ </P>
<P>分割文件，多线程下载 </P>
<P>使用多线程编程技术，同时启动多个线程，根据线程个数，计算文件分割位置，向服务器发送几个不同的下载断点，同时接受数据并写入文件，就可以实现多线程下载了。 </P>
<P>..... </P>
<P>raf=new RandomAccessFile(file,“rw”);//以随机存取方式打开文件 </P>
<P>..... </P>
<P>synchronized(raf) //按同步方式把各个线程得到数据分别写入文件 </P>
<P>{ raf.seek(thisthreadid*(filelength/threadcount)+k*buflength); </P>
<P>raf.write(readbytes); </P>
<P>...... </P>
<P>} </P>
<P>...... </P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/704705432008532111965</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/704705432008532111965</guid>
    <pubDate>Tue, 3 Jun 2008 14:01:11 +0800</pubDate>
    <dcterms:modified>2008-06-03T14:08:39+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[jar打包详解]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/70470543200842903014536</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT>j</P>
<P>ar文件听说过吗，没有？或者陌生！好，没关系，这就是我们的第一站：打包发布。　　 </P>
<P>　　为什么会有这个玩意呢，首先，这是jar的全称：JavaTM Archive (JAR) file，是的，就是java存档文件。这有点类似zip文件，想一想它是干什么的用的呢，压缩！？没错就是要压缩，将我们原先零散的东西放到一下，重新组织，所有这些目的只有一个：方便！好了，不用管他是怎么压缩的，我们的重点是哪些是我们要压缩的(输入)，还有压缩成了什么(输出)，进而将它发布(部署)。 　　 </P>
<P>　　那我们的输入(要压缩的东西)主要是class文件，还有辅助的资源(这其中可能有图片，jsp文件，html文件等等)。Jar技术在jdk1.1版本中就已存在，在1.2中又有了增强。接下来说说jar的好处吧，这是官方的描述：安全，快速下载，压缩，猎取包，版本化包，可携。 　 </P>
<P>　　说了这么多，我们现在开始实施。 　　 </P>
<P>　　先打开命令提示符(win2000或在运行框里执行cmd命令，win98为DOS提示符)，输入jar Chelp,然后回车(如果你盘上已经有了jdk1.1或以上版本)，看到什么： 　　 </P>
<P>　　用法：jar {ctxu}[vfm0Mi] [jar-文件] [manifest-文件] [-C 目录] 文件名 ... 　　 </P>
<P>　　选项： 　　 </P>
<P>　　-c 创建新的存档 </P>
<P>　　-t 列出存档内容的列表 </P>
<P>　　-x 展开存档中的命名的（或所有的〕文件 </P>
<P>　　-u 更新已存在的存档 </P>
<P>　　-v 生成详细输出到标准输出上 </P>
<P>　　-f 指定存档文件名 </P>
<P>　　-m 包含来自标明文件的标明信息 </P>
<P>　　-0 只存储方式；未用zip压缩格式 </P>
<P>　　-M 不产生所有项的清单（manifest〕文件 </P>
<P>　　-i 为指定的jar文件产生索引信息 </P>
<P>　　-C 改变到指定的目录，并且包含下列文件：　　 </P>
<P>　　如果一个文件名是一个目录，它将被递归处理。 　　 </P>
<P>　　清单（manifest〕文件名和存档文件名都需要被指定，按'm' 和 'f'标志指定的相同顺序。 　　 </P>
<P>　　示例1：将两个class文件存档到一个名为 'classes.jar' 的存档文件中：<BR>　 jar cvf classes.jar Foo.class Bar.class 　 <BR>　 </P>
<P>　　示例2：用一个存在的清单（manifest）文件 'mymanifest' 将 foo/ 目录下的所有文件存档到一个名为 'classes.jar' 的存档文件中： <BR></P>
<P>　　jar cvfm classes.jar mymanifest -C foo/ . 　 <BR>　 </P>
<P>　　来个小例子试试看： </P>
<P>　　我们只有一个HelloWorld，如下： 　　 <BR></P>
<P>　　public class HelloWorld{ <BR>　　 public static void main(String[ ] args){ <BR>　　 System.out.println(“Hi, Hello World!”); <BR>　　 } <BR>　　 } <BR>　　 <BR>　　 将这个java文件存到C盘跟目录下，ok，接下来， 　　 </P>
<P>　　在先前打开的命令提示符下(跳转到C盘提示符下)，我们输入javac HelloWorld.java，然后继续输入：jar cvf hello.jar HelloWorld.class，回车后去你的C盘看看，多了什么，没错 hello.jar 。 </P>
<P>　　基本的步骤我们现在都知道了，你可以自己去尝试一下随着jar后面的参数的不同，结果有什么变化。 　　 <BR>　　 紧接着我们看看如何运行我们的jar包。 　　 </P>
<P>　　在进入正题之前，你要先打开我们刚刚做好的jar包看看，多了什么呢，META-INF目录？再看看里面是什么，还有一个MANIFEST.MF文件是不是？用文本编辑器(我这里是UltraEdit)打开它看看： <BR></P>
<P>　　Manifest-Version: 1.0 <BR>　　 Created-By: 1.4.2 (Sun Microsystems Inc.) 　 <BR>　 </P>
<P>　　就是这样。这里我们对它进行修改，加一句：Main-Class: HelloWorld (在第三行)。这个就是我们之前写的那个类，也就是我们的入口类。也即， <BR></P>
<P>　　Manifest-Version: 1.0 <BR>　　 Created-By: 1.4.2 (Sun Microsystems Inc.) <BR>　　 Main-Class: HelloWorld 　 <BR></P>
<P>　　接下来，我们在命令提示符里执行： <BR></P>
<P>　　jar umf MANIFEST.MF app.jar 　(应该是hello.jar吧)　 <BR></P>
<P>　　这样我们使用了我们自己的MANIFEST.MF文件对原来默认的进行了更新。你不妨可以再进去看看是不是添上了Main-Class: HelloWorld这一句。 　　 (是吗，我怎么没试出来，提示java.io.FileNotFoundException:MANIFEST.MF（系统找不到指定的文件）怎么回事？)</P>
<P>Ok，这个最后的一步了，来验证我们做的一切，在命令提示符中输入： <BR></P>
<P>　　java -jar hello.jar(执行) 　 <BR>　 </P>
<P>　　出现了什么， Hi, Hello World! 　 </P>
<P>　　我们再来看看jar文件在tomcat中发布，注意：在tomcat中我们就不能再用jar这种格式，而改war格式，它是专门用于web应用的，其实整个过程下来基本上和jar是类似的：　 </P>
<P>　　先准备我们要打包的资源。 　　 </P>
<P>　　找到存放tomcat的webapps目录，进到其中，新建一个文件夹，这里命名为hello，再进去新建WEB-INF文件夹，再进去新建classes文件夹，此时我们也将我们唯一的servlet，HelloWorld.java放到这里，在与classes目录同级下建立一文件web.xml。Ok，目前我们初步建立了一个简单的web应用。　　 </P>
<P>　　这是HelloWorld.java： 　　 <BR></P>
<P>　　import java.io.*; <BR>　　 import javax.servlet.*; <BR>　　 import javax.servlet.http.*; <BR>　　 public class HelloWorld extends HttpServlet { <BR>　　 public void doGet(HttpServletRequest req, HttpServletResponse res) <BR>　　 throws ServletException, IOException { <BR>　　 res.setContentType("text/html"); <BR>　　 PrintWriter out = res.getWriter(); <BR>　　 out.println(""); <BR>　　 out.println(""); <BR>　　 out.println(""); <BR>　　 out.println("Hello, World!"); <BR>　　 out.println(""); <BR>　　 } <BR>　　 }//end here!　　 <BR>　　 对它编译。下面是web.xml：<BR>　　 &lt;?xml version="1.0" encoding="UTF-8"?&gt;<BR>　　 &lt;!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"&gt;<BR>　　 &lt;web-app&gt;<BR>　　 &lt;servlet&gt;<BR>　　 &lt;servlet-name&gt;hello&lt;/servlet-name&gt;<BR>　　 &lt;servlet-class&gt;HelloWorld&lt;/servlet-class&gt;<BR>　　 &lt;/servlet&gt;<BR>　　 &lt;servlet-mapping&gt;<BR>　　 &lt;servlet-name&gt;hello&lt;/servlet-name&gt;<BR>　　 &lt;url-pattern&gt;/HelloWorld&lt;/url-pattern&gt;<BR>　　 &lt;/servlet-mapping&gt;<BR>　　 &lt;/web-app&gt;<BR></P>
<P>在命令提示符下进到先前创制的hello目录下，执行 jar cvf hello.war * ，我们便得到hello.war。将它拷贝至webapps目录下，ok，来看最后一步，打开tomcat的目录conf中的server.xml，加入：</P>
<P>　　&lt;Context path="/hello" docBase="hello.war" debug="0" reloadable="true"/&gt; </P>
<P>　　大功告成！运行它，启动tomcat，后在浏览器中输入http://localhost:8080/hello/HelloWorld，有了吗？　</P>
<P>　　最后，如果你想用ant来完成以上的打包活动，下面就告诉你：<BR>　　 对于jar来说。在build.xml中， </P>
<P>　　&lt;target name="jar"&gt;<BR>　　 &lt;jar destfile="${app_home}/hello.jar"&gt;<BR>　　 &lt;fileset dir="${dest}" includes="**"/&gt;<BR>　　 &lt;!--fileset dir="${dest}" includes="**/action.properties"/--&gt;<BR>　　 &lt;/jar&gt;<BR>　　 &lt;/target&gt;　　<BR><BR>　　 对于war， 　　<BR></P>
<P>　　&lt;war warfile="hello.war" webxml="./WEB-INF/web.xml"&gt;<BR>　　 &lt;fileset dir="html"/&gt;<BR>　　 &lt;lib dir="lib/"&gt;<BR>　　 &lt;exclude name="oracle*.jar"/&gt;<BR>　　 &lt;/lib&gt;<BR>　　 &lt;classes dir="build/servlets"&gt;<BR>　　 &lt;include name="**/*.class"/&gt;<BR>　　 &lt;/classes&gt;<BR>　　 &lt;/war&gt;　　<BR><BR>　　 好了，就这么多，希望对你有点帮助。：） </P>
<P>　　补充：　 </P>
<P>　　jar基本操作： 　　 </P>
<P>　　1. 创建jar文件 　 <BR></P>
<P>　　jar cf jar-file input-file(s) <BR>　　 c---want to Create a JAR file. <BR>　　 f---want the output to go to a file rather than to stdout. <BR>　　 eg: 1)jar cf myjar.jar query_maintain_insert.htm <BR>　　 2)jar cvf myjar.jar query_maintain_insert.htm <BR>　　 v---Produces verbose(详细的) output. </P>
<P>　3)jar cvf myjar.jar query_maintain_insert.htm mydirectory <BR>　　 4)jar cv0f myjar.jar query_maintain_insert.htm mydirectory <BR>　　 0---don't want the JAR file to be compressed. <BR>　　 5)jar cmf MANIFEST.MF myjar.jar yahh.txt <BR>　　 m---Used to include manifest information from an existing manifest file. <BR>　　 6)jar cMf MANIFEST.MF myjar.jar yahh.txt <BR>　　 M---the default manifest file should not be produced. <BR>　　 7)jar cvf myjar.jar * <BR>　　 *---create all contents in current directory.　 <BR>　 </P>
<P>　　2. 察看jar文件 　　 <BR></P>
<P>　　jar tf jar-file <BR>　　 t---want to view the Table of contents of the JAR file. <BR>　　 eg: 1)jar vft yahh.jar <BR>　　 v---Produces verbose(详细的) output. 　 <BR></P>
<P>　　3. 提取jar文件 　 <BR></P>
<P>　　jar xf jar-file [archived-file(s)] <BR>　　 x---want to extract files from the JAR archive. <BR>　　 eg: 1)jar xf yahh.jar yahh.txt(仅提取文件yahh.txt) <BR>　　 2)jar xf yahh.jar alex/yahhalex.txt(仅提取目录alex下的文件yahhalex.txt) 　 </P>
<P>　　3)jar xf yahh.jar(提取该jar包中的所有文件或目录) 　<BR><BR>　 </P>
<P>　　4. 修改Manifest文件 　 <BR></P>
<P>　　jar cmf manifest-addition jar-file input-file(s) <BR>　　 m---Used to include manifest information from an existing manifest file. 　 <BR></P>
<P>　　5. 更新jar文件 　 <BR></P>
<P>　　jar uf jar-file input-file(s) <BR>　　 u---want to update an existing JAR file. </P>
<P></P>
<P>　　比如如果表中有三列id, name, address,按条件查询得到的结果放到ResultSet中，显示结果时如果这样写代码：</P>
<P>&nbsp;</P>
<P>　　java.sql.ResultSet employees;<BR>　　 while(employees.next()){<BR>　　 employees.getString("address");<BR>　　 employees.getString("name");<BR>　　 } <BR></P>
<P>　　在运行的时候将会"column can not be re-read"错误，此时把两条语句颠倒就好了（此条只适用于连接MS SQL Server 2000）</P>
<P>　　连接数据库代码如下：<BR></P>
<P>　　try {<BR>　　 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");<BR>　　 conn =<BR>　　 DriverManager.getConnection(<BR>　　"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Hotel;User=sa;Password=sa");<BR>　　 // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<BR>　　 // conn = DriverManager.getConnection("jdbc:odbc:hotel");<BR>　　 } catch (SQLException e) {<BR>　　 System.err.println(e);<BR>　　 } catch (ClassNotFoundException e) {<BR>　　 System.err.println(e);<BR>　　 } </P>
<P>&nbsp;</P></div>]]></description>
	    <author><![CDATA[luotingzhang]]></author>
	    <comments>http://luotingzhang.blog.163.com/blog/static/70470543200842903014536</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://luotingzhang.blog.163.com/blog/static/70470543200842903014536</guid>
    <pubDate>Thu, 29 May 2008 12:30:14 +0800</pubDate>
    <dcterms:modified>2008-05-29T12:30:46+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[怎么学习Java ]]></title>	
    <link>http://luotingzhang.blog.163.com/blog/static/70470543200842901127651</link>
    <description><![CDATA[<div><P><FONT size=4>成都游戏学院 &lt;</FONT><A href="http://www.cdgamecollege.org/"><FONT face=Arial color=#0c4468 size=4>http://www.cdgamecollege.org</FONT></A><FONT face=Arial size=4>&gt; 电话：028-85586115</FONT>j</P>
<P>ar文件听说过吗，没有？或者陌生！好，没关系，这就是我们的第一站：打包发布。　　 </P>
<P>　　为什么会有这个玩意呢，首先，这是jar的全称：JavaTM Archive (JAR) file，是的，就是java存档文件。这有点类似zip文件，想一想它是干什么的用的呢，压缩！？没错就是要压缩，将我们原先零散的东西放到一下，重新组织，所有这些目的只有一个：方便！好了，不用管他是怎么压缩的，我们的重点是哪些是我们要压缩的(输入)，还有压缩成了什么(输出)，进而将它发布(部署)。 　　 </P>
<P>　　那我们的输入(要压缩的东西)主要是class文件，还有辅助的资源(这其中可能有图片，jsp文件，html文件等等)。Jar技术在jdk1.1版本中就已存在，在1.2中又有了增强。接下来说说jar的好处吧，这是官方的描述：安全，快速下载，压缩，猎取包，版本化包，可携。 　 </P>
<P>　　说了这么多，我们现在开始实施。 　　 </P>
<P>　　先打开命令提示符(win2000或在运行框里执行cmd命令，win98为DOS提示符)，输入jar Chelp,然后回车(如果你盘上已经有了jdk1.1或以上版本)，看到什么： 　　 </P>
<P>　　用法：jar {ctxu}[vfm0Mi] [jar-文件] [manifest-文件] [-C 目录] 文件名 ... 　　 </P>
<P>　　选项： 　　 </P>
<P>　　-c 创建新的存档 </P>
<P>　　-t 列出存档内容的列表 </P>
<P>　　-x 展开存档中的命名的（或所有的〕文件 </P>
<P>　　-u 更新已存在的存档 </P>
<P>　　-v 生成详细输出到标准输出上 </P>
<P>　　-f 指定存档文件名 </P>
<P>　　-m 包含来自标明文件的标明信息 </P>
<P>　　-0 只存储方式；未用zip压缩格式 </P>
<P>　　-M 不产生所有项的清单（manifest〕文件 </P>
<P>　　-i 为指定的jar文件产生索引信息 </P>
<P>　　-C 改变到指定的目录，并且包含下列文件：　　 </P>
<P>　　如果一个文件名是一个目录，它将被递归处理。 　　 </P>
<P>　　清单（manifest〕文件名和存档文件名都需要被指定，按'm' 和 'f'标志指定的相同顺序。 　　 </P>
<P>　　示例1：将两个class文件存档到一个名为 'classes.jar' 的存档文件中：<BR>　 jar cvf classes.jar Foo.class Bar.class 　 <BR>　 </P>
<P>　　示例2：用一个存在的清单（manifest）文件 'mymanifest' 将 foo/ 目录下的所有文件存档到一个名为 'classes.jar' 的存档文件中： <BR></P>
<P>　　jar cvfm classes.jar mymanifest -C foo/ . 　 <BR>　 </P>
<P>　　来个小例子试试看： </P>
<P>　　我们只有一个HelloWorld，如下： 　　 <BR></P>