⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j2me中使用canvas制作简单的游戏菜单-开发者网络-j2me-天极yesky.htm

📁 j2me的一些东西。有用的东东
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0038)http://dev.yesky.com/284/2464784.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky</TITLE>
<META content=J2ME中使用Canvas制作简单的游戏菜单, name=description>
<META content=J2ME中使用Canvas制作简单的游戏菜单, name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all 
href="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/y_mobile_content.css" 
type=text/css rel=stylesheet>
<SCRIPT 
src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/showlay.js"></SCRIPT>

<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A 
href="http://product.yesky.com/">产品报价</A>| <A 
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A> 
| <A href="http://digital.yesky.com/">数码</A> | <A 
href="http://notebook.yesky.com/">笔记本</A> | <A 
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A> 
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A> 
| <A href="http://dh.yesky.com/">数字家庭</A> | <A 
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> | 
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A> 
| <A href="http://pic.yesky.com/">图片</A> | <A 
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> | 
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A> 
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户 
src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/logo.gif" 
border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get 
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select() 
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden 
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A 
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广东</A> <A 
href="http://xa.yesky.com/">西安</A><BR><A href="http://cq.yesky.com/">重庆</A> <A 
href="http://cd.yesky.com/">成都</A> <A href="http://sy.yesky.com/">沈阳</A> <A 
href="http://js.yesky.com/">江苏</A> </DIV>
<DIV class=rightarea_right><A 
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/content_text_ad.js" 
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/flash.js" 
type=text/javascript></SCRIPT>

<DIV id=childmenu><A href="http://soft.yesky.com/">软件频道&gt;</A><A 
href="http://dev.yesky.com/">程序开发&gt;</A><A 
href="http://dev.yesky.com/devjava/">Java</A><A 
href="http://dev.yesky.com/ysdevvb">VB</A><A 
href="http://dev.yesky.com/ysdevvc">VC</A><A 
href="http://dev.yesky.com/ysdevdelphi">Delphi</A><A 
href="http://dev.yesky.com/ysdevc">C/C++</A><A 
href="http://dev.yesky.com/web/">Web开发</A><A 
href="http://dev.yesky.com/msdn/">微软专栏</A><A 
href="http://dev.yesky.com/devydkf/">移动</A><A 
href="http://dev.yesky.com/devsjk/">数据库</A><A 
href="http://dev.yesky.com/ysdevcxrs">程序人生</A><A 
href="http://dev.yesky.com/ysdevrjgc">软件工程</A>|<A 
href="http://soft.yesky.com/lesson/">产品中心</A><A 
href="http://www.mydown.com/soft/develop/news.shtml">下载</A><A 
href="http://q.yesky.com/group/179"><FONT 
color=#ff0000>群乐:大家耍QQ</FONT></A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/">天极Yesky</A> &gt; <A 
href="http://soft.yesky.com/">软件频道</A> &gt; <A 
href="http://dev.yesky.com/">开发者网络</A> &gt; J2ME中使用Canvas制作简单的游戏菜单</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://dev.yesky.com/284/2464784.shtml#pls" 
target=_self>评论</A></DIV>
<DIV class=kr></DIV>
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
</DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>J2ME中使用Canvas制作简单的游戏菜单</H1></DIV>
<DIV id=conauthor><SPAN>2006-06-29 07:00 </SPAN><SPAN>作者: </SPAN><SPAN>出处: 
j2me开发网 </SPAN><SPAN>责任编辑:<A title=向本编辑提问 
href="http://comments.yesky.com/t/·&frac12;&Ouml;&Ucirc;/6,324/2464784.shtml" target=_blank>方舟</A> 
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN 
id=contentAdv></SPAN></DIV>  我们知道MIDP的图形用户界面分为两类,分别是高级图形用户界面和低级用户界面。一般来讲高级图形用户界面类使用起来比较方便,可移植性强,但是程序员对他的控制能力也很低,因为它们的界面表现是由底层控制的,而不是我们控制的。相比高级UI类,低级UI类则使用起来更难一些,但是控制能力更强,可以做出自己需要的界面。 
<BR><BR>  Canvas和Graphics是我们必须熟练使用的两个类,分别代表了画布和画笔(事实上更丰富,姑且这么比喻)。而我们则是画画的人,而指导我们如何下笔的就是java 
doc了,再加上勤奋努力一定可以画出不错的界面。比如tabbed菜单,二级菜单等。这里我们讲述一个简单菜单的制作方式。<BR><BR>  在画菜单的时候,需要考虑两面的问题,第一是计算相对位置,让菜单能够尽可能适应更多的机型,尽量少使用绝对值。例如画下面的菜单的时候<BR><BR>   
我们应该计算菜单的每个条目的高度,这些可以有Font的高度算出,当然你可以给条目留一些padding的距离。还应该计算条目的最宽值,毕竟每个条目的字数不一样。这样基本知道了整个菜单占的空间。最后还需要计算菜单在屏幕的位置。菜单的绘制如下所示:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e1e1e1 border=1>
  <TBODY>
  <TR>
    <TD>public void paint(Graphics g){<BR> //清除屏幕<BR> int color = 
      g.getColor();<BR> g.setColor(0xFFFFFF);<BR> g.fillRect(0,0,getWidth(),getHeight());<BR> g.setColor(color);<BR> //计算整个菜单的高度,宽度和(x,y)<BR> int 
      rectWidth = preferWidth;<BR> int rectHeight = preferHeight * 
      LABELS.length;<BR> int x = (getWidth()-rectWidth)/2;<BR> int y = 
      (getHeight()-rectHeight)/2;<BR> //画矩形<BR> g.drawRect(x,y,rectWidth,rectHeight);<BR> for(int 
      i = 
      1;i&lt;LABELS.length;i++){<BR>  g.drawLine(x,y+preferHeight*i,x+rectWidth,y+preferHeight*i);<BR> }<BR> //画菜单选项,并根据selected的值判断焦点<BR> for(int 
      j = 0;j&lt;LABELS.length;j++){<BR>  if(selected == 
      j){<BR>   g.setColor(0x6699cc);<BR>   g.fillRect(x+1,y+j*preferHeight+1,rectWidth-1,preferHeight-1);<BR>   g.setColor(color);<BR>  }<BR>  g.drawString(LABELS[j],x+8,y+j*preferHeight+4,Graphics.LEFT|Graphics.TOP);<BR> }<BR>}</TD></TR></TBODY></TABLE><BR>  第二个重要的问题是:焦点的切换,在高级UI类中,这是不需要我们处理的。但是使用Canvas制作菜单需要自己来处理焦点的移动,这里我们定义一个int类型变量selected,来记录焦点所在的菜单条目位置,也就是选择的索引。当用户按键的时候,我们在keyPressed()方法中判断用户的移动方向,对selected进行相关的加减运算,然后repaint()整个屏幕即可。<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e1e1e1 border=1>
  <TBODY>
  <TR>
    <TD>public void keyPressed(int 
      keyCode){<BR> //根据用户输入更新selected的值,并重新绘制屏幕<BR> int action = 
      this.getGameAction(keyCode);<BR> switch(action){<BR>  case 
      Canvas.FIRE:<BR>   printLabel(selected);<BR>   break;<BR>  case 
      Canvas.DOWN:<BR>   selected = (selected+1)%4;<BR>   break;<BR>  case 
      Canvas.UP:{<BR>   if(--selected &lt; 
      0){<BR>    selected+=4;<BR>   }<BR>   break;<BR>  }<BR>  default:<BR>   break;<BR> }<BR> repaint();<BR> serviceRepaints();<BR>}</TD></TR></TBODY></TABLE><BR>  这样我们就制作出了一个基本的菜单,你还可以发挥想象给被选中的菜单增加动画效果。MenuCanvas的代码如下所示:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e1e1e1 border=1>
  <TBODY>
  <TR>
    <TD>package com.j2medev.chapter3;<BR><BR>import 
      javax.microedition.lcdui.*;<BR><BR>public class MenuCanvas extends 
      Canvas{<BR><BR> //selected变量标记了焦点位置<BR> private int selected = 
      0;<BR> private int preferWidth = -1;<BR> private int preferHeight = 
      -1;<BR> public static final int[] OPTIONS = {0,1,2,3};<BR> public static 
      final String[] LABELS={"New Game","Setttings","High 
      Scores","Exit"};<BR><BR> public MenuCanvas() {<BR>  selected = 
      OPTIONS[0];<BR>  //计算菜单选项的长度和高度值<BR>  Font f = 
      Font.getDefaultFont();<BR>  for(int i = 
      0;i&lt;LABELS.length;i++){<BR>   int temp = 
      f.stringWidth(LABELS[i]);<BR>   if(temp &gt; 
      preferWidth){<BR>    preferWidth = temp;<BR>   }<BR>  }<BR>  preferWidth = 
      preferWidth + 2*8;<BR>  preferHeight = 
      f.getHeight()+2*4;<BR> }<BR><BR> public void paint(Graphics 
      g){<BR>  //清除屏幕<BR>  int color = 
      g.getColor();<BR>  g.setColor(0xFFFFFF);<BR>  g.fillRect(0,0,getWidth(),getHeight());<BR>  g.setColor(color);<BR>  //计算整个菜单的高度,宽度和(x,y)<BR>  int 
      rectWidth = preferWidth;<BR>  int rectHeight = preferHeight * 
      LABELS.length;<BR>  int x = (getWidth()-rectWidth)/2;<BR>  int y = 
      (getHeight()-rectHeight)/2;<BR>  //画矩形<BR>  g.drawRect(x,y,rectWidth,rectHeight);<BR>  for(int 
      i = 
      1;i&lt;LABELS.length;i++){<BR>   g.drawLine(x,y+preferHeight*i,x+rectWidth,y+preferHeight*i);<BR>  }<BR>  //画菜单选项,并根据selected的值判断焦点<BR>  for(int 
      j = 0;j&lt;LABELS.length;j++){<BR>   if(selected == 
      j){<BR>    g.setColor(0x6699cc);<BR>    g.fillRect(x+1,y+j*preferHeight+1,rectWidth-1,preferHeight-1);<BR>    g.setColor(color);<BR>   }<BR>   g.drawString(LABELS[j],x+8,y+j*preferHeight+4,Graphics.LEFT|Graphics.TOP);<BR>  }<BR> }<BR><BR> public 
      void keyPressed(int keyCode){<BR>  //根据用户输入更新selected的值,并重新绘制屏幕<BR>  int 
      action = this.getGameAction(keyCode);<BR>  switch(action){<BR>   case 
      Canvas.FIRE:<BR>    printLabel(selected);<BR>    break;<BR>   case 
      Canvas.DOWN:<BR>    selected = (selected+1)%4;<BR>    break;<BR>   case 
      Canvas.UP:{<BR>    if(--selected &lt; 
      0){<BR>     selected+=4;<BR>   }<BR>   break;<BR>  }<BR>  default:<BR>  break;<BR> }<BR> repaint();<BR> serviceRepaints();<BR>}<BR>//showNotify()在paint()之前被调用<BR>public 
      void showNotify(){<BR> System.out.println("showNotify() is 
      called");<BR>}<BR>private void printLabel(int 
      selected){<BR> System.out.println(LABELS[selected]);<BR>}<BR>}</TD></TR></TBODY></TABLE><BR>
<TABLE 
style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" 
cellSpacing=0 cellPadding=9 width=500 align=center border=0>
  <TBODY>
  <TR align=middle>
    <TD colSpan=4><FONT face=黑体 size=5><B>软件频道精品推荐 </B></FONT></TD></TR>
  <TR align=middle>
    <TD width="25%"><A 
      href="http://soft.yesky.com/lesson/231/2372731.shtml"><IMG 
      src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/xl5o4ax13d32.gif" 
      border=0> </A></TD>
    <TD width="25%"><A 
      href="http://search.mydown.com/mydown/search.jsp?tag=6&amp;nameField0=NAME&amp;searchName=天极软件教程电子书&amp;sortID=504403158265495552&amp;x=18&amp;y=8"><IMG 
      src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/i9ngk6il2sg9.jpg" 
      border=0></A> </TD>
    <TD width="25%"><A 
      href="http://comments.yesky.com/t/100913/6,324/2069547.shtml"><IMG 
      src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/fih1dt6d9cxz.gif" 
      border=0></A> </TD>
    <TD><A href="http://www.yesky.com/imagesnew/software/cmap/index.html"><IMG 
      src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/5m8o30513cyt.gif" 
      border=0></A> </TD></TR>
  <TR align=middle>
    <TD><A href="http://soft.yesky.com/lesson/314/2372814.shtml"><IMG 
      src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/hn0va2pl920r.gif" 
      border=0></A> </TD>
    <TD><A href="http://soft.yesky.com/lesson/178/2440678.shtml"><IMG 
      src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/136b17cw487u.gif" 
      border=0></A></TD>
    <TD><A 
      href="http://soft.yesky.com/SoftChannel/72339069014638592/index.shtml"><IMG 
      src="J2ME中使用Canvas制作简单的游戏菜单-开发者网络-J2ME-天极Yesky.files/8mg1e7bpzjjx.jpg" 
      border=0> </A></TD>
    <TD>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -