精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-04-15
最后修改:2011-04-15
本次会详细讲解将Android的Snake Sample移植到J2ME上,从而比较二者的区别和联系。 package com.deaboway.j2me; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class MyMidlet extends MIDlet { 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 } }
package com.deaboway.android; import android.app.Activity; import android.os.Bundle; public class myActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } }
Display d = getWindowManager().getDefaultDisplay(); screenWidth = d.getWidth(); screenHeight = d.getHeight();
int clipX = g.getClipX(); int clipY = g.getClipY(); int clipWidth = g.getClipWidth(); int clipHeight = g.getClipHeight(); g.clipRect(x, y, width, height); g.setClip(clipX, clipY, clipWidth, clipHeight);//释放当前状态
canvas.save();//保存当前状态 canvas.clipRect(x,y, x+width, y+height) cavnas.resave();//释放当前状态
g.setColor(Color.WHITE); g.fillRect(0,0,getWidth(),getHeight());
// 首先定义paint Paint paint = new Paint(); // 绘制矩形区域-实心矩形 // 设置颜色 paint.setColor(Color.WHITE); // 设置样式-填充 paint.setStyle(Style.FILL); // 绘制一个矩形 canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint);
Image bufImage=Image.createImage(bufWidth, bufHeight); Graphics bufGraphics=bufImage.getGraphics();
Bitmap carBuffer = Bitmap.createBitmap(bufWidth, bufHeight, Bitmap.Config.ARGB_4444); Canvas carGp = new Canvas(carBuffer);
Player s=Manager.createPlayer(InputStream); s.prepare();//创建 s.start();//播放 s.stop();//暂停 s.stop();//关闭 s.release();//释放
private void initSnakeView() { // 获取图片资源 try { imageRED_STAR = Image.createImage("/redstar.png"); imageRED_STAR = Utils.zoomImage(imageRED_STAR,mTileSize,mTileSize); imageYELLOW_STAR = Image.createImage("/yellowstar.png"); imageYELLOW_STAR = Utils.zoomImage(imageYELLOW_STAR,mTileSize,mTileSize); imageGREEN_STAR = Image.createImage("/greenstar.png"); imageGREEN_STAR = Utils.zoomImage(imageGREEN_STAR,mTileSize,mTileSize); } catch(Exception e) { Log.info("Create Images: "+e); } // 设置贴片图片数组 resetTiles(4); // 把三种图片存到Bitmap对象数组 loadTile(RED_STAR, imageRED_STAR); loadTile(YELLOW_STAR, imageYELLOW_STAR); loadTile(GREEN_STAR, imageGREEN_STAR); }
// 坐标数组转整数数组,把Coordinate对象的x y放到一个int数组中——用来保存状态 private String coordVectorToString(Vector cvec) { int count = cvec.size(); StringBuffer rawArray = new StringBuffer(); for (int index = 0; index < count; index++) { Coordinate c = (Coordinate) cvec.elementAt(index); rawArray.append(c.x+","); rawArray.append(c.y+","); Log.info("coordVectorToString(), c.x="+c.x+",c.y="+c.y); } Log.info("coordVectorToString(), rawArray.toString="+rawArray); return rawArray.toString(); } // 整数数组转坐标数组,把一个int数组中的x y放到Coordinate对象数组中——用来恢复状态 // @J2ME 还是用Vector替换ArrayList private Vector coordStringToVector(String raw) { Vector coordArrayList = new Vector(); Log.info("coordStringToVector(), raw="+raw); String[] rawArray = Utils.splitUtil(raw,","); Log.info("coordStringToVector(), rawArray.length="+rawArray.length); int coordCount = rawArray.length; for (int index = 0; index < coordCount; index += 2) { Coordinate c = new Coordinate(Integer.parseInt(rawArray[index]), Integer.parseInt(rawArray[index + 1])); coordArrayList.addElement(c); } return coordArrayList; }
/** * <p>Title: Snake</p> * <p>Copyright: (C) 2011 Gavin's Snake project. Licensed under the Apache License, Version 2.0 (the "License")</p> * @author Gavin */ package com.deaboway.snake.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import javax.microedition.rms.RecordStore; public class Bundle extends BaseRMS { private static String[] SECTION = { "\"AL\":","\"DT\":", "\"ND\":","\"MD\":", "\"SC\":","\"ST\":"}; private static int LEN = SECTION.length; private static boolean inited = false; private static Bundle INSTANCE; public static void INIT(){ if(inited)return; inited = true; INSTANCE = new Bundle(); INSTANCE.loadBundles(); } public static Bundle getInstance(){ return INSTANCE; } private String[] CONTENT = new String[LEN]; private Bundle() { super("snake-view"); } public void loadBundles() { try { this.open(); this.close(); } catch (Exception e) { try { this.close(); RecordStore.deleteRecordStore(this.getRMSName()); this.open(); this.close(); } catch (Exception ex) { } } } public void resetBundles() { try { this.close(); RecordStore.deleteRecordStore(this.getRMSName()); this.open(); this.close(); } catch (Exception ex) { } } public void updateBundles() throws Exception { try { this.openonly(); updateData(); if (this.getRecordStore() != null) this.close(); } catch (Exception e) { throw new Exception(this.getRMSName() + "::updateBundles::" + e); } } protected void loadData() throws Exception { try { byte[] record = this.getRecordStore().getRecord(1); DataInputStream istream = new DataInputStream( new ByteArrayInputStream(record, 0, record.length)); String content = istream.readUTF(); int[] start = new int[LEN+1]; for(int i =0;i<LEN;i++){ start[i] = content.indexOf(SECTION[i]); } start[LEN]=content.length(); for(int i =0;i<LEN;i++){ CONTENT[i] = content.substring(start[i]+5,start[i+1]); Log.info("CONTENT["+i+"]="+CONTENT[i]); } } catch (Exception e) { throw new Exception(this.getRMSName() + "::loadData::" + e); } } protected void createDefaultData() throws Exception { try { ByteArrayOutputStream bstream = new ByteArrayOutputStream(12); DataOutputStream ostream = new DataOutputStream(bstream); CONTENT[0] = "9,20,9,7"; CONTENT[1] = "1"; CONTENT[2] = "1"; CONTENT[3] = "600"; CONTENT[4] = "0"; CONTENT[5] = "7,7,6,7,5,7,4,7,3,7,2,7"; StringBuffer sb = new StringBuffer(); for(int i=0;i<LEN;i++){ sb.append(SECTION[i]); sb.append(CONTENT[i]); } ostream.writeUTF( sb.toString()); ostream.flush(); ostream.close(); byte[] record = bstream.toByteArray(); this.getRecordStore().addRecord(record, 0, record.length); } catch (Exception e) { throw new Exception(this.getRMSName() + "::createDefaultData::" + e); } } protected void updateData() throws Exception { try { ByteArrayOutputStream bstream = new ByteArrayOutputStream(12); DataOutputStream ostream = new DataOutputStream(bstream); StringBuffer sb = new StringBuffer(); for(int i=0;i<LEN;i++){ sb.append(SECTION[i]); sb.append(CONTENT[i]); } ostream.writeUTF(sb.toString()); ostream.flush(); ostream.close(); byte[] record = bstream.toByteArray(); this.getRecordStore().setRecord(1, record, 0, record.length); } catch (Exception e) { throw new Exception(this.getRMSName() + "::updateData::" + e); } } public String getValue(int key) { return CONTENT[key]; } public void setValue(int key, String value) { CONTENT[key] = value; } }
/** * <p>Title: Snake</p> * <p>Copyright: (C) 2011 Gavin's Snake project. Licensed under the Apache License, Version 2.0 (the "License")</p> * @author Gavin */ package com.deaboway.snake.util; public class Log{ private static final int FATAL = 0; private static final int ERROR = 1; private static final int WARN = 2; private static final int INFO = 3; private static final int DEBUG = 4; private static int LOG_LEVEL = INFO; public static void info(String string){ if(LOG_LEVEL >= INFO){ System.out.println("[Deaboway][ INFO] " + string); } } public static void debug(String string){ if(LOG_LEVEL >= DEBUG){ System.out.println("[Deaboway][DEBUG] " + string); } } public static void warn(String string){ if(LOG_LEVEL >= WARN){ System.out.println("[Deaboway][ WARN] " + string); } } public static void error(String string){ if(LOG_LEVEL >= ERROR){ System.out.println("[Deaboway][ERROR] " + string); } } public static void fatal(String string){ if(LOG_LEVEL >= FATAL){ System.out.println("[Deaboway][FATAL] " + string); } } }
class RefreshHandler extends Thread { public void run() { while (true) { try { //delay一个延迟时间单位 Thread.sleep(mMoveDelay); } catch (Exception e) { e.printStackTrace(); } // 更新View对象 SnakeView.this.update(); // 强制重绘 SnakeView.this.repaint(); } } };
/** * <p>Title: Snake</p> * <p>Copyright: (C) 2011 Gavin's Snake project. Licensed under the Apache License, Version 2.0 (the "License")</p> * @author Gavin */ package com.deaboway.snake; import javax.microedition.lcdui.Display; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import com.deaboway.snake.util.BaseRMS; import com.deaboway.snake.util.Bundle; import com.deaboway.snake.util.Log; /** * Snake: a simple game that everyone can enjoy. * * 贪吃蛇: 经典游戏,在一个花园中找苹果吃,吃了苹果会变长,速度变快。碰到自己和墙就挂掉 * */ public class Snake extends MIDlet { public static Display display; public static SnakeView mSnakeView; public static MIDlet SNAKE; public Snake() { Bundle.INIT(); display=Display.getDisplay(this); SNAKE = this; mSnakeView = new SnakeView(); mSnakeView.setTextView(""); mSnakeView.setMode(SnakeView.READY); display.setCurrent(mSnakeView); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { mSnakeView.saveState(); } protected void pauseApp() { mSnakeView.setMode(SnakeView.PAUSE); } protected void startApp() throws MIDletStateChangeException { // 检查存贮状态以确定是重新开始还是恢复状态 Log.debug("startApp(), BaseRMS.FIRST="+BaseRMS.FIRST); if (BaseRMS.FIRST) { // 存储状态为空,说明刚启动可以切换到准备状态 mSnakeView.setMode(SnakeView.READY); } else { // 已经保存过,那么就去恢复原有状态 // 恢复状态 if (!mSnakeView.restoreState()) { // 恢复状态不成功,设置状态为暂停 mSnakeView.setMode(SnakeView.PAUSE); } } } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-04-26
期待中
|
|
返回顶楼 | |
发表时间:2011-04-26
archangelwin 写道 期待中
已经放出来了,参考:http://www.iteye.com/topic/1013918 |
|
返回顶楼 | |
浏览 3742 次