`
touchmm
  • 浏览: 1005379 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics