Using Raster Geometry
ExRaster.java
//
// CLASS
// ExRaster - illustrate use of rasters
//
// LESSON
// Add a Raster node to place an image on the screen based upon
// a transformed 3D coordinate
//
// AUTHOR
// Michael J. Bailey / San Diego Supercomputer Center
//
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.net.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
public class ExRaster
extends Example
{
// nodes that can be updated via a menu:
private Shape3D sh = null; // the 3D shape
private TextureLoader texLoader = null; // to load the image
private ImageComponent2D ic = null; // the image component
private Raster raster = null; // the raster object
// Build the scene:
public Group buildScene()
{
// Turn on the headlight
setHeadlightEnable( true );
// Build the scene root
Group scene = new Group();
// Create application bounds
BoundingSphere worldBounds = new BoundingSphere(
new Point3d( 0.0, 0.0, 0.0 ), // Center
1000.0 ); // Extent
raster = new Raster( );
raster.setPosition( new Point3f( 1.f, 0.f, 0.f ) );
raster.setType( Raster.RASTER_COLOR );
raster.setOffset( 0, 0 );
raster.setSize( 256, 256 );
raster.setImage( ic );
sh = new Shape3D( raster, new Appearance() );
scene.addChild( sh );
Appearance app0 = new Appearance( );
Material mat0 = new Material();
mat0.setAmbientColor( 0.8f, 0.4f, 0.2f );
mat0.setDiffuseColor( 0.8f, 0.4f, 0.2f );
mat0.setSpecularColor( 0.0f, 0.0f, 0.f );
app0.setMaterial( mat0 );
Transform3D t3d = new Transform3D( );
t3d.setTranslation( new Vector3f( 0.f, 0.f, 0.f ) );
TransformGroup tg0 = new TransformGroup( t3d );
Sphere sph0 = new Sphere( 0.5f,
Primitive.GENERATE_NORMALS, 16, app0 );
tg0.addChild( sph0 );
scene.addChild( tg0 );
t3d = new Transform3D( );
t3d.setTranslation( new Vector3f( 1.f, 0.f, 0.f ) );
tg0 = new TransformGroup( t3d );
sph0 = new Sphere( 0.5f,
Primitive.GENERATE_NORMALS, 16, app0 );
tg0.addChild( sph0 );
scene.addChild( tg0 );
t3d = new Transform3D( );
t3d.setTranslation( new Vector3f( 1.f, -1.f, 0.f ) );
tg0 = new TransformGroup( t3d );
sph0 = new Sphere( 0.5f,
Primitive.GENERATE_NORMALS, 16, app0 );
tg0.addChild( sph0 );
scene.addChild( tg0 );
t3d = new Transform3D( );
t3d.setTranslation( new Vector3f( 0.f, -1.f, 0.f ) );
tg0 = new TransformGroup( t3d );
sph0 = new Sphere( 0.5f,
Primitive.GENERATE_NORMALS, 16, app0 );
tg0.addChild( sph0 );
scene.addChild( tg0 );
return scene;
}
//
// Main (if invoked as an application)
//
public static void main( String[] args )
{
ExRaster ex = new ExRaster();
ex.initialize( args );
ex.buildUniverse();
ex.showFrame();
}
//
// Initialize the GUI (application and applet)
//
public void initialize( String[] args )
{
// Initialize the window, menubar, etc.
super.initialize( args );
exampleFrame.setTitle( "Java 3D Raster Example" );
// Preload the image
texLoader = new TextureLoader( "earth.jpg", this);
ic = texLoader.getImage();
if( ic == null )
{
System.err.println( "Cannot load 'earth.jpg'" );
}
}
//
// Handle checkboxes
//
public void itemStateChanged( ItemEvent event )
{
Object src = event.getSource();
// Handle all other checkboxes
super.itemStateChanged( event );
}
}