An Object : Object Model « 3D « Java






An Object

An Object
// From: http://www.micg.et.fh-stralsund.de/Java3D/java3D.htm#Bild1


import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class FarbWurfelAsQuads extends Applet {

  /**
   * init Methoden fur die Darstellung als Applet
   */
  public void init() {
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();
    canvas3D = new Canvas3D(config);
    add("Center", canvas3D);
    BranchGroup szene = macheSzene();
    szene.compile();
    universe = new SimpleUniverse(canvas3D);
    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(szene);
  }

  /**
   * Erstellt den Szenegraphen
   * 
   * @return BranchGroup
   */
  public BranchGroup macheSzene() {
    BranchGroup objWurzel = new BranchGroup();
    // Transformation, 2 Rotationen:
    Transform3D drehung = new Transform3D();
    Transform3D drehung2 = new Transform3D();
    drehung.rotX(Math.PI / 4.0d);
    drehung2.rotY(Math.PI / 5.0d);
    drehung.mul(drehung2);
    drehung.setScale(0.5d);
    TransformGroup objDreh = new TransformGroup(drehung);
    Shape3D shape = new Shape3D(makeCube(), makeAppearance());
    objDreh.addChild(shape);

    objWurzel.addChild(objDreh);
    return objWurzel;
  }

  /**
   * Erstellung des ColorCube
   * 
   * @return
   */
  private QuadArray makeCube() {
    QuadArray plane = new QuadArray(24, GeometryArray.COORDINATES
        | GeometryArray.COLOR_3);
    // Punkte und Farben
    Point3f pa = new Point3f(-1.0f, 1.0f, -1.0f);
    Point3f pb = new Point3f(-1.0f, -1.0f, -1.0f);
    Point3f pc = new Point3f(1.0f, -1.0f, -1.0f);
    Point3f pd = new Point3f(1.0f, 1.0f, -1.0f);
    Point3f pe = new Point3f(-1.0f, 1.0f, 1.0f);
    Point3f pf = new Point3f(-1.0f, -1.0f, 1.0f);
    Point3f pg = new Point3f(1.0f, -1.0f, 1.0f);
    Point3f ph = new Point3f(1.0f, 1.0f, 1.0f);
    // Coloreigenschaften
    Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
    Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
    Color3f cyan = new Color3f(0.0f, 1.0f, 1.0f);
    Color3f yellow = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f magenta = new Color3f(1.0f, 0.0f, 1.0f);

    // 1. Seite
    plane.setCoordinate(0, pa);
    plane.setColor(0, red);
    plane.setCoordinate(1, pb);
    plane.setColor(1, red);
    plane.setCoordinate(2, pc);
    plane.setColor(2, red);
    plane.setCoordinate(3, pd);
    plane.setColor(3, red);
    // 2. Seite
    plane.setCoordinate(4, pe);
    plane.setColor(4, blue);
    plane.setCoordinate(5, pf);
    plane.setColor(5, blue);
    plane.setCoordinate(6, pg);
    plane.setColor(6, blue);
    plane.setCoordinate(7, ph);
    plane.setColor(7, blue);
    // 3. Seite
    plane.setCoordinate(8, pe);
    plane.setColor(8, green);
    plane.setCoordinate(9, pf);
    plane.setColor(9, green);
    plane.setCoordinate(10, pb);
    plane.setColor(10, green);
    plane.setCoordinate(11, pa);
    plane.setColor(11, green);
    // 4. Seite
    plane.setCoordinate(12, pd);
    plane.setColor(12, cyan);
    plane.setCoordinate(13, pc);
    plane.setColor(13, cyan);
    plane.setCoordinate(14, pg);
    plane.setColor(14, cyan);
    plane.setCoordinate(15, ph);
    plane.setColor(15, cyan);
    // 5. Seite
    plane.setCoordinate(16, pe);
    plane.setColor(16, yellow);
    plane.setCoordinate(17, pa);
    plane.setColor(17, yellow);
    plane.setCoordinate(18, pd);
    plane.setColor(18, yellow);
    plane.setCoordinate(19, ph);
    plane.setColor(19, yellow);
    // 6. Seite
    plane.setCoordinate(20, pf);
    plane.setColor(20, magenta);
    plane.setCoordinate(21, pb);
    plane.setColor(21, magenta);
    plane.setCoordinate(22, pc);
    plane.setColor(22, magenta);
    plane.setCoordinate(23, pg);
    plane.setColor(23, magenta);
    // ubergib den Wurfel
    return plane;
  }

  /**
   * Wurfeldarstellung
   * 
   * @return Appearance
   */
  private Appearance makeAppearance() {
    Appearance a = new Appearance();
    PolygonAttributes p = new PolygonAttributes();
    p.setCullFace(PolygonAttributes.CULL_NONE);
    a.setPolygonAttributes(p);
    return a;
  }

  /**
   * gibt speicher frei
   */
  public void destroy() {
    universe.removeAllLocales();
  }

  public static void main(String[] args) {
    frame = new MainFrame(new FarbWurfelAsQuads(), 500, 500);
    frame.setTitle("ColorCube mittels Geometrieklassen");
  }

  //---- Attribute -----------------------
  private SimpleUniverse universe;

  private Canvas3D canvas3D;

  private static Frame frame;
}

           
       








Related examples in the same category

1.Line TypesLine Types
2.Shape: Point outlineShape: Point outline
3.Color YoyoColor Yoyo
4.Yoyo LineYoyo Line
5.The use of the GeometryInfo class and related classesThe use of the GeometryInfo class and related classes
6.Example SwitchExample Switch
7.A Morph object to animate a shape between two key shapesA Morph object to animate a shape between two key shapes
8.ExHenge - create a stone-henge like (vaguely) mysterious temple thing
9.Appearance Is Everything
10.Geometry By ReferenceGeometry By Reference
11.Stereo Girl
12.Red Green GirlRed Green Girl
13.Red Green GriffinRed Green Griffin
14.cg viewer
15.A basic hierarchical model of the top part of a human torsoA basic hierarchical model of the top part of a human torso
16.A large hollow box
17.Java 3D Box and a custom Cuboid implementationJava 3D Box and a custom Cuboid implementation
18.A simple class using the an indexed quadrilateral arrayA simple class using the an indexed quadrilateral array
19.Simple Indexed Quad NormalsSimple Indexed Quad Normals
20.Simple Indexed QuadSimple Indexed Quad
21.Twist Strip visual objectTwist Strip visual object
22.Door AppDoor App
23.ShadowApp creates a single planeShadowApp creates a single plane
24.TeapotTeapot
25.MorphingMorphing
26.VolRend
27.GouraudGouraud