import java.util.Vector;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.m3g.*;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import java.io.InputStream;
public class M3GCanvas extends GameCanvas implements Runnable{
private World world;
private Camera camera;
private Vector cubeVector;
private Vector cubeScaleVector;
private boolean isRunning,firstrun;
private int cube_matrix[][][];
private int cube_matrix_final[][][];
private int cube_surface[][];
private int pre_act,t,steps;
private int stack[];
private Thread thread;
private InputStream is1,is2;
private Player player1,player2;
private boolean b_playSound;
private static final String []move_music = {
"/roll.WAV",
"/CLICK.WAV"
};
public void setSoundOn(boolean playSound)
{
b_playSound = playSound;
if (b_playSound){
System.out.println("play sound on");
is1 = getClass().getResourceAsStream(move_music[0]);
is2 = getClass().getResourceAsStream(move_music[1]);
try {
player1 = Manager.createPlayer(is1, "audio/X-wav");
player2 = Manager.createPlayer(is2, "audio/X-wav");
} catch (Exception e){
System.out.println(e.toString());
}
}
}
public M3GCanvas()
{
super(false);
cube_point p;
cubeVector = new Vector();
cubeScaleVector = new Vector();
cube_matrix = new int[3][3][3];
cube_matrix_final = new int[3][3][3];
cube_surface = new int[3][3];
//setFullScreenMode(true);
world = new World();
setTitle("Magic Cube");
pre_act = -1;firstrun = true;
stack = new int[]{6,4,1,7,0,5,2,0,6,1,2,4,8,0,8,1,7,0,7,8,2,7,3,8,1,9,6,4,3,5,2,0,7,2,0,6,1,-1,3,5,
6,7,5,3,8,9,8,1,8,1,0,4,5,5,0,8,2,7,8,0,0,8,7,5,4,8,9,3,7,5,3,8,7,5,4,8,7,7,8,6,
0,6,3,6,4,3,3,7,1,0,4,9,8,5,8,1,1,3,4,7,-1};
//stack = new int[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1};
for (int i = 0;i < 27;i ++)
{
cube_scale scale = new cube_scale();
cubeScaleVector.addElement(scale);
}
for (int z = 0,t = 0;z < 3;z ++)
{
for (int y = 0;y < 3;y ++)
{
for (int x = 0;x < 3;x ++)
{
cube_matrix[x][y][z] = t;
cube_matrix_final[x][y][z] = t ++;
}
}
}
for (int i = 0;i < 27;i ++)
{
p = getDirection(i + 1);
createCube(i,p.x,p.y,p.z);
addAppearance(i);
}
createCamera();
//isRunning = true;
thread = new Thread(this);
thread.start();
}
private void playMusic(int nIndex)
{
if (b_playSound == false)
{
return;
}
try {
//InputStream is = getClass().getResourceAsStream(move_music[nIndex]);
if (is1 == null){
System.out.println("is1 == null");
}
if (nIndex == 0 && is1 != null){
player1.realize();
player1.prefetch();
player1.start();
}
else if (nIndex == 1){
player2.realize();
player2.prefetch();
player2.start();
}
} catch(Exception e){
System.out.println("Error Playing Music :" + e.toString());
}
}
private cube_point getDirection(int nIndex)
{
cube_point p = new cube_point();
switch (nIndex){
case 1:
p.set(-2, 2, 2);break;
case 2:
p.set(0, 2, 2);break;
case 3:
p.set(2, 2, 2);break;
case 4:
p.set(-2, 0, 2);break;
case 5:
p.set(0, 0, 2);break;
case 6:
p.set(2, 0, 2);break;
case 7:
p.set(-2, -2, 2);break;
case 8:
p.set(0, -2, 2);break;
case 9:
p.set(2, -2, 2);break;
case 10:
p.set(-2, 2, 0);break;
case 11:
p.set(0, 2, 0);break;
case 12:
p.set(2, 2, 0);break;
case 13:
p.set(-2, 0, 0);break;
case 14:
p.set(0, 0, 0);break; //center point
case 15:
p.set(2, 0, 0);break;
case 16:
p.set(-2, -2, 0);break;
case 17:
p.set(0, -2, 0);break;
case 18:
p.set(2, -2, 0);break;
case 19:
p.set(-2, 2, -2);break;
case 20:
p.set(0, 2, -2);break;
case 21:
p.set(2, 2, -2);break;
case 22:
p.set(-2, 0, -2);break;
case 23:
p.set(0, 0, -2);break;
case 24:
p.set(2, 0, -2);break;
case 25:
p.set(-2, -2, -2);break;
case 26:
p.set(0, -2, -2);break;
case 27:
p.set(2, -2, -2);break;
}
return p;
}
private void createCube(int nIndex,short dx,short dy,short dz)
{
short []POINTS = new short[]{
/*1, 1, 1,
-1, 1, 1,
-1, -1, 1,
1, -1, 1,
1, 1, -1,
-1, 1, -1,
-1, -1, -1,
1, -1, -1*/
-1,-1,-1, 1,-1,-1, 1,1,-1, -1,1,-1,
-1,1,-1, -1,1,1, -1,-1,1, -1,-1,-1,
-1,-1,1, -1,1,1, 1,1,1, 1,-1,1,
1,-1,1, 1,1,1, 1,1,-1, 1,-1,-1,
1,-1,-1, -1,-1,-1, -1,-1,1, 1,-1,1,
1,1,-1, -1,1,-1, -1,1,1, 1,1,1,
1,1,-1, -1,1,-1, -1,-1,-1, 1,-1,-1,
-1,-1,1, -1,-1,-1, -1,1,-1, -1,1,1,
1,1,1, 1,-1,1, -1,-1,1, -1,1,1,
1,1,-1, 1,-1,-1, 1,-1,1, 1,1,1,
-1,-1,1, 1,-1,1, 1,-1,-1, -1,-1,-1,
-1,1,1, 1,1,1, 1,1,-1, -1,1,-1
};
byte []COLORS = new byte[]{
/*
127, 127, 127,
127, 0, 0,
0, 127, 0,
0, 0, 127,
0, 0, 0,
60, 60, 60,
127, 127, 0,
0, 127, 127,*/
127,0,0, 127,0,0, 127,0,0, 127,0,0,
0,127,0, 0,127,0, 0,127,0, 0,127,0,
0,0,127, 0,0,127, 0,0,127, 0,0,127,
0,127,127, 0,127,127, 0,127,127, 0,127,127,
127,0,127, 127,0,127, 127,0,127, 127,0,127,
127,127,0, 127,127,0, 127,127,0, 127,127,0,
127,0,0, 127,0,0, 127,0,0, 127,0,0,
0,127,0, 0,127,0, 0,127,0, 0,127,0,
0,0,127, 0,0,127, 0,0,127, 0,0,127,
0,127,127, 0,127,127, 0,127,127, 0,127,127,
127,0,127, 127,0,127, 127,0,127, 127,0,127,
127,127,0, 127,127,0, 127,127,0, 127,127,0
};
int []INDICES = new int[]{
//0,3,7,4,5,0,1,2,5,6,7,2,3,0
/*
0,1,2,3,
3,7,4,0,
4,7,6,5,
5,6,2,1,
1,0,4,5,
2,3,7,6*/
0,1,2,3,
4,5,6,7,
8,9,10,11,
12,13,14,15,
16,17,18,19,
20,21,22,23,
24,25,26,27,
28,29,30,31,
32,33,34,35,
36,37,38,39,
40,41,42,43,
44,45,46,47
};
int []LENGTH = new int [] {4,4,4,4,4,4, 4,4,4,4,4,4};
for (int i = 0;i < POINTS.length;i += 3)
{
POINTS[i] += dx;
POINTS[i + 1] += dy;
POINTS[i + 2] += dz;
}
IndexBuffer ibCube = new TriangleStripArray(INDICES, LENGTH);
VertexArray vaCube = new VertexArray(POINTS.length / 3, 3, 2);
vaCube.set(0, POINTS.length / 3, POINTS);
VertexArray vaColor = new VertexArray(COLORS.length / 3,3,1);
vaColor.set(0,COLORS.length / 3,COLORS);
VertexBuffer vertexBuffer = new VertexBuffer();
vertexBuffer.setPositions(vaCube,0.25f,null);
vertexBuffer.setColors(vaColor);
Mesh cube = new Mesh(vertexBuffer,ibCube,null);
cube.setTranslation(0.0f, 0.0f, -4.0f);
world.addChild(cube);
cubeVector.addElement(cube);
}
private void addAppearance(int nIndex)
{
Appearance appearance = new Appearance();
PolygonMode polygonMode = new PolygonMode();
polygonMode.setPerspectiveCorrectionEnable(true);
polygonMode.setCulling(PolygonMode.CULL_NONE);
polygonMode.setShading(PolygonMode.SHADE_FLAT);
polygonMode.setTwoSidedLightingEnable(true);
appearance.setPolygonMode(polygonMode);
Mesh cube = (Mesh)cubeVector.elementAt(nIndex);
cube.setAppearance(0, appearance);
}
private void createCamera()
{
float w = getWidth();
float h = getHeight();
camera = new Camera();
camera.setPerspective(60.0f, w / h, 1f, 50f);
world.addChild(camera);
world.setActiveCamera(camera);
}
private void turn_matrix(int nDirection)
{
int [][][]temp = new int[3][3][3];
int x,y,z;
switch (nDirection)
{
case 1: //up
for (z = 0;z < 3;z ++){
for (y = 0;y < 3;y ++){
for (x = 0;x < 3;x ++){
temp[x][y][z] = cube_matrix[x][2 - z][y];
}
}
}
for (z = 0;z < 3;z ++){
for (y = 0;y < 3;y ++){
for (x = 0;x < 3;x ++){
cube_matrix[x][y][z] = temp[x][y][z];
没有合适的资源?快使用搜索试试~ 我知道了~
J2ME 3D魔方游戏
共28个文件
class:10个
java:5个
wav:4个
4星 · 超过85%的资源 需积分: 3 42 下载量 13 浏览量
2009-04-23
20:32:34
上传
评论 1
收藏 137KB RAR 举报
温馨提示
J2ME 3D魔方游戏,ecplise完整工程。代码完成的有点急了,仅供参考。感觉某些地方还是解决的比较经典的,碰巧想到了好方法
资源推荐
资源详情
资源评论
收起资源包目录
TryCube.rar (28个子文件)
TryCube
.project 539B
bin
playsound.class 906B
roll.WAV 17KB
cube_scale.class 2KB
CLICK.WAV 9KB
tryCube.class 2KB
M3GCanvas.class 15KB
cube_point.class 499B
.settings
org.eclipse.jdt.core.prefs 208B
TryCube.jad 221B
deployed
TryCube.jad 245B
TryCube.jar 33KB
.eclipseme.tmp
emulation
TryCube.jad 245B
TryCube.jar 33KB
verified
libs
classes
playsound.class 906B
cube_scale.class 2KB
tryCube.class 3KB
M3GCanvas.class 18KB
cube_point.class 499B
src
tryCube.java 3KB
cube_scale.java 2KB
cube_point.java 239B
playsound.java 549B
M3GCanvas.java 17KB
.eclipseme 241B
res
roll.WAV 17KB
CLICK.WAV 9KB
.classpath 242B
共 28 条
- 1
资源评论
- 夜影殇魂2013-05-29很好,可以运行,也学习到了不少,感谢楼主分享
- hdmnba2015-03-30可以运行,还行
jdzxcarl
- 粉丝: 2
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功