www.pudn.com > 50code.rar > MyRecord.java, change:2007-02-26,size:2539b


package demo; 
import javax.microedition.lcdui.Graphics; 
import javax.microedition.rms.RecordStore; 
 
public class MyRecord{ 
	private int m_nCurRecord	= 0;	//当前得分 
	private int m_nHighRecord = 0;		//历史最高得分 
	private String m_strName;			//RMS系统中数据表的名称 
	 
	//构造方法,参数strName为存储数据的表单名称 
	public MyRecord( String strName ){ 
		m_strName = strName; 
		Load(); 
	} 
	//获取当前得分 
	public int getCurRecord(){ 
		return m_nCurRecord; 
	} 
	//设置当前得分 
	public void setCurRecord( int record ){ 
		m_nCurRecord = record; 
		//如果当前得分高于历史最高分,则保存 
		if( m_nHighRecord < m_nCurRecord ){ 
			m_nHighRecord = m_nCurRecord;	 
			Save(); 
		} 
	} 
	//存储历史最高分 
	public void Save(){ 
		try{			 
			byte b[] = new byte[4]; 
			int temp = m_nHighRecord; 
			//将int型数据,保存到byte数组 
			for(int i = b.length - 1; i >= 0; i--){ 
				b[i] = new Integer(temp & 0xff).byteValue(); 
				temp = temp >> 8; 
			}			 
			//打开RecordStore,没有则新建			 
			RecordStore rs = RecordStore.openRecordStore( 
					m_strName,true);			 
			if( rs.getNumRecords() > 0 ){	//如果记录数大于0 
				rs.setRecord(1, b, 0, b.length);  //更改第一条记录 
			}			 
			else{      
				//如果没有记录,则添加记录	 
				rs.addRecord(b,0,b.length);	 
			}		 
			rs.closeRecordStore(); 
		}	 
		catch (Exception ex){}	 
	}	 
	//读取历史最高分 
	public boolean Load(){ 
		try{			 
			RecordStore rs =  
				RecordStore.openRecordStore( m_strName, false);			 
			byte b[] = rs.getRecord(1); 
			m_nHighRecord = 0; 
			for (int i = 0; i < 3; i++){ 
				//如果b[i]小于0,则将b[i]转换成相应的正数 
				if( b[i] < 0 )	 
					m_nHighRecord += ( b[i] + 256 ); 
				else 
					m_nHighRecord += b[i];	 
				m_nHighRecord *= 256;	 
			}					 
			m_nHighRecord += b[3];	 
			if( b[3] < 0 )	 
				m_nHighRecord += 256;				 
			rs.closeRecordStore(); 
		} 
		catch (Exception ex){ 
			return false; 
		} 
		return true;		 
	} 
	//显示历史最高分记录 
	//参数g对应手机屏幕,scrWidth与scrHeight分别是屏幕的宽与高 
	public void Paint( Graphics g, int scrWidth, int scrHeight ){ 
		StringBuffer buffer = new StringBuffer(); 
		buffer.append("历史最高分:"); 
		buffer.append(m_nHighRecord);	 
		int x = scrWidth / 2; 
		int y = scrHeight / 2 - 20; 
		g.drawString(buffer.toString(), x, y,  
				Graphics.TOP|Graphics.HCENTER ); 
		 
		buffer.delete(0,buffer.length()); 
		buffer.append("本次得分:"); 
		buffer.append(m_nCurRecord); 
		y = y + 40; 
		g.drawString(buffer.toString(), x, y,  
				Graphics.TOP|Graphics.HCENTER ); 
	} 
}