免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1240 | 回复: 0
打印 上一主题 下一主题

Doja移植的一些心得,提供参考 [转] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-03 15:49 |只看该作者 |倒序浏览
1.Doja不支持png,只支持gif
  2.Graphics不支持setClip函数,所以我们程序中所用到的所有与setClip函数相关的东西需要做相应转换,在我们的很多程序里面,使用了从一张大图里面clip一个小图出来,在Doja中需要将这些图片直接切开存成单张图片。
 
 3.drawText和drawImage函数不支持Anchor,drawText方法使用了相当于J2ME里面的Baseline|Left锚点。
而drawImage使用了相当于J2ME里面的LEFT|TOP锚点。请各位程序员在移植时作相应的转换,最好的方法是自己封装一个方法,做一些变换。
   public static final int HCENTER = 1;
   public static final int VCENTER = 2;
   public static final int LEFT = 4;
   public static final int RIGHT = 8;
   public static final int TOP = 16;
   public static final int BOTTOM = 32;
   public void drawImage(Graphics g,Image img,int x, int y,int mode){
  switch(mode){
   case LEFT|TOP:
   g.drawImage(img,x,y);
   break;
   case HCENTER|VCENTER:
   g.drawImage(img,x - (img.getWidth()>>1),y - (img.getHeight()>>1));
   break;
   case LEFT|BOTTOM:
   g.drawImage(img,x ,y - (img.getHeight()));
   break;
   case LEFT|VCENTER:
   g.drawImage(img,x ,y - (img.getHeight()>>1));
   break;
  }
   }
  还有另外几种对齐方式,各位可以自己补齐:)
  对于MIDP中的g.drawImage(img,x,y,anchor)在Doja中就可以用
  drawImage(g,img,x,y,anchor);
  比如,g.drawImage(imgLogo,120,132,HCENTER|VCENTER),在Doja中可以替换成
  drawImage(g,imgLogo, 120, 132,HCENTER|VCENTER);
  4.Doja创建图片的方法和J2ME不一样,我已经封装的该方法:
  public Image createImage(String path){
   Image s = null;
   MediaImage m1 = null;
   m1 = MediaManager.getImage("resource:///images/" + path + ".gif");
   try {
   m1.use();
   }
   catch (ConnectionException ce) {
   ce.printStackTrace();
   }
   catch (UIException uie) {
   uie.printStackTrace();
   }
   s = m1.getImage();
   m1 = null;
   return s;
  }
  上述方法中图片是放在res的子目录images目录下,如有其它改变,请作相应变换。
  图片用完后,可以采用img.dispose();方法来销毁。
  5.Doja由于完全不支持MIDP1.0所以我们只能使用它的SDK来编译。Doja里面的JAM对应了MIDP中的JAD,具体设置可以参考Doja文档。
   其目录结构和WTK相似:
   \src:存放代码
   \res:存放各种图片、声音及其它资源文件。
   \bin:存放打包后的jam,jar文件。
  
  6. J2ME中的MIDlet在Doja中对应了IApplication,
  MIDlet IApplication
  startApp() start()
  在Doja中需要关闭游戏,可以调用terminate()方法.
  注意:在Doja中不能创建带参数的构造函数,也不要生成IApplication的实例。
  7.Doja中使用双缓冲的方法是在Canvas的paint方法中使用成对的g.lock()和g.unlock(true)方法。
  8.Doja中设置颜色的方法g.setColor(g.getColorOfRGB(00, 00, 00));
   g.setColor(colour); ==> g.setColor(g.getColorOfRGB(colour>>16&0xff,
   colour>>8&0xff,
   colour&0xff));
  9.Doja中处理按键事件是processEvent(int type,int param).type是事件类型,对于按键事件是KEY_PRESSED_EVENT,param是对应事件类型的参数,对于按键事件来说,param是keyCode.
   下面是一个按键处理的例子:
   public void processEvent(int type,int param){
   switch(gameState){
   case ST_MAINMENU:
   if(type == Display.KEY_PRESSED_EVENT){
   if(param == Display.KEY_SOFT1){
   System.out.println("Left Soft key pressed");
   }
   }
   }
   }
   对于J2ME游戏移植到Doja,为了减少这部分改动,可以采取下面的方法。
   public void processEvent(int type,int keyCode){
   if(type == Display.KEY_PRESSED_EVENT){
   {
   switch(gameState){
   case ST_MAINMENU:
   switch(keyCode){
   case Display.KEY_SOFT1:
   System.out.println("Left Soft key pressed");
   break;
   case Display.KEY_SOFT2:
   ....
   break;
   }
   break;
   case ST_RUN:
   ....
   }
   }
   }
  
  另外,在Doja中,左软和右软的label可以通过Canvas的setSoftLabel(int key,String label)来设置,在游戏中,希望大家该设置的地方,都需要设置,下面是一个例子:
  setSoftLabel(SOFT_KEY_1,"OK");
  setSoftLabe2(SOFT_KEY_2,"Cancel");
  
  10.对于操纵RMS,首先需要在JAM中设置SPSize,下面是简单的读写RMS方法,请加上错误处理。
   public void save(){
   try{
   OutputStream out = Connector.openOutputStream("scratchpad:///0");
   DataOutputStream dos = new DataOutputStream(out);
   int a = 20;
   dos.writeInt(a);
   dos.flush();
   dos.close();
   out.close();
   }catch(Exception e){}
   }
   public void read(){
   try{
   InputStream in = Connector.openInputStream("scratchpad:///0");
   DataInputStream dis = new DataInputStream(in);
   int a = dis.readInt();
   System.out.println(a);
  }catch(Exception e){
   System.out.println(e.getMessage());
   }
   }
  11.Doja版本也需要采用低级界面。
  12.请大家采用P4SDK来编译,采用P6SDK来运行。
  13.Doja中读取文件方式和MIDP一样,下面是一个例子:
   /**
   * 从一个数据文件里面读取一块出来
   * @param filePath String 数据文件名
   * @param buf byte[]数据缓冲区
   * @param offset int 偏移量
   * @param length int 读取长度
   */
  public void ReadDataFromFile(String filePath,byte[] buf ,int offset,int length){
   DataInputStream stream;
   try{
   stream = new DataInputStream(getClass().getResourceAsStream(filePath));
   stream.skipBytes(offset); //先偏移一段距离
   stream.read(buf,0,length);//读取到缓冲区里面
   stream.close() ;
   }catch(IOException e){
   stream = null;
   System.out.println("Error");
   }
  }
  14.Doja中播放音乐的方法:
  private static final int SOUND_NUMBER = 3;
  MediaSound[] msList = new MediaSound[SOUND_NUMBER];
  AudioPresenter[] apList = new AudioPresenter[SOUND_NUMBER];
  private int curSoundIndex = -1;
  
  //初始化音乐文件
   private void initSound(int index){
   try{
   apList[index] = AudioPresenter.getAudioPresenter();
   msList[index] = MediaManager.getSound("resource:///images/" + index + ".mid");
   msList[index].use();
   apList[index].setSound(msList[index]);
   }catch(Exception e){
   System.out.println("init error");
   System.out.println(e.getMessage());
   }
   }
   //播放音乐
   public void playSound(int index){
   if(!bSound)
   return;
   if(index != curSoundIndex){
   disposeSound(curSoundIndex);
   System.out.println("run here1");
   }
   if(msList[index]== null || apList[index] == null){
   System.out.println("run here2");
   initSound(index);
   System.out.println("run here3");
   }
  
   apList[index].play();
   }
   //销毁音乐资源
   private void disposeSound(int index){
   if(index = SOUND_NUMBER)
   return;
   if(apList[index] != null){
   apList[index].stop();
   apList[index] = null;
   }
   if(msList[index] != null){
   msList[index].dispose();
   msList[index] = null;
   }
   }
  15. g.setColor(g.getColorOfRGB (0x00,0x00,0x00));
   g.drawLine(0, 0, getWidth(), 0);
   set these two sentence at the beginning of the method 'paint()' can resolve the dithering of the screen


  当使用MediaManager的getImage()方法从
ScratchPad中装载大量图片候,有时候会出现OutOfMemory
的exception,或者花费很长的时间,甚至在N90X系列机型上会无法继续装载。这是可以使用Docomo api
文档中没有出现的带两个参数getImage()方法来装载,而且速度会相当快。示例:
  MediaImagemi=MediaManager.getImage("scratchpad:///0;pos="+pos+",length="+length);
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/56194/showart_1811187.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP