j2me学习八_线程动画及rms

线程动画: 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

 

public class c extends MIDlet {
 MyCanvas mycanvas;
 Display display;
 public c()
 {
  mycanvas=new MyCanvas();
  display=Display.getDisplay(this);
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  display.setCurrent(mycanvas);
 }

}
class MyCanvas extends Canvas implements Runnable
{
 int w,h;
 Image img;
 int index;
 MyCanvas()
 {
  w=this.getWidth();
  h=this.getHeight();
 
 try{
  img=Image.createImage("/moon.png");
  
 }catch (Exception e) {
  // TODO: handle exception
  e.printStackTrace();
 }
 new Thread(this).start();
 }
 protected void paint(Graphics g) {
  // TODO Auto-generated method stub
  g.setColor(0xffffff);
  g.fillRect(0, 0, w, h);
  drawClipImag(g,img,40,40,index++%8*60,0,60,50);
 }
 //在屏幕上显示画帧,x,y为图像在屏幕的位置,mx,my为小图在大图位置,mw,mh为小图尺寸
 public void drawClipImag(Graphics g,Image img,int x,int y,int mx,int my,int mw,int mh)
 {
  g.setClip(x, y, mw, mh);
  g.drawImage(img, x-mx, y-my, 0);
 }
 public void run() {
  // TODO Auto-generated method stub
  while(true)
  {
   try{
    Thread.sleep(150);
   }catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
   }
   repaint();
   serviceRepaints();
  }
 }
 
 
}

rms:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;


public class Rms extends MIDlet {
 String name="jack";
 int score=80;
 public Rms()
 {
  initRMS();
 }
 //增加数据
 public void initRMS()
 {
  try {
   RecordStore rs=RecordStore.openRecordStore("testRMS", true);
   ByteArrayOutputStream baos=new ByteArrayOutputStream();
   DataOutputStream os=new DataOutputStream(baos);
   os.writeUTF(name);
   os.writeInt(score);
   os.close();
   byte[] data=baos.toByteArray();
   rs.addRecord(data, 0, data.length);
   System.out.println("Ok~");
   rs.closeRecordStore();
   baos.close();
   os.close();
   } catch (Exception e) {
   // TODO: handle exception
    e.printStackTrace();
  }
 }
 //获得数据
 public void loadRMS(int ID)
 {
  try{
   RecordStore rs=RecordStore.openRecordStore("testRMS", true);
   byte[] buf=rs.getRecord(ID);
   //删除记录
   //rs.deleteRecord(ID);
   ByteArrayInputStream bis=new ByteArrayInputStream(buf);
   DataInputStream dis=new DataInputStream(bis);
   dis.readUTF();
   dis.readInt();
   rs.closeRecordStore();
   dis.close();
   bis.close();
   
  }catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
 public void saveRms() {
  try{
   RecordStore rs=RecordStore.openRecordStore("testRMS", true);
   ByteArrayOutputStream bos=new ByteArrayOutputStream();
   DataOutputStream dos=new DataOutputStream(bos);
   dos.writeUTF("NewName");
   dos.writeInt(100);
   byte[] temp=bos.toByteArray();
   dos.flush();
   bos.reset();
   rs.setRecord(1, temp, 0, temp.length);
   rs.closeRecordStore();
   bos.close();
   dos.close();
  }catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值