import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class game0320 extends Frame implements Runnable{
SimpleUniverse universe;
BranchGroup group1,group2;
TransformGroup transform_group1;
Transform3D transform1;
float x,y,z;
float cx,cy,cz;
float lx,ly,lz;
public static void main(String [] args) {
Frame f=new game0320();
f.setTitle("game0320");
f.setSize(700,700);
f.setBackground(Color.yellow);
f.setVisible(true);
}
game0320(){
x=0.0f;
y=0.0f;
z=0.0f;
cx=1.4f;
cy=1.4f;
cz=2.0f;
lx=0.2f;
ly=-0.2f;
lz=-0.8f;
space3d();
cube3d(x,y,z);
camera3d(cx,cy,cz);
light3d(lx,ly,lz);
this.setVisible(true);
Thread th=new Thread(this);
th.start();
addWindowListener(new stopwin());
}
class stopwin extends WindowAdapter{
public void windowClosing(WindowEvent we){System.exit(0);}
}
public void run(){
int t;
t=0;
while(t<100){
x=x+0.01f;
y=y+0.01f;
z=z+0.00f;
cube3d(x,y,z);
try{
Thread.sleep(100);
}catch(InterruptedException e){}
t=t+1;
}
}
void space3d(){
Panel cp = new Panel();
cp.setLayout(null);
this.add(cp);
GraphicsConfiguration g_config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(g_config);
canvas.setBounds(0,0,700,700);
cp.add(canvas);
universe = new SimpleUniverse(canvas);
}
void cube3d(float x,float y,float z){
group1 = new BranchGroup();
ColorCube cube = new ColorCube(0.3f);
transform_group1 = new TransformGroup();
transform_group1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transform_group1.addChild(cube);
transform1 = new Transform3D();
group1.addChild(transform_group1);
universe.addBranchGraph(group1);
transform1.set(new Vector3d(x,y,z));
transform_group1.setTransform(transform1);
}
void camera3d(float cx,float cy,float cz){
ViewingPlatform vp = universe.getViewingPlatform();
TransformGroup Camera = vp.getViewPlatformTransform();
Transform3D view_pos = new Transform3D();
Vector3f pos_vec = new Vector3f(cx,cy,cz);
view_pos.setTranslation(pos_vec);
Transform3D view_dir = new Transform3D();
Transform3D view_dir2 = new Transform3D();
view_dir.rotY(Math.PI/4);
view_dir2.rotX(-Math.PI/4 + 0.1f);
view_dir.mul(view_dir2);
view_pos.mul(view_dir);
Camera.setTransform(view_pos);
}
void light3d(float lx,float ly,float lz){
Color3f light_color = new Color3f(1.7f,1.7f,1.7f);
Vector3f light_direction = new Vector3f(lx,ly,lz);
DirectionalLight light = new DirectionalLight(light_color,light_direction);
BoundingSphere bounds = new BoundingSphere();
light.setInfluencingBounds(bounds);
BranchGroup group2 = new BranchGroup();
group2.addChild(light);
universe.addBranchGraph(group2);
}
}
最終更新:2011年02月19日 03:25