• 模拟器与真机的程序差别J2ME


    1. S60机器上文字需要设置如

    1. public static final Font smallFont    =   
    2. Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); 

     在需要写文字时候

    1. g.setColor(0xFFFFFF);  
    2. g.setFont(smallFont);  
    3. g.drawString(“需要写的文字”,0,0, g.TOP|g.HCENTER); 

    如果不设置字体颜色,他会自己默认一个颜色是上次设置的,字体是系统默认(大字体)。

    在QB上不需要每次写文字时候都设置文字字体,直接

    1. g.drawString(“需要写的文字”,0,0, g.TOP|g.HCENTER); 

    它的字体是上次设置的字体。(如果程序中都使用的是小字体只需要设置一次字体即可。)

    2. 模拟器中支持

    1. String str ="字符串";  
    2. str=str.intern();    //将字符对象串转化成字符串  
    3. if( str == "字符串" )   //可以返回真  
    4.  

    真机上是不支持该方法只能用

    1. if( str.equals("字符串") )   //才能返回真  


    如果也用上面的方法 得不到想要的结果的。

    3. j2me没有浮点数

    1. Role.MONEY *= 9/10; 

    结果

    1. Role.MONEY=0; 

    只能

    1. Role.MONEY = Role.MONEY*9/10; 

    4.

    1. imgRens    = Image.createImage("/pics/state.png");        //人物状态  
    2. imgState    = new Image[4];  
    3. imgWidth= 10;  
    4. for( int i=0; i<imgState.length; i++ )  
    5. {  
    6. //    imgState    = Image.createImage( imgWidth, imgRens.getHeight() );  
    7.    imgState    = DirectUtils.createImage( imgWidth, imgRens.getHeight(), 0x00000000 );  
    8.    gren        = imgState.getGraphics();  
    9.    gren.drawImage(imgRens, -imgWidth*i, 0, Graphics.TOP|Graphics.LEFT);  
    10. }  
    11. gren   =null;  
    12. imgRens=null;  
    13. System.gc(); 

    模拟器用DirectUtils建的图片时候运行出问题,不过真机上没问题。可以生成JAR使用!

    5.

    1. public void run()  
    2. {  
    3. try 
    4. {  
    5. while( true )  
    6. {  
    7. //    if( isStopGame ) continue;            //如果暂停游戏  
    8. updatas++;  
    9. switch (State)   
    10. {  
    11. case S_GAME_MAP:if( isStopGame ) break;    if(gamemap!=null)gamemap.updata(updatas);    break;  
    12. case STATE_MBOX:    setState(S_COVER);    break;    //切换到游戏状态  
    13. case STATE_LOAD_CMCC:    State=STATE_CMCC;    break;      
    14. case STATE_CMCC:    State=STATE_MBOX;    break;    //切换到第二屏  
    15. }  
    16. this.repaint();        //有需要时才重新绘制  
    17. Thread.sleep(sleepTime);  
    18. }  
    19. }    catch (Exception ex){}  

    如果使用注释掉的代码暂停游戏60会死机.v300.k700不会死掉.所以在60上只好用注释下面的那个暂停了!

    6.关于随机数对0求余的问题

    1. iRand() % 0; 

    一个正整数对0求余会抛出java/lang/ArithmeticException这个错误
    不过 一些机器上(S60.K700.V300)还是会强制iRand() % 0=0的(但是会卡一下)
    但是 S40报错之后无法继续运行。
    所以 S40上不要出现对0求余运算!

    7.

    1. int a=1000000000;  
    2. int b=1000000000;  
    3. int c=a*b;  
    4. System.out.println("c="+c); 

    这样写编译器允许,不过结果是错误的C的值已经超过INT的范围了,他会自动截取INT的32为,其他位为益出。

    1. byte a=100;  
    2. byte b=100;  
    3. byte c=a+b; 

    这样写编译器不允许,必须byte c=(byte)(a+b);才能通过。

    8. 偶尔在程序中用到了浮点数,部分模拟器,真机通过了!

    1. (int)( ax*Math.sin(dx/10)/precision ); 

    正弦曲线使用Math.sin( dx/10 ),dx/10为Double类型,输入参数为Double,也就是弧度,返回也是Double,即对应弧度的正弦值,经测试 Math.sin( Math.PI/2 ) 返回1, Math.sin( Math.PI )返回0,
    Nokia s60模拟器通过测试,可以使用。
    Nokia 6230i 和 Sony Ericsson k700 真机通过测试,可以使用!
    其他机型 估计新出的能通过,Nokia S60系列机器不能使用,如3230,6670等。
    相信 J2ME支持浮点运算只是个时间问题!

    9. 方法体大小是有限制的!
    不可以超过65535个字符!
        通常不会有这么多个字符,但是在写一些地图数组的时候方法体就可能很大,如果出现IDE报错,那么你就应该考虑是不是方法体过大了!结果办法就是 把地图数组写成资源文件,在程序中调入,解析,在使用!

    1. /*  
    2. * 生成 地图数文件Created on 2005-10-13  
    3. */ 
    4. import java.io.DataOutputStream;  
    5. import java.io.FileOutputStream;  
    6.  
    7. public class mapData  
    8. {  
    9.    byte[][] mapData =   //某一地图 图片数组  
    10.    {  
    11.      
    12.    };  
    13.      
    14.    int        dataBH        = 6;                    //0//地图编号        (地下城2.3.4.5)  
    15.    int        dataYs        = 3;                    //1//元素图片编号      
    16.    int    dataHs        = mapData.length;        //2//行数  
    17.    int    dataLs        = mapData[0].length;    //3//列数  
    18.    int    dataPz        = 41;                    //4//碰撞点  
    19.      
    20.    String    fileName    = "map"+dataBH+".gl";    // //文件名  
    21.    DataOutputStream out= null;  
    22.      
    23.    public static void main(String[] args)  
    24.    {  
    25.        mapData a = new mapData();  
    26.        try 
    27.        {  
    28.            a.out = new DataOutputStream( new FileOutputStream( a.fileName ) );  
    29.        }    catch(Exception e)    {}  
    30.        a.writeByteArray();  
    31.    }  
    32.  
    33.    public void writeByteArray()  
    34.    {  
    35.        try 
    36.        {  
    37.            out.writeByte( dataBH );    //地图编号        0  
    38.            out.writeByte( dataYs );    //地图元素编号    1  
    39.            out.writeByte( dataHs );    //行数            2  
    40.            out.writeByte( dataLs );    //列数            3  
    41.            out.writeByte( dataPz );    //碰撞点        4      
    42.  
    43.            for( int i=0; i<mapData.length; i++ )  
    44.            {  
    45.                for( int j=0; j<mapData[0].length; j++ )  
    46.                    out.writeByte( mapData[j] );  
    47.            }  
    48.        } catch(Exception e){}  
    49.    }  

    这个J2SE程序可以生成一个MAP1.GL的文件,文件中的数都是BYTE类型的 。

    1. /----------------------------j2me中的地图类----------------------------------------------------------------/  
    2.    /*读文件*/ 
    3.    public byte[] readFile(String filename)  
    4.    {  
    5.       byte[] data = null;  
    6.       try 
    7.       {  
    8.         InputStream in = this.getClass().getResourceAsStream(filename);  
    9.         in.read( dataIndex, 0, 5 );  
    10.         data = new byte[ dataIndex[2]*dataIndex[3] ];  
    11.         in.skip( 0 );  
    12.         in.read( data );  
    13.         in.close();  
    14.       }  
    15.       catch(Exception e){e.printStackTrace();}  
    16.       return data;  
    17.    }  
    18.    /*解吸文件*/ 
    19.   public byte[][] readMapData( String filename )  
    20.   {  
    21.      int k=0;  
    22.      byte[] data = readFile(filename);  
    23.      byte[][] map = new byte[ dataIndex[2] ][ dataIndex[3] ];  
    24.        
    25.     for( int i=0; i<dataIndex[2]; i++ )  
    26.        {  
    27.            for( int j=0; j<dataIndex[3]; j++ )  
    28.            {  
    29.                map[j] = data[k];  
    30. //                System.out.print(data[k]+"," );  
    31.                k++;  
    32.            }  
    33. //            System.out.println("" );  
    34.        }  
    35.     return map;  
    36.   } 

    在 构造函数中

    1. byte[][]   map = readMapData("/res/map"+this.mapNum+".gl");//读文件/解吸/地图数组 

    就可以得到地图数组
    地图信息已经写到

    1. byte[] dataIndex 中了,需要添加其他信息可以更改 dataIndex 的大小 

    10. 字符串连接 与 ?: 判断符号 优先级

    1. String str0 = "abd";  
    2. String str1 = "123" + str0==null ? "null" : str0 ; 

    表达试中 ? : 的优先级没有 + 的高
    所以 计算结果等于

    1. String str1 = ("123" + str0) ==null ? "null" : str0 ; 

    所以 要的到 str0不等于NULL的结果,可以

    1. String str1 = "123" + (str0==null ? "null" : str0 ); 

    11. 当定义一个对象数组出现没有初始化错误时候:

    1. int typeSum = 10;  
    2. JTextField[] jTextField1 = null;  
    3. jTextField1     = new JTextField[typeSum]; 

    这样并未初始化 jTextField1[0]..jTextField1[9]等对象!
    这就要看JTextField类的构造函数了
    public class JTextField extends JTextComponent
    这个类JTextField也是继承来的
    构造函数:

    1. public JTextField() {  
    2.     this(null, null, 0);  
    3.   }  
    4.  
    5.   public JTextField(String text) {  
    6.     this(null, text, 0);  
    7.   }  
    8.  
    9.   public JTextField(int columns) {  
    10.     this(null, null, columns);  
    11.   }  
    12.  
    13.   public JTextField(String text, int columns) {  
    14.     this(null, text, columns);  
    15.   }  
    16.  
    17.   public JTextField(Document doc, String text, int columns) {  
    18.   ...  
    19.   } 

    等等..

    1. jTextField1     = new JTextField[typeSum]; 

    这句话调用的是

    1. public JTextField() {  
    2.     this(null, null, 0);  
    3.   } 

    这个构造函数
    所以没有正确初始化 jTextField1这个数组
    可以在使用前

    1. for( int i=0; i<typeSum; i++)  
    2. {  
    3. jTextField1 = new JTextField();  

    初始化就不会报错拉!

  • 相关阅读:
    gitlab
    MySQL千万级别大表,你要如何优化?
    kafka入门
    zookeeper的原理和应用
    MySQL 性能优化之慢查询
    Redis一些新的看法
    mysql 数据库锁
    MYSQL查看进程和kill进程
    hadoop批量命令脚本xcall.sh及jps找不到命令解决
    java stream 处理分组后取每组最大
  • 原文地址:https://www.cnblogs.com/liudianjia/p/12529629.html
Copyright © 2020-2023  润新知