FarbWurfelAsQuads.java Source code

Java tutorial

Introduction

Here is the source code for FarbWurfelAsQuads.java

Source

// 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;
}