mysimpleuniverse.java
来自「java2参考大全上的例子的源码和自己的理解.」· Java 代码 · 共 1,951 行 · 第 1/5 页
JAVA
1,951 行
break;
default:
System.out.println("无效的操作");
}
someBlockNewToOld();
}
//开始人工智能计算解法
//select:选择电脑的计算方案
public static void autoStart(int select) {
}
//输出每个位置上的块号
public static void showPosition() {
System.out.println("\n每个位置上的块号:");
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
for (int k = 0; k <= 2; k++) {
//System.out.println("Block"+i+","+j+","+k+" "+blockArray[i][j][k].x+","+blockArray[i][j][k].y+","+blockArray[i][j][k].z);
System.out.println("Position:" + (i - 1) + "," + (j - 1) + "," +
(k - 1) + " 的块号是: " +
(positionArray[i][j][k].x - 1) + "," +
(positionArray[i][j][k].y - 1) + "," +
(positionArray[i][j][k].z - 1));
}
}
}
}
//把刚才记录的有变化(来了新的块)的位置 存储到 没个position的块号中
public static void someBlockNewToOld() {
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
for (int k = 0; k <= 2; k++) {
if (positionArray[i][j][k].haveNew) {
positionArray[i][j][k].newToOld();
}
}
}
}
}
public static void myWait() {
try {
System.in.read();
System.in.read();
//暂停
}
catch (Exception e) {
}
}
//测试用代码
public static void someTest() {
doIt('Y', 0 + 1, 90);
// //new MoFang().showPosition();
MoFang.myWait();
doIt('Y', 0 + 1, -90);
MoFang.myWait();
// new MoFang().showPosition();
doIt('Z', -1 + 1, -90);
//new MoFang().showPosition();
MoFang.myWait();
doIt('Z', -1 + 1, 90);
// new MoFang().showPosition();
//MoFang.myWait();
//new MoFang().showPosition();
doIt('X', 1 + 1, 90);
MoFang.myWait();
//new MoFang().showPosition();
doIt('X', 1 + 1, -90);
// new MoFang().showPosition();
MoFang.myWait();
//showPosition();
}
//魔方自动随机变化多少条
public static void ranGet(int num) {
char selectChar = 'E'; //'X','Y','Z'
int layer; //-1,0,1
int jiaoDu = 0; //90,-19,180
for (int i = 0; i < num; i++) {
//选xyz
int select = (int) ( (Math.random() * 10) % 3);
if (select == 0) {
selectChar = 'X';
}
if (select == 1) {
selectChar = 'Y';
}
if (select == 2) {
selectChar = 'Z';
}
//
layer = (int) ( (Math.random() * 10) % 3); //0,1,2
layer -= 1; //-1,0,1
//
int jiao = (int) ( (Math.random() * 10) % 3); //0,1,2
if (jiao == 0) {
jiaoDu = 90;
}
if (jiao == 1) {
jiaoDu = -90;
}
if (jiao == 2) {
jiaoDu = 180 - 90;
}
System.out.println("\n*******************************\nRandom Generater:" +
(i + 1) + " of " + num);
System.out.print(selectChar);
System.out.print("," + layer + "," + jiaoDu + "\n按任意键开始动画?\n");
myWait();
doIt(selectChar, layer + 1, jiaoDu);
}
}
//从三维图形界面输入操作参数
public static void graphicStart() {
JFrame myframe = new JFrame();
myframe.setVisible(true);
}
public static void main(String[] args) {
//初始化位置数组,也可以从文件中加载,其实应该把所有关键信息从文件载入,关闭时保存
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
for (int k = 0; k <= 2; k++) {
MoFang.positionArray[i][j][k] = new Position(i, j, k);
}
}
}
System.out.println("初始化位置数组完成.最初全在原位置\n");
//showPosition();
new MainFrame(new mySimpleUniverse(), 200, 200);
//加applet到应用程序界面
//someTest();
//graphicStart();
myWait();
ranGet(30);
//
//
}
}
class Block
implements Runnable {
//计算已画的块数
private static int whickBlockPainted = 0;
//计算生成的第几块
private static int BlockCreated = 0;
//每面的颜色,排列为:+x,-x,+y,-y,+z,-z,no
public static Color3f[] mianColor = {
new Color3f(1.0f, 0.0f, 0.0f),
new Color3f(0.0f, 1.0f, 0.0f),
new Color3f(0.0f, 0.0f, 1.0f),
new Color3f(1.0f, 1.0f, 0.0f),
new Color3f(1.0f, 0.0f, 1.0f),
new Color3f(0.0f, 1.0f, 1.0f),
new Color3f(0.2f, 0.2f, 0.2f)
};
//每面的材质,排列为:+x,-x,+y,-y,+z,-z,no
public static String[] mianImageFile = new String[7];
//块偏移量
private static float kuaiZhongXinWeizhi = 0.4f;
//创建材质时要用的,仅用于他,他是一个applet对象
Component observer;
//该块的编号
private int blockIdX;
private int blockIdY;
private int blockIdZ;
//该块的位置
private int x;
private int y;
private int z;
//该块的坐标轴点,最初全为(1,1,1),表示与外坐标一致
private MyPoint xvec;
private MyPoint yvec;
private MyPoint zvec;
//position=new Verctor3f(0.0f,0.0f,0.0f);
//该块的角度,限定为从-359到359,模360可以限定为一周,如果模180,则只能表示半个圆,不行
//int anglex ;
//int angley ;
//int anglez ;
//添加物品的变换组
public TransformGroup transGroup;
public Transform3D trans;
//用于位置,角度变动的变换组
public TransformGroup transGroupx;
public Transform3D transx;
public TransformGroup transGroupy;
public Transform3D transy;
public TransformGroup transGroupz;
public Transform3D transz;
public TransformGroup transGroupp;
public Transform3D transp;
//新的方块位置
int[] nexyz = {
0, 0, 0};
//动画相关
int totateArg;
boolean canNew = false;
char selectedC;
Thread myThread;
public static boolean closeDonghua = false; //为true使用线程动画,为false直接调用
private static int selectDonghuaId = 0; //取ID变量,取值从0~8,每次从0开始取,正好9个,取完后变为10(每取一个自动加1),这时才让所有线程一起动
private int myDonghuaId;
private static int donghuaDelay = 50; //绝对延迟,
public static int whileDelay = 20; //加快同步系统性能延迟,
//公共控制变量
public static int yunXingThread = 0; //运行的线程数,启动线程时加1,退出时减一。可以控制主线程等待,直到为0时才开始做所有的xyzChange,否则等待
//动画时的多线程要用它,因为主线程仍在跑,x,y,z会变化,只能用它保存
int yuanx;
int yuany;
int yuanz;
//当前位置x点的偏移
MyPoint chaxvec;
//当前位置y点的偏移
MyPoint chayvec;
//当前位置z点的偏移
MyPoint chazvec;
//构造函数,给初值
public Block(int i, int j, int k, int px, int py, int pz,
TransformGroup parentTransGroup, Transform3D t3d,
BranchGroup objRoot, Component obServer1) {
blockIdX = i;
blockIdY = j;
blockIdZ = k;
x = px;
y = py;
z = pz;
//计算轴向量 :块坐标加坐标偏移
xvec = new MyPoint( (blockIdX - 1) + 1, (blockIdY - 1), (blockIdZ - 1));
yvec = new MyPoint( (blockIdX - 1), (blockIdY - 1) + 1, (blockIdZ - 1));
zvec = new MyPoint( (blockIdX - 1), (blockIdY - 1), (blockIdZ - 1) + 1);
//System.out.println("轴点:"+(zoux)+(zouy)+(zouz));
//System.out.println("轴向量:"+(zoux-(x-1))+(zouy-(y-1))+(zouz-(z-1)));
observer = obServer1;
//anglex=0 ;
//angley=0 ;
//anglez=0 ;
trans = new Transform3D();
transGroup = new TransformGroup(trans);
transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
transGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
transp = new Transform3D();
transGroupp = new TransformGroup(transp);
transGroupp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transGroupp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//transGroupp.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
transx = new Transform3D();
transGroupx = new TransformGroup(transx);
transGroupx.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transGroupx.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//transGroupx.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
transy = new Transform3D();
transGroupy = new TransformGroup(transy);
transGroupy.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transGroupy.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//transGroupy.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
transz = new Transform3D();
transGroupz = new TransformGroup(transz);
transGroupz.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transGroupz.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//transGroupz.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
//用已知信息组装变换组,,
//注意这里的顺序,先平移,再旋转,或先x,先y的顺序是不一样的,这里
//必须是先在原位置自转好方向,再平移,这样,平移不会改变它的自转方向,
//如果先平移,再转,转动会带动平移,刚才的平移无任何意义。
//还要注意是先绕x转,再绕y转,再绕z转
//换了顺序,是不一样的,例如x,y,z和z,y,x的结果不一样
//但是,可以证明x,y和y,x的顺序无关性,当都只左转或右转90度时,很容易证明
//如果有180度,可以分成两次90度操作
parentTransGroup.addChild(transGroupp);
transGroupp.addChild(transGroupz);
transGroupz.addChild(transGroupy);
transGroupy.addChild(transGroupx);
transGroupx.addChild(transGroup);
// 测试,直接加到根,没有鼠标功能
//objRoot.addChild(transGroup);
//System.out.println("\n\n\n处理第"+BlockCreated+"块");
//System.out.println("块"+(blockIdX-1)+(blockIdY-1)+(blockIdZ-1)+"的坐标系添加完成");
BlockCreated++;
//if (BlockCreated==14)//||BlockCreated==3//||BlockCreated==2||BlockCreated==20||BlockCreated==16
add3DCube(x - 1, y - 1, z - 1, transGroup);
//建小坐标轴
SomeShape3D.zuoBiaoZhuSmallXShape3D(transGroup);
SomeShape3D.zuoBiaoZhuSmallYShape3D(transGroup);
SomeShape3D.zuoBiaoZhuSmallZShape3D(transGroup);
//测试主变换组的平移
//Shape3D shape1=SomeShape3D.FlowerShape3D();
//parentTransGroup.addChild(shape1);
//t3d.setTranslation(new Vector3f(0.0f,1.0f,0.0f));
//parentTransGroup.setTransform(t3d);
//测试子变换组的平移(不行,不起作用)
//Shape3D shape2=SomeShape3D.SanShape3D();
//transGroup.addChild(shape2);
//trans.setTranslation(new Vector3f(0.0f,-1f,0.0f));
//transGroup.setTransform(t3d);
//创建鼠标行为
//MouseRotate behavior1=new MouseRotate();
//behavior1.setTransformGroup(transGroup);
//behavior1.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),100));
//objRoot.addChild(behavior1);//鼠标行为 加到 分枝组
//设置初始值,显示输出
float fx;
float fy;
float fz;
fx = (float) kuaiZhongXinWeizhi * (x - 1);
fy = (float) kuaiZhongXinWeizhi * (y - 1);
fz = (float) kuaiZhongXinWeizhi * (z - 1);
//变换量
transx.rotX(Math.toRadians(0)); //anglex
transy.rotY(Math.toRadians(0));
transz.rotZ(Math.toRadians(0));
transp.setTranslation(new Vector3f(fx, fy, fz));
//生效
transGroupp.setTransform(transp);
transGroupx.setTransform(transx);
transGroupy.setTransform(transy);
transGroupz.setTransform(transz);
}
int[] jisuanNextXYZ(char doType, int totateArg, int oldx, int oldy, int oldz)
//需要变量x,y,z,doType,totateArg,输出返回到数组
{
//System.out.println("计算下一个点。。。");
//用于计算
int newz = 0;
int newy = 0;
int newx = 0;
//返回时的标准格式
int[] nextXYZ = {
0, 0, 0};
//计算时接收调用外面函数的返回函数值
int[] myShunShiNext = {
0, 0};
if (totateArg == 0) {
//当totateArg为0时,没有可用的if匹配,返回原位置
newx = oldx;
newy = oldy;
newz = oldz;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?