Original Post
Hi folks, i''m trying to use a ColorInterpolator in my Java3D program as far as I can see I''ve created everything but for some reason that I''m not sure of it doesn''t work. Heres the code hopefully someone can see something that I can''t!!!
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.*; // Added to allow images to be loaded
import javax.media.j3d.*;
import javax.swing.*;
import javax.vecmath.*;
import java.awt.*;
import java.awt.event.*;
public class SolarSystem extends JFrame
{
JPanel jpanel = new JPanel();
Canvas3D drawingCanvas;
BasicUniverse universe;
BranchGroup scene;
DirectionalLight directionalLight;
TextureLoader loader;
Texture texture;
TextureAttributes texAttr;
Appearance ap;
int primflags;
/**
* Set up colours and spheres
*/
Color3f blue3f = new Color3f(Color.blue);
Color3f white3f = new Color3f(Color.white);
Color3f black3f = new Color3f(Color.black);
Color3f green3f = new Color3f(Color.green);
Color3f yellow3f = new Color3f(Color.yellow);
Color3f red3f = new Color3f(Color.red);
Sphere sun,earth,mars;
Box box;
public static void main(String args[])
{
SolarSystem solar = new SolarSystem();
solar.setVisible(true);
}
/**
* The constructor for the solarsysytem initialises the 3d scene
*/
SolarSystem()
{
/**
* This section of code sets up the viewing window, with parameters
* such as the size and position on the screen
*/
WindowHandler windowHandler= new WindowHandler();
this.addWindowListener(windowHandler);
setTitle("Solar System");
getContentPane().setLayout(new BorderLayout(0,0));
setSize(800,600);
jpanel.setLayout(null);
getContentPane().add(BorderLayout.CENTER, jpanel);
jpanel.setBounds(0,0,800,600);
GraphicsConfiguration graphicsConfig =
SimpleUniverse.getPreferredConfiguration();
drawingCanvas = new Canvas3D(graphicsConfig);
jpanel.add(drawingCanvas);
universe= new BasicUniverse(drawingCanvas, 5.0f);
scene = new BranchGroup();
setupLightSources();
scene.addChild(directionalLight);
setupTextures();
/**
* This intialises two different Sphere objects
* of given radius and appearance i.e. the constuctor takes the form
* Sphere(float radius, Appearance appearance)
*/
sun = new Sphere(0.5f, createAppearance(yellow3f));
earth = new Sphere(0.15f, createAppearance(blue3f));
mars = new Sphere(0.10f, createAppearance(red3f)); //Line added for part one
box = new Box(0.10f, 0.10f, 0.10f, primflags, ap); //Line added for part two
/**
* Set''s up the transforms, i.e. rotation for the earth sphere
*/
Transform3D groupTrans = new Transform3D();
TransformGroup earthTransform = new TransformGroup();
groupTrans.
setTranslation(new Vector3f(0.0f, -0.20f, 1.5f));
earthTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
earthTransform.setTransform(groupTrans);
earthTransform.addChild(earth);
/**
* mars stuff
*/
Transform3D marsgroupTrans = new Transform3D();
TransformGroup marsTransform = new TransformGroup();
marsgroupTrans.
setTranslation(new Vector3f(0.0f, -0.40f, 3.0f));
marsTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
marsTransform.setTransform(marsgroupTrans);
marsTransform.addChild(mars);
/**
* box stuff
*/
Transform3D boxgroupTrans = new Transform3D();
TransformGroup boxTransform = new TransformGroup();
boxgroupTrans.
setTranslation(new Vector3f(0.0f, -0.30f, 2.25f));
boxTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
boxTransform.setTransform(boxgroupTrans);
boxTransform.addChild(box);
/**
* Set up a TransformGroup for earth sphere object
* and give a time varying alpha class, which takes the
* following form Alpha(int loopCount, long increasingAlphaDuration
*
* i.e. loopCount = -1 and increasingAlphaDuration = 10000
* The loopCount specifies the number of times to run the alpha object
* in this case since it is -1 it will run indefinitely. The increasingAlphaDuration
* specifies the time in milliseconds in which alpha goes from zero to one.
* So in this case ten seconds
*/
TransformGroup earthRotTrans = new TransformGroup();
earthRotTrans.setCapability(
TransformGroup.ALLOW_TRANSFORM_WRITE);
Alpha alphaEarth = new Alpha(-1, 10000);
/**
* This is similar to the above, except in this case we are giving
* the mars sphere a longer orbit of the sun
*/
TransformGroup marsRotTrans = new TransformGroup();
marsRotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Alpha alphamars = new Alpha(-1, 17000);
TransformGroup boxRotTrans = new TransformGroup();
boxRotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Alpha alphabox = new Alpha(-1, 8000);
/**
* This section defines the rotation of the earth object. It initialises
* a bounding sphere for which the animation should take place, this takes
* the form BoundingSphere(Point3d centre, double radius)
*
* It also sets up a RotationInterpolator based upon the time varying alpha
* class defined above, and the rotation transform of the earth.
*
* Once this is done it adds the appropriate transforms to the scene and
* TransformGroup
*/
BoundingSphere behaveBounds =
new BoundingSphere(new Point3d(0,0,0), 10);
RotationInterpolator rotIntEarth =
new RotationInterpolator(alphaEarth, earthRotTrans);
rotIntEarth.setSchedulingBounds(behaveBounds);
RotationInterpolator rotIntmars =
new RotationInterpolator(alphamars, marsRotTrans);
rotIntmars.setSchedulingBounds(behaveBounds);
RotationInterpolator rotIntbox =
new RotationInterpolator(alphabox, boxRotTrans);
rotIntbox.setSchedulingBounds(behaveBounds);
/**
* This piece of code sets up a ColorInterpolator for the earth sphere
* as part of the requirement of Part 1. This type of interpolator allows
* a behaviour to be defined which alternates between two different colours
*/
TransformGroup earthColTrans = new TransformGroup();
earthColTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Alpha colorAlpha = new Alpha(-1, 10000);
ColorInterpolator colIntEarth = new ColorInterpolator(colorAlpha,
earth.getAppearance().getMaterial(),blue3f,white3f);
colIntEarth.setSchedulingBounds(behaveBounds);
/**
* This section adds the transforms created to the scene and transformation groups
*/
scene.addChild(rotIntEarth);
scene.addChild(rotIntmars);
scene.addChild(rotIntbox);
scene.addChild(colIntEarth);
earthRotTrans.addChild(earthTransform);
// earthColTrans.addChild(earthTransform);
marsRotTrans.addChild(marsTransform);
boxRotTrans.addChild(boxTransform);
scene.addChild(earthRotTrans);
// scene.addChild(earthColTrans);
scene.addChild(marsRotTrans);
scene.addChild(boxRotTrans);
scene.addChild(sun);
universe.addBranchGraph(scene);
drawingCanvas.setBounds(0, 0, 800, 600);
}
/**
* Used to initialise an appearance
*/
private Appearance createAppearance(Color3f color)
{
Appearance appear = new Appearance();
Material material = new Material();
material.setDiffuseColor(color);
appear.setMaterial(material);
return appear;
}
/**
* Function to initialise light sources
*/
private void setupLightSources()
{
directionalLight = new DirectionalLight();
directionalLight.setEnable(true);
directionalLight.setInfluencingBounds
(new BoundingSphere(new Point3d(), 500.0));
}
private void setupColours()
{
}
private void setupTextures()
{
loader = new TextureLoader("borgcube23.gif","LUMINANCE", new Container());
texture = loader.getTexture();
texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
texture.setBoundaryColor( new Color4f( 0.0f, 1.0f, 0.0f, 0.0f ) );
texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
ap = new Appearance();
ap.setTexture(texture);
ap.setTextureAttributes(texAttr);
//set up the material
ap.setMaterial(new Material(green3f, black3f, green3f, black3f, 1.0f));
primflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;
}
void exitApplication()
{
this.setVisible(false);
this.dispose();
System.exit(0);
}
class WindowHandler extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
Object object = event.getSource();
if (object == SolarSystem.this)
exitApplication();
}
}
}