|
//
//CLASS
//ExBluePrint - illustrate use of background images
//
//LESSON
//Add a Background node to place a background image of a blueprint
//behind foreground geometry of a mechanical part.
//
//SEE ALSO
//ExBackgroundImage
//
//AUTHOR
//David R. Nadeau / San Diego Supercomputer Center
//
import java.applet.Applet;
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.Enumeration;
import java.util.EventListener;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Group;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Light;
import javax.media.j3d.LineAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Switch;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleStripArray;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.media.j3d.WakeupOr;
import javax.vecmath.Color3f;
import javax.vecmath.Matrix4d;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.PlatformGeometry;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.Viewer;
import com.sun.j3d.utils.universe.ViewingPlatform;
public class ExBluePrint extends Java3DFrame {
//--------------------------------------------------------------
// SCENE CONTENT
//--------------------------------------------------------------
//
// Nodes (updated via menu)
//
private Background background = null;
private Switch shadingSwitch = null;
//
// Build scene
//
public Group buildScene() {
// Get the current image
ImageComponent2D image = imageComponents[currentImage];
// Build the scene root
Group scene = new Group();
// BEGIN EXAMPLE TOPIC
// Create application bounds
BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0,
0.0), // Center
1000.0); // Extent
// Set the background color and its application bounds
background = new Background();
background.setColor(White);
background.setImage(image);
background.setCapability(Background.ALLOW_IMAGE_WRITE);
background.setApplicationBounds(worldBounds);
scene.addChild(background);
// END EXAMPLE TOPIC
// Build foreground geometry
scene.addChild(buildGadget());
return scene;
}
//--------------------------------------------------------------
// FOREGROUND AND ANNOTATION CONTENT
//--------------------------------------------------------------
//
// Build a mechanical gadget including a few gears and a
// shaft going through them.
//
private Group buildGadget() {
if (debug)
System.err.println(" gadget...");
//
// Create two appearances:
// wireframeApp: draw as blue wireframe
// shadedApp: draw as metalic shaded polygons
//
// Wireframe:
// no Material - defaults to coloring attributes color
// polygons as lines, with backfaces
// thick lines
Appearance wireframeApp = new Appearance();
ColoringAttributes wireframeCatt = new ColoringAttributes();
wireframeCatt.setColor(0.0f, 0.2559f, 0.4213f);
wireframeCatt.setShadeModel(ColoringAttributes.SHADE_FLAT);
wireframeApp.setColoringAttributes(wireframeCatt);
PolygonAttributes wireframePatt = new PolygonAttributes();
wireframePatt.setPolygonMode(PolygonAttributes.POLYGON_LINE);
wireframePatt.setCullFace(PolygonAttributes.CULL_NONE);
wireframeApp.setPolygonAttributes(wireframePatt);
LineAttributes wireframeLatt = new LineAttributes();
wireframeLatt.setLineWidth(2.0f);
wireframeApp.setLineAttributes(wireframeLatt);
// Shaded:
// silver material
Appearance shadedApp = new Appearance();
Material shadedMat = new Material();
shadedMat.setAmbientColor(0.30f, 0.30f, 0.30f);
shadedMat.setDiffuseColor(0.30f, 0.30f, 0.50f);
shadedMat.setSpecularColor(0.60f, 0.60f, 0.80f);
shadedMat.setShininess(0.10f);
shadedApp.setMaterial(shadedMat);
ColoringAttributes shadedCatt = new ColoringAttributes();
shadedCatt.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
shadedApp.setColoringAttributes(shadedCatt);
//
// Create a switch group to hold two versions of the
// shape: one wireframe, and one shaded
//
Transform3D tr = new Transform3D();
tr.set(new Vector3f(-1.0f, 0.2f, 0.0f));
TransformGroup gadget = new TransformGroup(tr);
shadingSwitch = new Switch();
shadingSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
Group wireframe = new Group();
Group shaded = new Group();
shadingSwitch.addChild(wireframe);
shadingSwitch.addChild(shaded);
shadingSwitch.setWhichChild(1); // shaded
gadget.addChild(shadingSwitch);
//
// Build a gear (wireframe and shaded)
//
tr = new Transform3D();
tr.rotY(Math.PI / 2.0);
TransformGroup tg = new TransformGroup(tr);
SpurGear gear = new SpurGearThinBody(24, // tooth count
1.6f, // pitch circle radius
0.3f, // shaft radius
0.08f, // addendum
0.05f, // dedendum
0.3f, // gear thickness
0.28f, // tooth tip thickness
wireframeApp);// appearance
tg.addChild(gear);
wireframe.addChild(tg);
tg = new TransformGroup(tr);
gear = new SpurGearThinBody(24, // tooth count
1.6f, // pitch circle radius
0.3f, // shaft radius
0.08f, // addendum
0.05f, // dedendum
0.3f, // gear thickness
0.28f, // tooth tip thickness
shadedApp); // appearance
tg.addChild(gear);
shaded.addChild(tg);
//
// Build another gear (wireframe and shaded)
//
tr.rotY(Math.PI / 2.0);
Vector3f trans = new Vector3f(-0.5f, 0.0f, 0.0f);
tr.setTranslation(trans);
tg = new TransformGroup(tr);
gear = new SpurGearThinBody(30, // tooth count
2.0f, // pitch circle radius
0.3f, // shaft radius
0.08f, // addendum
0.05f, // dedendum
0.3f, // gear thickness
0.28f, // tooth tip thickness
wireframeApp);// appearance
tg.addChild(gear);
wireframe.addChild(tg);
tg = new TransformGroup(tr);
gear = new SpurGearThinBody(30, // tooth count
2.0f, // pitch circle radius
0.3f, // shaft radius
0.08f, // addendum
0.05f, // dedendum
0.3f, // gear thickness
0.28f, // tooth tip thickness
shadedApp); // appearance
tg.addChild(gear);
shaded.addChild(tg);
//
// Build a cylindrical shaft (wireframe and shaded)
//
tr.rotZ(-Math.PI / 2.0);
trans = new Vector3f(1.0f, 0.0f, 0.0f);
tr.setTranslation(trans);
tg = new TransformGroup(tr);
Cylinder cyl = new Cylinder(0.3f, // radius
4.0f, // length
Primitive.GENERATE_NORMALS, // format
16, // radial resolution
1, // length-wise resolution
wireframeApp);// appearance
tg.addChild(cyl);
wireframe.addChild(tg);
tg = new TransformGroup(tr);
cyl = new Cylinder(0.3f, // radius
4.0f, // length
Primitive.GENERATE_NORMALS, // format
16, // radial resolution
1, // length-wise resolution
shadedApp); // appearance
tg.addChild(cyl);
shaded.addChild(tg);
//
// Build shaft teeth (wireframe and shaded)
//
tr.rotY(Math.PI / 2.0);
trans = new Vector3f(2.05f, 0.0f, 0.0f);
tr.setTranslation(trans);
tg = new TransformGroup(tr);
gear = new SpurGear(12, // tooth count
0.5f, // pitch circle radius
0.3f, // shaft radius
0.05f, // addendum
0.05f, // dedendum
1.5f, // gear thickness
0.8f, // tooth tip thickness
wireframeApp);// appearance
tg.addChild(gear);
wireframe.addChild(tg);
tg = new TransformGroup(tr);
gear = new SpurGear(12, // tooth count
0.5f, // pitch circle radius
0.3f, // shaft radius
0.05f, // addendum
0.05f, // dedendum
1.5f, // gear thickness
0.8f, // tooth tip thickness
shadedApp); // appearance
tg.addChild(gear);
shaded.addChild(tg);
return gadget;
}
//--------------------------------------------------------------
// USER INTERFACE
//--------------------------------------------------------------
//
// Main
//
public static void main(String[] args) {
ExBluePrint ex = new ExBluePrint();
ex.initialize(args);
ex.buildUniverse();
ex.showFrame();
}
// Image menu choices
private NameValue[] images = { new NameValue("None", null),
new NameValue("Blueprint", "blueprint.jpg"), };
private int currentImage = 0;
private ImageComponent2D[] imageComponents;
private CheckboxMenuItem[] imageMenu;
private int currentAppearance = 0;
private CheckboxMenuItem[] appearanceMenu;
//
// Initialize the GUI (application and applet)
//
public void initialize(String[] args) {
// Initialize the window, menubar, etc.
super.initialize(args);
exampleFrame.setTitle("Java 3D Blueprint Example");
//
// Add a menubar menu to change parameters
// (images)
// --------
// Wireframe
// Shaded
//
// Add a menu to select among background and shading options
Menu m = new Menu("Options");
imageMenu = new CheckboxMenuItem[images.length];
for (int i = 0; i < images.length; i++) {
imageMenu[i] = new CheckboxMenuItem(images[i].name);
imageMenu[i].addItemListener(this);
imageMenu[i].setState(false);
m.add(imageMenu[i]);
}
imageMenu[currentImage].setState(true);
m.addSeparator();
appearanceMenu = new CheckboxMenuItem[2];
appearanceMenu[0] = new CheckboxMenuItem("Wireframe");
appearanceMenu[0].addItemListener(this);
appearanceMenu[0].setState(false);
m.add(appearanceMenu[0]);
appearanceMenu[1] = new CheckboxMenuItem("Shaded");
appearanceMenu[1].addItemListener(this);
appearanceMenu[1].setState(true);
m.add(appearanceMenu[1]);
exampleMenuBar.add(m);
// Preload background images
TextureLoader texLoader = null;
imageComponents = new ImageComponent2D[images.length];
String value = null;
for (int i = 0; i < images.length; i++) {
value = (String) images[i].value;
if (value == null) {
imageComponents[i] = null;
continue;
}
texLoader = new TextureLoader(value, this);
imageComponents[i] = texLoader.getImage();
}
}
//
// Handle checkboxes
//
public void itemStateChanged(ItemEvent event) {
Object src = event.getSource();
// Check if it is an image choice
for (int i = 0; i < imageMenu.length; i++) {
if (src == imageMenu[i]) {
// Update the checkboxes
imageMenu[currentImage].setState(false);
currentImage = i;
imageMenu[currentImage].setState(true);
// Set the background image
ImageComponent2D image = imageComponents[currentImage];
background.setImage(image);
return;
}
}
// Check if it is an appearance choice
if (src == appearanceMenu[0]) {
appearanceMenu[1].setState(false);
shadingSwitch.setWhichChild(0);
return;
}
if (src == appearanceMenu[1]) {
appearanceMenu[0].setState(false);
shadingSwitch.setWhichChild(1);
return;
}
// Handle all other checkboxes
super.itemStateChanged(event);
}
}
/*
* @(#)SpurGearThinBody.java 1.3 98/02/20 14:29:59
*
* Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear facility.
* Licensee represents and warrants that it will not use or redistribute the
* Software for such purposes.
*/
class SpurGearThinBody extends SpurGear {
/**
* Construct a SpurGearThinBody;
*
* @return a new spur gear that conforms to the input paramters
* @param toothCount
* number of teeth
* @param pitchCircleRadius
* radius at center of teeth
* @param shaftRadius
* radius of hole at center
* @param addendum
* distance from pitch circle to top of teeth
* @param dedendum
* distance from pitch circle to root of teeth
* @param gearThickness
* thickness of the gear
*/
public SpurGearThinBody(int toothCount, float pitchCircleRadius,
float shaftRadius, float addendum, float dedendum,
float gearThickness) {
this(toothCount, pitchCircleRadius, shaftRadius, addendum, dedendum,
gearThickness, gearThickness, 0.25f, null);
}
/**
* Construct a SpurGearThinBody;
*
* @return a new spur gear that conforms to the input paramters
* @param toothCount
* number of teeth
* @param pitchCircleRadius
* radius at center of teeth
* @param shaftRadius
* radius of hole at center
* @param addendum
* distance from pitch circle to top of teeth
* @param dedendum
* distance from pitch circle to root of teeth
* @param gearThickness
* thickness of the gear
* @param look
* the gear's appearance
*/
public SpurGearThinBody(int toothCount, float pitchCircleRadius,
float shaftRadius, float addendum, float dedendum,
float gearThickness, Appearance look) {
this(toothCount, pitchCircleRadius, shaftRadius, addendum, dedendum,
gearThickness, gearThickness, 0.25f, look);
}
/**
* Construct a SpurGearThinBody;
*
* @return a new spur gear that conforms to the input paramters
* @param toothCount
* number of teeth
* @param pitchCircleRadius
* radius at center of teeth
* @param shaftRadius
* radius of hole at center
* @param addendum
* distance from pitch circle to top of teeth
* @param dedendum
* distance from pitch circle to root of teeth
* @param gearThickness
* thickness of the gear
* @param toothTipThickness
* thickness of the tip of the tooth
* @param look
* the gear's appearance
*/
public SpurGearThinBody(int toothCount, float pitchCircleRadius,
float shaftRadius, float addendum, float dedendum,
float gearThickness, float toothTipThickness, Appearance look) {
this(toothCount, pitchCircleRadius, shaftRadius, addendum, dedendum,
gearThickness, toothTipThickness, 0.25f, look);
}
/**
* Construct a SpurGearThinBody;
*
* @return a new spur gear that conforms to the input paramters
* @param toothCount
* number of teeth
* @param pitchCircleRadius
* radius at center of teeth
* @param shaftRadius
* radius of hole at center
* @param addendum
* distance from pitch circle to top of teeth
* @param dedendum
* distance from pitch circle to root of teeth
* @param gearThickness
* thickness of the gear
* @param toothTipThickness
* thickness of the tip of the tooth
* @param toothToValleyRatio
* ratio of tooth valley to circular pitch (must be <= .25)
* @param look
* the gear's appearance object
*/
public SpurGearThinBody(int toothCount, float pitchCircleRadius,
float shaftRadius, float addendum, float dedendum,
float gearThickness, float toothTipThickness,
float toothToValleyAngleRatio, Appearance look) {
this(toothCount, pitchCircleRadius, shaftRadius, addendum, dedendum,
gearThickness, toothTipThickness, 0.25f, look,
0.6f * gearThickness, 0.75f * (pitchCircleRadius - shaftRadius));
}
/**
* Construct a SpurGearThinBody;
*
* @return a new spur gear that conforms to the input paramters
* @param toothCount
* number of teeth
* @param pitchCircleRadius
* radius at center of teeth
* @param shaftRadius
* radius of hole at center
* @param addendum
* distance from pitch circle to top of teeth
* @param dedendum
* distance from pitch circle to root of teeth
* @param gearThickness
* thickness of the gear
* @param toothTipThickness
* thickness of the tip of the tooth
* @param toothToValleyRatio
* ratio of tooth valley to circular pitch (must be <= .25)
* @param look
* the gear's appearance object
* @param bodyThickness
* the thickness of the gear body
* @param crossSectionWidth
* the width of the depressed portion of the gear's body
*/
public SpurGearThinBody(int toothCount, float pitchCircleRadius,
float shaftRadius, float addendum, float dedendum,
float gearThickness, float toothTipThickness,
float toothToValleyAngleRatio, Appearance look,
float bodyThickness, float crossSectionWidth) {
super(toothCount, pitchCircleRadius, addendum, dedendum,
toothToValleyAngleRatio);
float diskCrossSectionWidth = (rootRadius - shaftRadius - crossSectionWidth) / 2.0f;
float outerShaftRadius = shaftRadius + diskCrossSectionWidth;
float innerToothRadius = rootRadius - diskCrossSectionWidth;
// Generate the gear's body disks, first by the shaft, then in
// the body and, lastly, by the teeth
addBodyDisks(shaftRadius, outerShaftRadius, gearThickness, look);
addBodyDisks(innerToothRadius, rootRadius, gearThickness, look);
addBodyDisks(outerShaftRadius, innerToothRadius, bodyThickness, look);
// Generate the gear's "shaft" equivalents the two at the teeth
// and the two at the shaft
addCylinderSkins(innerToothRadius, gearThickness, InwardNormals, look);
addCylinderSkins(outerShaftRadius, gearThickness, OutwardNormals, look);
// Generate the gear's interior shaft
addCylinderSkins(shaftRadius, gearThickness, InwardNormals, look);
// Generate the gear's teeth
addTeeth(pitchCircleRadius, rootRadius, outsideRadius, gearThickness,
toothTipThickness, toothToValleyAngleRatio, look);
}
}
/*
* @(#)SpurGear.java 1.12 98/02/20 14:29:58
*
* Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT
|