1://see Java 3D API Tutorial p. 2-5
   2:
   3:import java.awt.*;
   4:import javax.media.j3d.*;
   5:import javax.vecmath.*;
   6:import com.sun.j3d.utils.image.TextureLoader;
   7:
   8:
   9:public class AppearanceTestCube extends Shape3D {
  10:
  11:        private Geometry TestGeometry;
  12:        private Appearance TestAppearance;
  13:        
  14:
  15:        public AppearanceTestCube() {
  16:                
  17:                TestGeometry = createGeometry();
  18:                TestAppearance = createAppearance();
  19:                
  20:                this.setGeometry(TestGeometry);
  21:                this.setAppearance(TestAppearance);
  22:                
  23:                //this.setCapability(this.ALLOW_APPEARANCE_WRITE);
  24:        }
  25:
  26:        private Geometry createGeometry() {
  27:
  28:                float[] verts = {
  29:                // Front Face
  30:                                1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  31:                                -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
  32:                // Back Face
  33:                                -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
  34:                                1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
  35:                // Right Face
  36:                                1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f,
  37:                                1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f,
  38:                // Left Face
  39:                                -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
  40:                                -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
  41:                // Top Face
  42:                                1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
  43:                                -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
  44:                // Bottom Face
  45:                                -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f,
  46:                                1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f,
  47:                                };
  48:
  49:                float[] normals = {
  50:                // Front Face
  51:                                0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  52:                                0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  53:                // Back Face
  54:                                0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,
  55:                                0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,
  56:                // Right Face
  57:                                1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  58:                                1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  59:                // Left Face
  60:                                -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
  61:                                -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
  62:                // Top Face
  63:                                0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  64:                                0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  65:                // Bottom Face
  66:                                0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
  67:                                0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
  68:                                };
  69:
  70:                float[] textCoords = {
  71:                // Front Face
  72:                                1.0f, 0.0f, 1.0f, 1.0f,
  73:                                0.0f, 1.0f, 0.0f, 0.0f,
  74:                // Back Face
  75:                                1.0f, 0.0f, 1.0f, 1.0f,
  76:                                0.0f, 1.0f, 0.0f, 0.0f,
  77:                // Right Face
  78:                                1.0f, 0.0f, 1.0f, 1.0f,
  79:                                0.0f, 1.0f, 0.0f, 0.0f,
  80:                // Left Face
  81:                                1.0f, 0.0f, 1.0f, 1.0f,
  82:                                0.0f, 1.0f, 0.0f, 0.0f,
  83:                // Top Face
  84:                                1.0f, 0.0f, 1.0f, 1.0f,
  85:                                0.0f, 1.0f, 0.0f, 0.0f,
  86:                // Bottom Face
  87:                                1.0f, 0.0f, 1.0f, 1.0f,
  88:                                0.0f, 1.0f, 0.0f, 0.0f
  89:                                };
  90:                QuadArray quadArray = new QuadArray(24, QuadArray.COORDINATES |
  91:                                QuadArray.NORMALS |
  92:                                QuadArray.TEXTURE_COORDINATE_2);
  93:                quadArray.setCoordinates(0, verts);
  94:                quadArray.setNormals(0, normals);
  95:                quadArray.setTextureCoordinates(0, 0, textCoords);
  96:
  97:                return quadArray;
  98:        }
  99:
 100:
 101:
 102:        private Appearance createAppearance() {
 103:                
 104:                //Define colors
 105:                //These are RGB values, eg 1.0f, 0.0f, 0.0f is bright red
 106:                Color3f black        = new Color3f(0.0f,  0.0f,  0.0f);
 107:                Color3f red          = new Color3f(0.8f,  0.2f,  0.2f);
 108:                Color3f ambientRed   = new Color3f(0.7f,  0.05f, 0.3f);
 109:                Color3f specular     = new Color3f(0.7f,  0.4f,  0.4f);
 110:
 111:                //The Material class defines the appearance of an object under illumination.
 112:                //Fields in the constructor are ambientColor, emissiveColor, diffuseColor, specularColor and shininess
 113:                Material redMaterial = new Material(ambientRed, black, red, specular, 75.0f);
 114:                //Material also enables or disables lighting
 115:                redMaterial.setLightingEnable(true);
 116:                //create an Appeance object
 117:                Appearance redAppearance = new Appearance();
 118:                //set the Material
 119:                redAppearance.setMaterial(redMaterial);
 120:                //Other attributes can also be set, eg Tranparancy
 121:                
 122:                return redAppearance;
 123:        }
 124:
 125:}
 126:
previous  start  toc  next