⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectappcanvas.java

📁 一款模仿劲舞团的手机游戏
💻 JAVA
字号:


import java.io.IOException;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Image2D;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Loader;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.Object3D;
import javax.microedition.m3g.Texture2D;
import javax.microedition.m3g.World;

/**
 * @author trojan
 * 
 * ����������ʾ�����һ���
 * 
 */
public class SelectAppCanvas extends GameCanvas {

	// ------------------------------����ֿ�ʼ------------------------------------

	private Hashtable characterInfo;// ������������Ϣ

	private MyMIDLET myMidlet;// ����Canvas����

	// -----------------------------��ĻԪ��---------------------------------------
	private int width;// ��

	private int height;// ��

	private Image bg;// ����

	private Image[] appImageArray;// ���ͼƬ����

	private Image selectedImage;// ѡ���ͼƬ

	private Image leftArrow;// ���ͷͼƬ

	private Image leftArrowPressed;// ���ͷ����ͼƬ

	private Image rightArrow;// �Ҽ�ͷͼƬ

	private Image rightArrowPressed;// �Ҽ�ͷ����ͼƬ

	private Image lock;// ��ͼ;

	private final int GRAPHICS_ANCHOR = Graphics.HCENTER | Graphics.VCENTER;// ����ͼƬ��ê��������

	private final int STRING_ANCHOR = Graphics.LEFT | Graphics.TOP;

	// -----------------------------3D���----------------------------------------
	private World world;// �=�

	private Background background;// 3D����

	private Light light;// �ƹ�

	private Graphics3D g3d;// 3D�����豸

	private int[] m3gTexIndex = new int[] { 120, 114, 121 };// �����˸�λ��M3G�ļ��е�ID

	// -----------------------------����������---------------------------------------------
	private String[] appPathArray = new String[] { "app00", "app01", "app02",
			"app03", "app04", "app05" };// ������ϵͳ������۵�·��

	private int[] appPurview = new int[] { 0, 0, 0, 0, 0, 0 };// ��������۵Ŀ���״̬

	private int purview;// �������������

	private String appPath;// ��������ʱ�õ������·��

	private int currentSelectedAppIndex;// ��ǰѡ�����������

	private int showingAppIndex;// ��ǰ��ʾ����������

	private int oldAppIndex;// ԭ4��ʾ����������

	// -----------------------------��������----------------------------------------------
	private final int LEFT = -4;// ��

	private final int RIGHT = 4;// ��

	private final int LEFTSOFT = -6;// �����

	private final int RIGHTSOFT = -7;// �����

	private int pressedKey;// ���µļ�

	private boolean leftHasKeyed = false;// ��־����Ƿ���

	private boolean rightHasKeyed = false;// ��־�Ҽ��Ƿ���

	// -----------------------------ʱ���������------------------------------------------------
	private Timer timer;// ��ʱ��

	private int startTime = 0;// ��ʼʱ��

	private int currentTime;// ��ǰʱ��

	private boolean isMoving = false;// ��־�Ƿ������ƶ�

	private int moveCount = 1;// ��ͼ������

	private Graphics g;

	private boolean isAuto = true;

	private int angle = 0;

	private final int AUTO = 100;

	// -----------------------------����ֽ���-----------------------------------------------

	// -----------------------------�������ֿ�ʼ------------------------------------------------
	// ���췽��
	public SelectAppCanvas(MyMIDLET myMidlet, Hashtable characterInfo) {
		super(false);
		this.myMidlet = myMidlet;
		this.characterInfo = characterInfo;
		initialize();
	}

	// ��ʼ������
	protected void initialize() {

		// ����ȫ��
		setFullScreenMode(true);

		// ��ȡg
		g = getGraphics();

		// ��������
		bg = createImage("/images/appbg.gif");

		// �������ͼƬ����
		appImageArray = new Image[appPathArray.length];
		for (int i = 0; i < appPathArray.length; i++) {
			appImageArray[i] = createImage("/images/" + appPathArray[i]
					+ ".png");
		}

		// ����ѡ���ͼƬ
		selectedImage = createImage("/images/selected.gif");

		// ������ͷ
		leftArrow = createImage("/images/left_arrow_1.gif");
		leftArrowPressed = createImage("/images/left_arrow_2.gif");
		rightArrow = createImage("/images/right_arrow_1.gif");
		rightArrowPressed = createImage("/images/right_arrow_2.gif");

		// ������ͼ
		lock = createImage("/images/lock.gif");

		// ��ȡԭ�е����·���������
		currentSelectedAppIndex = Integer.parseInt(characterInfo
				.get("cAppPath").toString());
		oldAppIndex = currentSelectedAppIndex;
		showingAppIndex = currentSelectedAppIndex;
		appPath = appPathArray[currentSelectedAppIndex];

		// ��ȡ����Ĺ����Ȩ��
		purview = Integer.parseInt(characterInfo.get("gate").toString());
		for (int i = 0; i < purview; i++) {
			appPurview[i] = 1;
		}

		// ��ȡ��Ļ����
		width = getWidth();
		height = getHeight();

		// ��ȡ3D���ƶ���
		g3d = Graphics3D.getInstance();

		// ��ȡģ��
		readModel();

		// ������ʱ��
		timer = new Timer();
		timer.scheduleAtFixedRate(new SelectAppTask(), 100, 100);
		getCurrentTime();
		startTime = currentTime;
	}

	// ��ͼ����
	protected void paintCanvas() {
		// ����
		Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
				Font.SIZE_LARGE);
		g.setFont(font);

		// ���Ʊ���
		g.drawImage(bg, width / 2, height / 2, Graphics.VCENTER
				| Graphics.HCENTER);

		// ����ѡ����
		if (isMoving == false) {
			g.drawImage(selectedImage, width / 2, height / 2 - 75,
					GRAPHICS_ANCHOR);
		}

		// ������ͼ����
		if (isMoving == true) {
			moveAppImage(pressedKey, moveCount);
			moveCount++;
			if (moveCount == 12) {
				isMoving = false;
				currentSelectedAppIndex -= (pressedKey / 4);
				currentSelectedAppIndex = (currentSelectedAppIndex + appImageArray.length)
						% appImageArray.length;
				if (appPurview[currentSelectedAppIndex] == 1) {
					characterInfo.put("cAppPath", String
							.valueOf(currentSelectedAppIndex));
					showingAppIndex = currentSelectedAppIndex;
					readModel();
				}
			}

		} else {
			// �������ͼƬ
			showAppImage();
		}

		// ��ʾ��ͷ
		if (leftHasKeyed == false) {
			g.drawImage(leftArrow, width / 2 - 80, height / 2 - 75,
					GRAPHICS_ANCHOR);
		} else {
			g.drawImage(leftArrowPressed, width / 2 - 80, height / 2 - 75,
					GRAPHICS_ANCHOR);
			leftHasKeyed = false;
		}

		if (rightHasKeyed == false) {
			g.drawImage(rightArrow, width / 2 + 80, height / 2 - 75,
					GRAPHICS_ANCHOR);
		} else {
			g.drawImage(rightArrowPressed, width / 2 + 80, height / 2 - 75,
					GRAPHICS_ANCHOR);
			rightHasKeyed = false;
		}

		// �����
		if (isAuto == true) {
			int pastTime = getPastTime();
			render(pastTime);
		} else {
			int key = getKeyStates();
			if (key == 2048) {
				angle -= 100;
				if (angle < 0) {
					angle += 6000;
				}
			}
			if (key == 4096) {
				angle += 100;
			}
			render(angle);
		}

		// ���ư�ť
		g.setColor(0xff000000);
		g.drawString("ȷ��", width / 2 - 83, height / 2 + 90, STRING_ANCHOR);
		g.drawString("����", width / 2 + 55, height / 2 + 90, STRING_ANCHOR);

		flushGraphics();
	}

	// ��ȡģ��
	protected void readModel() {
		Object3D[] obj = load("model");
		for (int i = 0; i < obj.length; i++) {
			if (obj[i] instanceof World) {
				world = (World) obj[i];
			}
		}

		for (int i = 0; i < 3; i++) {
			attachTexture(i);
		}

		light = new Light();
		light.setMode(Light.AMBIENT);
		light.setIntensity(3.0f);
		world.addChild(light);

		background = world.getBackground();
		background.setImage(createImage2D("/images/appbg_1.gif"));
	}

	// 3D���Ʒ���
	protected void render(int pastTime) {
		g3d.bindTarget(g);

		g3d.setViewport(width / 2 - 55, height / 2 - 45, 116, 150);
		world.animate(pastTime % 6000);
		g3d.render(world);

		g3d.releaseTarget();
	}

	// ��ʾͼƬ
	protected void showAppImage() {
		int left = (currentSelectedAppIndex == 0) ? appImageArray.length - 1
				: currentSelectedAppIndex - 1;
		int right = (currentSelectedAppIndex == appImageArray.length - 1) ? 0
				: currentSelectedAppIndex + 1;
		g.drawImage(appImageArray[left], width / 2 - 44, height / 2 - 75,
				GRAPHICS_ANCHOR);
		g.drawImage(appImageArray[currentSelectedAppIndex], width / 2,
				height / 2 - 75, GRAPHICS_ANCHOR);
		g.drawImage(appImageArray[right], width / 2 + 44, height / 2 - 75,
				GRAPHICS_ANCHOR);

		if (appPurview[left] == 0) {
			g.drawImage(lock, width / 2 - 44, height / 2 - 75, GRAPHICS_ANCHOR);
		}
		if (appPurview[currentSelectedAppIndex] == 0) {
			g.drawImage(lock, width / 2, height / 2 - 75, GRAPHICS_ANCHOR);
		}
		if (appPurview[right] == 0) {
			g.drawImage(lock, width / 2 + 44, height / 2 - 75, GRAPHICS_ANCHOR);
		}
	}

	// �ƶ�ͼƬ
	protected void moveAppImage(int direction, int moveCount) {
		int left1 = (currentSelectedAppIndex + 4) % (appImageArray.length);
		int left2 = (currentSelectedAppIndex + 5) % (appImageArray.length);
		int right1 = (currentSelectedAppIndex + 1) % (appImageArray.length);
		int right2 = (currentSelectedAppIndex + 2) % (appImageArray.length);
		g.drawImage(appImageArray[left1], width / 2 - 88
				+ (direction * moveCount), height / 2 - 75, GRAPHICS_ANCHOR);
		g.drawImage(appImageArray[left2], (width / 2 - 44)
				+ (direction * moveCount), height / 2 - 75, GRAPHICS_ANCHOR);
		g.drawImage(appImageArray[currentSelectedAppIndex], (width / 2)
				+ (direction * moveCount), height / 2 - 75, GRAPHICS_ANCHOR);
		g.drawImage(appImageArray[right1], (width / 2 + 44)
				+ (direction * moveCount), height / 2 - 75, GRAPHICS_ANCHOR);
		g.drawImage(appImageArray[right2], (width / 2 + 88)
				+ (direction * moveCount), height / 2 - 75, GRAPHICS_ANCHOR);

		if (appPurview[left1] == 0) {
			g.drawImage(lock, (width / 2 - 88) + (direction * moveCount),
					height / 2 - 75, GRAPHICS_ANCHOR);
		}
		if (appPurview[left2] == 0) {
			g.drawImage(lock, (width / 2 - 44) + (direction * moveCount),
					height / 2 - 75, GRAPHICS_ANCHOR);
		}
		if (appPurview[currentSelectedAppIndex] == 0) {
			g.drawImage(lock, (width / 2) + (direction * moveCount),
					height / 2 - 75, GRAPHICS_ANCHOR);
		}
		if (appPurview[right1] == 0) {
			g.drawImage(lock, (width / 2 + 44) + (direction * moveCount),
					height / 2 - 75, GRAPHICS_ANCHOR);
		}
		if (appPurview[right2] == 0) {
			g.drawImage(lock, (width / 2 + 88) + (direction * moveCount),
					height / 2 - 75, GRAPHICS_ANCHOR);
		}
	}

	// ���ռ��̰���
	protected void keyPressed(int key) {
		// ��ǰ������ͼ,����Ӧ����
		if (isMoving == true) {
			return;
		}

		// �жϰ�������
		switch (key) {
		case -3:
			key = LEFT;
			break;
		case 52:
			key = LEFT;
			break;
		case -4:
			key = RIGHT;
			break;
		case 54:
			key = RIGHT;
			break;
		case -5:
			key = LEFTSOFT;
			break;
		case -6:
			key = LEFTSOFT;
			break;
		case -7:
			key = RIGHTSOFT;
			break;
		case 48:
			key = AUTO;
			break;
		}
		// ������
		if (key == LEFT) {
			leftHasKeyed = true;
			pressedKey = LEFT;
			isMoving = true;
			moveCount = 1;
		}
		// ������
		if (key == RIGHT) {
			rightHasKeyed = true;
			pressedKey = RIGHT;
			isMoving = true;
			moveCount = 1;
		}

		if (key == AUTO) {
			if (isAuto == true) {
				isAuto = false;
				angle = getPastTime();
			} else if (isAuto == false) {
				isAuto = true;
				getCurrentTime();
				startTime = currentTime - angle % 6000;
			}
		}

		// ���������������
		if (key == LEFTSOFT || key == RIGHTSOFT) {
			confirm(key);
		}
		super.keyPressed(key);
	}

	// ��ȡM3G�ļ�
	public Object3D[] load(String m3gPath) {

		Object3D[] objs;

		try {
			objs = Loader.load("/m3g/" + m3gPath + ".m3g");
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return objs;
	}

	// ����Image2D����
	private Image2D createImage2D(String imagePath) {
		Image image = null;
		Image2D image2D = null;
		try {
			image = Image.createImage(imagePath);
		} catch (IOException e) {
			// TODO �Զ���� catch ��
			e.printStackTrace();
		}

		if (image != null) {
			image2D = new Image2D(Image2D.RGB, image);
		}
		return image2D;
	}

	// ����ͼƬ
	protected Image createImage(String path) {
		Image image = null;
		try {
			image = Image.createImage(path);
		} catch (IOException e) {
			// TODO �Զ���� catch ��
			e.printStackTrace();
		}
		return image;
	}

	// ��������
	private Texture2D createTexture(String appPath) {
		Image2D image2D = createImage2D(appPath);
		Texture2D texture = new Texture2D(image2D);
		return texture;
	}

	// �������
	private void attachTexture(int partIndex) {

		Mesh mesh = (Mesh) world.find(m3gTexIndex[partIndex]);

		Appearance app = mesh.getAppearance(0);
		appPath = appPathArray[showingAppIndex];
		Texture2D texture = createTexture("/images/" + appPath + "_"
				+ partIndex + ".png");
		app.setTexture(0, texture);
	}

	// ��ȡ��ǰʱ��
	protected void getCurrentTime() {
		currentTime = (int) System.currentTimeMillis();
	}

	// ��ȡ��Ϸʱ��
	protected int getPastTime() {
		getCurrentTime();
		return Math.abs(currentTime - startTime);
	}

	// ȷ�����?��
	protected void confirm(int key) {
		if (key == LEFTSOFT) {
			characterInfo.put("cAppPath", new Integer(showingAppIndex));
		} else {
			characterInfo.put("cAppPath", new Integer(oldAppIndex));
		}
		myMidlet.returnCompetition(characterInfo);
	}

	// ��ʱ����
	class SelectAppTask extends TimerTask {
		public void run() {
			paintCanvas();
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -