?让我继续解析
Graphics类
import lxs.slg.Game;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.Paint.Align;import android.graphics.Paint.Style;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Region;public class Graphics { public static final int BASELINE = 64; public static final int BOTTOM = 32; public static final int DOTTED = 1; public static final int HCENTER = 1; public static final int LEFT = 4; public static final int RIGHT = 8; public static final int SOLID = 0; public static final int TOP = 16; public static final int VCENTER = 2; //绘制的位置 Rect clipRect = new Rect(); Paint paint = null; Canvas canvas = null; private Rect getRect(int x, int y, int width, int height) { return new Rect(x, y, x + width, y + height); } public Graphics(Canvas canvas) { this.canvas = canvas; paint = new Paint(); canvas.clipRect(getRect(0, 0, Game.width, Game.height)); this.canvas.save(Canvas.CLIP_SAVE_FLAG); this.clipRect = canvas.getClipBounds(); } public Graphics(Canvas canvas, Paint paint) { this.canvas = canvas; this.paint = paint; canvas.clipRect(0, 0, canvas.getWidth(), canvas.getHeight()); this.clipRect = canvas.getClipBounds(); } public void clipRect(int x, int y, int width, int height) { canvas.clipRect(x, y, x + width, y + height); clipRect = canvas.getClipBounds(); } public void copyArea(int x_src, int y_src, int width, int height, int x_dest, int y_dest, int anchor) { } public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) { paint.setStyle(Style.STROKE); canvas.drawArc(new RectF(getRect(x, y, width, height)), startAngle, arcAngle, true, paint); } public void drawChar(char character, int x, int y, int anchor) { char[] chArr = new char[1]; chArr[0] = character; canvas.drawText(new String(chArr), x, y, paint); } public void drawChars(char[] data, int offset, int length, int x, int y, int anchor) { canvas.drawText(data, offset, length, x, y, paint); } public void drawImage(Image img, int x, int y, int anchor) { int alpha = paint.getAlpha(); paint.setAlpha(img.getAlpha()); canvas.drawBitmap(img.getImg(), x, y, paint); paint.setAlpha(alpha); } public void drawLine(int x1, int y1, int x2, int y2) { canvas.drawLine(x1, y1, x2, y2, paint); } public void drawRect(int x, int y, int width, int height) { Rect rect = getRect(x, y, width, height); paint.setStyle(Style.STROKE); canvas.drawRect(rect, paint); } public static Matrix getTransformMetrix(int transform, int width, int height) { Matrix mMatrix = new Matrix(); Matrix temp = new Matrix(); Matrix temp2 = new Matrix(); float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1 }; temp.setValues(mirrorY); switch (transform) { case Sprite.TRANS_NONE: break; case Sprite.TRANS_ROT90: mMatrix.setRotate(90, width / 2, height / 2); break; case Sprite.TRANS_ROT180: mMatrix.setRotate(180, width / 2, height / 2); break; case Sprite.TRANS_ROT270: mMatrix.setRotate(270, width / 2, height / 2); break; case Sprite.TRANS_MIRROR: mMatrix.postConcat(temp); break; case Sprite.TRANS_MIRROR_ROT90: mMatrix.postConcat(temp); mMatrix.setRotate(90, width / 2, height / 2); break; case Sprite.TRANS_MIRROR_ROT180: mMatrix.postConcat(temp); mMatrix.setRotate(180, width / 2, height / 2); break; case Sprite.TRANS_MIRROR_ROT270: mMatrix.postConcat(temp); mMatrix.setRotate(270, width / 2, height / 2); break; } return mMatrix; } public void drawRegion(Image src, int x_src, int y_src, int width, int height, int transform, int x_dst, int y_dst, int anchor) { if (x_src + width > src.getWidth() || y_src + height > src.getHeight() || width < 0 || height < 0 || x_src < 0 || y_src < 0) { throw new IllegalArgumentException("Area out of Image"); } int alpha = paint.getAlpha(); paint.setAlpha(src.getAlpha()); Bitmap newBitmap = null; Matrix tmp_matrix = new Matrix(); switch (transform) { case Sprite.TRANS_NONE: newBitmap = Bitmap.createBitmap(src.getImg(), x_src, y_src, width, height, tmp_matrix, true); break; case Sprite.TRANS_ROT90: tmp_matrix.reset(); tmp_matrix.preRotate(90); newBitmap = Bitmap.createBitmap(src.getImg(), x_src, y_src, width, height, tmp_matrix, true); break; case Sprite.TRANS_ROT180: tmp_matrix.reset(); tmp_matrix.preRotate(180); newBitmap = Bitmap.createBitmap(src.getImg(), x_src, y_src, width, height, tmp_matrix, true); break; case Sprite.TRANS_ROT270: tmp_matrix.reset(); tmp_matrix.preRotate(270); newBitmap = Bitmap.createBitmap(src.getImg(), x_src, y_src, width, height, tmp_matrix, true); break; case Sprite.TRANS_MIRROR: tmp_matrix.reset(); tmp_matrix.preScale(-1, 1); newBitmap = Bitmap.createBitmap(src.getImg(), x_src, y_src, width, height, tmp_matrix, true); break; case Sprite.TRANS_MIRROR_ROT90: tmp_matrix.reset(); tmp_matrix.preScale(-1, 1); tmp_matrix.preRotate(-90); newBitmap = Bitmap.createBitmap(src.getImg(), x_src, y_src, width, height, tmp_matrix, true); break; case Sprite.TRANS_MIRROR_ROT180: tmp_matrix.reset(); tmp_matrix.preScale(-1, 1); tmp_matrix.preRotate(-180); newBitmap = Bitmap.createBitmap(src.getImg(), x_src, y_src, width, height, tmp_matrix, true); break; case Sprite.TRANS_MIRROR_ROT270: tmp_matrix.reset(); tmp_matrix.preScale(-1, 1); tmp_matrix.preRotate(-270); newBitmap = Bitmap.createBitmap(src.getImg(), x_src, y_src, width, height, tmp_matrix, true); break; } canvas.drawBitmap(newBitmap, x_dst, y_dst, paint); paint.setAlpha(alpha); } public void drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height, boolean processAlpha) { canvas.drawBitmap(rgbData, offset, scanlength, x, y, width, height, processAlpha, paint); } public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) { Rect rect = getRect(x, y, width, height); paint.setStyle(Style.STROKE); canvas.drawRoundRect(new RectF(rect), arcWidth, arcHeight, paint); } public void drawString(String str, int x, int y, int anchor) { canvas.drawText(str, x, y, paint); } public void drawSubstring(String str, int offset, int len, int x, int y, int anchor) { canvas.drawText(str, offset, len, x, y, paint); } public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) { paint.setStyle(Style.FILL); canvas.drawArc(new RectF(x, y, width, height), startAngle, arcAngle, true, paint); } public void fillRect(int x, int y, int width, int height) { paint.setStyle(Style.FILL); canvas.drawRect(new RectF(getRect(x, y, width, height)), paint); } public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) { Rect rect = getRect(x, y, width, height); paint.setStyle(Style.FILL); canvas.drawRoundRect(new RectF(rect), arcWidth, arcHeight, paint); } public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) { paint.setStyle(Style.FILL); canvas.drawLine(x1, y1, x2, y2, paint); canvas.drawLine(x2, y2, x3, y3, paint); canvas.drawLine(x3, y3, x1, y1, paint); } public int getBlueComponent() { int color = paint.getColor(); color = color | 0x000000ff; return color; } public int getClipHeight() { return (int) clipRect.height(); } public int getClipWidth() { return (int) clipRect.width(); } public int getClipX() { return (int) clipRect.left; } public int getClipY() { return (int) clipRect.top; } public int getColor() { return paint.getColor(); } public int getAlpha() { return paint.getAlpha(); } // 和设备有关的函数 public int getDisplayColor(int color) { return -1; } public Font getFont() { return Font.getFont(paint); } public int getGrayScale() { return -1; } public int getGreenComponent() { int color = paint.getColor(); color = color | 0x0000ff00; return color >> 8; } public int getRedComponent() { int color = paint.getColor(); color = color | 0x00ff0000; return color >> 16; } public int getStrokeStyle() { return (int) paint.getStrokeWidth(); } public int getTranslateX() { return -1; } public int getTranslateY() { return -1; } // clipRect(RectF rect, Region.Op op) 函数的用法 public void setClip(int x, int y, int width, int height) { canvas.clipRect(getRect(x, y, width, height), Region.Op.REPLACE); clipRect = canvas.getClipBounds(); } public void setAlign(int align) { if (LEFT == align || (Graphics.LEFT | Graphics.TOP) == align || (Graphics.LEFT | Graphics.BOTTOM) == align) { paint.setTextAlign(Align.LEFT); } else if (HCENTER == align || (Graphics.HCENTER | Graphics.TOP) == align) { paint.setTextAlign(Align.CENTER); } else if (RIGHT == align || (Graphics.RIGHT | Graphics.TOP) == align) { paint.setTextAlign(Align.RIGHT); } } public void setColor(int RGB) { paint.setColor(RGB); } public void setAlpha(int alpha) { paint.setAlpha(alpha); } public void setColor(int red, int green, int blue) { paint.setARGB(0xff, red, green, blue); } // android 的字体的控制 public void setFont(Font font) { font.setPaintAtt(paint); } public void setGrayScale(int value) { } public void setStrokeStyle(int style) { paint.setStrokeWidth(style); } public void translate(int x, int y) { canvas.translate(x, y); }}
?sprite类
package lxs.slg.j2me;public class Sprite { // //////////////////////////////////////SPRITE ////////////////////////// public static final int TRANS_MIRROR = 2; public static final int TRANS_MIRROR_ROT180 = 1; public static final int TRANS_MIRROR_ROT270 = 4; public static final int TRANS_MIRROR_ROT90 = 7; public static final int TRANS_NONE = 0; public static final int TRANS_ROT180 = 3; public static final int TRANS_ROT270 = 6; public static final int TRANS_ROT90 = 5;}
?
?记录类RecordStore
import lxs.slg.DataBaseHelper;import lxs.slg.Game;import android.database.sqlite.SQLiteDatabase;public class RecordStore { final static int AUTHMODE_ANY = 0; final static int AUTHMODE_PRIVATE = 1; private int authmode; private boolean writable; private String recordStoreName; private int recordId; private DataBaseHelper dataBaseHelper = null; private SQLiteDatabase sqliteDatabase = null; private boolean cheakDataBase() { if (sqliteDatabase == null) { return false; } else { if (sqliteDatabase.isOpen()) { return true; } return false; } } public int addRecord(byte[] data, int offset, int numBytes) { if (!cheakDataBase()) { return 0; } if (sqliteDatabase.isReadOnly() || !writable) { return 0; } dataBaseHelper.insert(sqliteDatabase, recordId, new String(data, offset, numBytes)); recordId++; return recordId; } public void closeRecordStore() { sqliteDatabase.close(); } public void deleteRecord(int recordId) { dataBaseHelper.delete(sqliteDatabase, recordId); } public static void deleteRecordStore(String recordStoreName) { } public String getName() { return recordStoreName; } public int getNextRecordID() { return recordId; } public int getNumRecords() { return 0; } public byte[] getRecord(int recordId) { String storeData = dataBaseHelper.select(sqliteDatabase, recordId); return storeData.getBytes(); } public int getRecord(int recordId, byte[] buffer, int offset) { String storeData = dataBaseHelper.select(sqliteDatabase, recordId); byte byteArr[] = storeData.getBytes(); storeData = new String(byteArr, offset); buffer = storeData.getBytes(); return buffer.length; } public int getRecordSize(int recordId) { return (int) sqliteDatabase.getPageSize(); } public int getSize() { return (int) sqliteDatabase.getMaximumSize(); } public int getSizeAvailable() { return (int) sqliteDatabase.getMaximumSize(); } public int getVersion() { return sqliteDatabase.getVersion(); } public String getPath() { return sqliteDatabase.getPath(); } public static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary) { RecordStore recordStore = new RecordStore(recordStoreName); return recordStore; } public static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary, int authmode, boolean writable) { RecordStore recordStore = RecordStore.openRecordStore(recordStoreName, createIfNecessary); recordStore.setMode(authmode, writable); return recordStore; } public void setMode(int authmode, boolean writable) { this.authmode = authmode; this.writable = writable; if (writable) { sqliteDatabase = dataBaseHelper.getReadableDatabase(); } else { sqliteDatabase = dataBaseHelper.getReadableDatabase(); } } public void setRecord(int recordId, byte[] newData, int offset, int numBytes) { String tem = new String(newData, offset, numBytes); dataBaseHelper.update(sqliteDatabase, recordId, tem); } public RecordStore(String recordStoreName) { this.recordId = 0; this.recordStoreName = recordStoreName; dataBaseHelper = new DataBaseHelper(Game.father, Game.name, recordStoreName); dataBaseHelper.onCreate(sqliteDatabase); }}
?到此我们已经完成所用函数的方法的编写,但是现在我们还是需要编写一个Activity将MIDP接入到当前Andriod平台中
我编写了RunActivity
package lxs.slg;import java.util.Random;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.Window;import android.view.WindowManager;public class RunActivity extends Activity { static Random rd; Game game; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); rd = new Random(); game = new Game(RunActivity.this); setContentView(game); // Intent intent = getIntent(); // String name = intent.getStringExtra("name"); // setContentView(R.layout.main); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { super.onKeyDown(keyCode, event); return game.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { // TODO Auto-generated method stub super.onKeyUp(keyCode, event); return game.onKeyUp(keyCode, event); } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); return game.onTouchEvent(event); } public void notifyDestroyed() { android.os.Process.killProcess(android.os.Process.myPid()); }}
?
?