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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Etape 1 :
// Importation des packages Java 2
import java.applet.Applet;
import java.awt.*;
// Etape 2 :
// Importation des packages Java 3D
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Matrix3d;
import javax.vecmath.Vector3f;
public class test extends Applet {
public test() {
this.setLayout(new BorderLayout());
// Etape 3 :
// Creation du Canvas 3D
Canvas3D canvas3D = new Canvas3D(SimpleUniverse
.getPreferredConfiguration());
this.add(canvas3D, BorderLayout.CENTER);
// Etape 4 :
// Creation d'un objet SimpleUniverse
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// Etape 5 :
// Positionnement du point d'observation pour avoir une vue correcte de
// la
// scene 3D
TransformGroup oTransformGroup = simpleU.getViewingPlatform().getViewPlatformTransform();
float Zcamera = (float) 10.5;
Transform3D oTransform3D = new Transform3D();
oTransform3D.setTranslation(new Vector3f(0.0f, 0.0f, Zcamera));
oTransformGroup.setTransform(oTransform3D);
// Etape 6 :
// Creation de la scene 3D qui contient tous les objets 3D que l'on veut
// visualiser
BranchGroup scene = createSceneGraph();
// Etape 7 :
// Compilation de la scene 3D
scene.compile();
// Etape 8:
// Attachement de la scene 3D a l'objet SimpleUniverse
simpleU.addBranchGraph(scene);
}
/**
* Creation de la scene 3D qui contient tous les objets 3D
*
* @return scene 3D
*/
public BranchGroup createSceneGraph() {
// Creation de l'objet parent qui contiendra tous les autres objets 3D
BranchGroup parent = new BranchGroup();
// Creation du groupe de transformation sur lequel vont s'appliquer les
// comportements
TransformGroup mouseTransform = new TransformGroup();
// Le groupe de transformation sera modifie par le comportement de la
// souris
mouseTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
mouseTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// Creation comportement rotation a la souris
MouseRotate rotate = new MouseRotate(mouseTransform);
rotate.setSchedulingBounds(new BoundingSphere());
parent.addChild(rotate);
// Creation comportement deplacement a la souris
MouseTranslate translate = new MouseTranslate(mouseTransform);
translate.setSchedulingBounds(new BoundingSphere());
parent.addChild(translate);
// Creation comportement zoom a la souris
MouseZoom zoom = new MouseZoom(mouseTransform);
zoom.setSchedulingBounds(new BoundingSphere());
parent.addChild(zoom);
// Creation des cylindres
Appearance app = new Appearance();
app.setColoringAttributes(new ColoringAttributes(new Color3f(0.3f,
0.2f, 1.0f), ColoringAttributes.SHADE_GOURAUD));
Appearance app1 = new Appearance();
app1.setColoringAttributes(new ColoringAttributes(new Color3f(1f,
1f, 1f), ColoringAttributes.SHADE_GOURAUD));
Cylinder cylinder1 = new Cylinder(1f, 0.03f, 0, 80, 80, app);
Cylinder cylinder2 = new Cylinder(1f, 0.03f, 0, 80, 80, app1);
// rotation1
Transform3D oRotate1 = new Transform3D();
oRotate1.rotX(-Math.PI / 2);
TransformGroup oTGrotate1 = new TransformGroup(oRotate1);
// rotation2
Transform3D oRotate2 = new Transform3D();
oRotate2.setRotation(new Matrix3d(0.28f, 0.35f, -0.02f, -0.10f, -0.83f,
-0.12f, -0.02f, -0.41f, -0.27f));
TransformGroup oTGrotate2 = new TransformGroup(oRotate2);
// translation
Transform3D oTranslate = new Transform3D();
oTranslate.setTranslation(new Vector3f(0.35f, 0.83f, 0.41f));
TransformGroup oTGtranslate = new TransformGroup(oTranslate);
// Ajout des cylindres au graphe de la scene
mouseTransform.addChild(oTGtranslate);
mouseTransform.addChild(oTGrotate1);
oTGtranslate.addChild(oTGrotate2);
oTGrotate1.addChild(cylinder1);
oTGrotate2.addChild(cylinder2);
parent.addChild(mouseTransform);
return parent;
}
/**
* Etape 9 : Methode main() nous permettant d'utiliser cette classe comme
* une applet ou une application.
*
* @param args
* arguments de la ligne de commande
*/
public static void main(String[] args) {
new MainFrame(new test(), 512, 512);
}
} |