`
aiquantong
  • 浏览: 112757 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

J2ME游戏只移植到Android平台(二)

 
阅读更多

 让我继续解析

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());
	}

}

 

 

分享到:
评论

相关推荐

    J2ME游戏如何移植成android

    J2ME游戏移植到Android平台的技术要点 在本文中,我们将探讨如何将J2ME游戏移植到Android平台上。作为一名IT行业大师,我将详细地解释J2ME游戏移植到Android平台的技术要点。 首先, lets talk about the ...

    j2me移植Android引擎

    【标题】"J2ME移植Android引擎"涉及的是将基于Java Micro Edition (J2ME) 开发的游戏或应用转换到Android平台的过程。J2ME是Java平台的一个子集,主要用于移动设备和嵌入式系统,而Android则是一个开源的操作系统,...

    J2ME游戏移植到Android手机

    【J2ME游戏移植到Android手机】是一个技术性较强的主题,涉及到两个不同的移动平台:Java ME(J2ME)和Android。J2ME是早期移动设备上广泛使用的开发框架,而Android则是现代智能手机的主要操作系统。当需要将J2ME...

    j2me移植到Android(Ophone)-游戏文档

    手机游戏是目前深受广大手机用户喜爱的一种娱乐软件,各大运营商都有自己的手机游戏平台,比如中国移动的百宝箱,MM平台,当然...有了这个优点也就意味着所有的J2ME程序都可以非常快速的迁移到OPhone(Adnroid)平台。

    浅谈J2me游戏如何快速移植到Android

    对于许多从事J2ME开发工作的人员而言,可能会突然间面临一个紧迫的需求:那就是将原本在J2ME平台上的游戏移植到Android平台上。乍一看,这似乎是一项庞大且复杂的工程,但事实上,通过合理规划与策略实施,这一过程...

    J2ME游戏移植Android平台要点

    在本文中,我们将深入探讨如何将J2ME游戏移植到Android平台的关键点,以及两者之间的主要差异。 首先,让我们简要了解一下J2ME和Android的基础。J2ME(Java 2 Micro Edition)是Java 2的一个子集,设计用于嵌入式...

    j2me移植到android的代码封装

    当需要将J2ME应用移植到Android时,会遇到一些兼容性问题,因为两者的核心库和API有所差异。本篇文章将深入探讨如何将J2ME中的`Image`、`Font`、`Graphics`等关键类进行封装,以便在Android平台上运行。 首先,我们...

    j2me移植android

    Java 2 Micro Edition(J2ME)是一种专为嵌入式设备和移动设备设计的Java平台,它在功能手机时代广泛应用于开发各种应用程序。...提供的"j2me移植Android.txt"文件可能包含详细的步骤和技巧,建议仔细阅读并参考。

    j2me_android_sdk1.16

    随着Android平台的崛起,许多J2ME开发者希望能将他们的应用和游戏无缝迁移到Android平台上。"j2me_android_sdk1.16"便为此目的提供了一个解决方案,它是一个专为Android设计的SDK,允许开发者将J2ME程序移植到...

    j2me移植到android 代码替换开发包 移植利器(版权持有)

    j2me移植到android 代码替换开发包:替换j2me平台的Graphics ,Image ,HttpConntion,RecordStore,Player,Connector,Font,Canvas...

    android J2ME加载器 可以在android机上运行java游戏

    对于开发者而言,J2ME加载器提供了移植旧Java游戏到Android的可能性,从而扩大了这些游戏的受众群体。同时,这也为那些想将Android应用与Java ME生态结合的开发者提供了创新的途径。然而,需要注意的是,并非所有...

    J2ME游戏的基本结构

    在 J2ME 中开发游戏,理解其基本结构至关重要,因为这将直接影响到游戏的性能、可移植性和用户体验。下面我们将深入探讨 J2ME 游戏的基本结构。 一、J2ME 开发环境 J2ME 游戏开发通常需要一个集成开发环境(IDE),...

    两个小时精通Android开发之界面篇【j2me移植】

    笔者最近需要移植一个J2ME的项目到Android平台上,所以抽空研究了一下Android平台的接口规范。 Android平台除了在一些接口上采用了和J2ME不一样的名称之外,基本的接口内容还是一样的,那么移植J2ME到Android平台最...

    Android-development.rar_android_game android J2ME_site:en.pudn.c

    它特别指出,如果你已经有J2ME的基础,这个教程将有助于你理解如何将J2ME的游戏移植到Android平台。J2ME是一种用于开发移动设备和嵌入式系统的Java平台,它的移植性使得许多开发者能将其在J2ME上的作品适应到更现代...

    j2me_android_sdk_1.0

    Android下实现的J2ME API类库,使用此SDK可以无缝将你J2ME APP移植成android APP, 支持MIDP2.0、cldc1.0,WMA,jsr75等常用API 20111204更新新版: 1 JAD.MF文件:增加屏幕方向设置 2 JAD.MF文件:增加游戏专用摇杆...

    JAVA-zhuankuai(j2me).rar_J2ME游戏_j2me_j2me 游戏

    Java 2 Micro Edition (J2ME) 是一种Java平台,专为嵌入式设备和移动设备设计,如早期的智能手机和平板电脑。...尽管现代移动开发更多地依赖于Android和iOS平台,但J2ME的知识仍然对理解移动应用的演进历程有其价值。

    Android代码-安卓J2ME模拟器

    **Android代码 - 安卓J2ME模拟器** 在移动开发领域,J2ME(Java 2 Micro Edition)曾是广泛应用于早期...同时,对于开发者来说,它也是一个学习和研究Android平台上的模拟技术、跨平台移植以及旧代码复用的宝贵资源。

    ME上Layer,LayerManager,Sprite,TiledLayer移植到android

    总结起来,从J2ME移植到Android涉及到对原概念的重新实现,利用Android的视图系统和绘图API来模拟原有功能。理解并掌握这些概念在Android环境下的应用,是成功完成移植的关键。同时,要注意充分利用Android平台的...

    j2me2android

    1. **J2ME到Android的移植**:项目的核心在于提供了一种机制,使得开发者可以将原本为J2ME平台编写的应用程序移植到Android平台上,无需完全重写。 2. **源代码开放**:作为一个开源项目,"j2me2android"的源代码可...

Global site tag (gtag.js) - Google Analytics