How to Insert an Image in a 3D Model Using Java
- 1). Open a text editor or word processor and paste into a new document the java program at the end of this step.
The program constructs the image (as all java 3-D programs do) by constructing a "scene graph," which you can visualize on paper as a simple tree-like structure with parent and child objects. The top parent of a scene is the BranchGroup object, under which the program attaches (through the parentOfScene.addChild function call) the geometry of a simple model, a plane. The ApplyImageToModelProgram function creates components to complete the 3-D scene, including the canvas, (class "Canvas3D"), the universe (class "SimpleUniverse") and the viewer's position (class "ViewingPlatform").
The program code under the comment "Add image to model" creates an "Appearance" object, which applies a graphic file to the model.
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.*;
import javax.vecmath.*;
public class ApplyImageToModelProgram extends Applet {
private static final Point3d USERPOSITION = new Point3d(5,2,7);
BranchGroup makeTheScene() {
BranchGroup parentOfScene = new BranchGroup();
QuadArray simpleModel = new QuadArray(4, GeometryArray.COORDINATES
| GeometryArray.TEXTURE_COORDINATE_2);
Point3f vertex = new Point3f(-3.0f, 3.0f, 0.0f);
simpleModel.setCoordinate(0, vertex);
vertex.set(-3.0f, -3.0f, 0.0f);
simpleModel.setCoordinate(1, vertex);
vertex.set(3.0f, -3.0f, 0.0f);
simpleModel.setCoordinate(2, vertex);
vertex.set(3.0f, 3.0f, 0.0f);
simpleModel.setCoordinate(3, vertex);
TexCoord2f texturePoint = new TexCoord2f( 0.0f, 1.0f);
simpleModel.setTextureCoordinate(0, 0, texturePoint);
texturePoint.set(0.0f, 0.0f);
simpleModel.setTextureCoordinate(0, 1, texturePoint);
texturePoint.set(1.0f, 0.0f);
simpleModel.setTextureCoordinate(0, 2, texturePoint);
texturePoint.set(1.0f, 1.0f);
simpleModel.setTextureCoordinate(0, 3, texturePoint);
//Add image to model
Appearance objectsAppearance = new Appearance();
String textureFilename = "someImage.gif";
TextureLoader imageloader = new TextureLoader(textureFilename, null);
ImageComponent2D the_image = imageloader.getImage();
if(the_image == null) {
System.out.println("The image load failed.");
}
Texture2D objectsTexture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
the_image.getWidth(), the_image.getHeight());
objectsTexture.setImage(0, the_image);
objectsAppearance.setTexture(objectsTexture);
objectsAppearance.setTransparencyAttributes(
new TransparencyAttributes(TransparencyAttributes.FASTEST, 0.1f));
//Create actual model
Shape3D planeObj = new Shape3D(simpleModel, objectsAppearance);
parentOfScene.addChild(planeObj);
return parentOfScene;
}
public ApplyImageToModelProgram (){
setLayout(new BorderLayout());
GraphicsConfiguration gfxConfig =
SimpleUniverse.getPreferredConfiguration();
Canvas3D theCanvas = new Canvas3D(gfxConfig);
add("Center", theCanvas);
theCanvas.setStereoEnable(false);
SimpleUniverse universe = new SimpleUniverse(theCanvas);
// move the viewpoint
ViewingPlatform viewPlatform = universe.getViewingPlatform();
TransformGroup viewPlatTransGroup = viewPlatform.getViewPlatformTransform();
Transform3D camTform = new Transform3D();
viewPlatTransGroup.getTransform (camTform);
camTform.lookAt( USERPOSITION, new Point3d(0,0,0), new Vector3d(0,1,0));
camTform.invert();
viewPlatTransGroup.setTransform(camTform);
universe.addBranchGraph(makeTheScene());
}
public static void main(String argv[])
{
new MainFrame(new ApplyImageToModelProgram(), 256, 256);
}
} - 2). Make a GIF image whose dimensions are a power of two by using an image-processing program. For example, 8 by 8 (two to the third power), 16 by 16 (two to the fourth power), 256 by 256, (two to the eighth power). Save the image as "someimage.gif" in the same folder as your java file.
- 3). Save the file as "ApplyImageToModelProgram.java," with file type set to "Plain text," to ensure that the compiler can read the program code.
- 4). Paste the following HTML document (which is needed to run the program) into a new text document. Save the document with the name "ApplyImageToModelProgram.html," in the same folder as the java source file.
<html>
<applet code="ApplyImageToModelProgram.class"
codebase="." Width=500 height=500>
</applet>
</html> - 5). Open a DOS command prompt in the folder containing the java file that you saved in Step 2. Compile the program with "javac ApplyImageToModelProgra.java."
- 6). Run the program with this command:
Appletviewer ApplyImageToModelProgram.HTML
The Appletviewer program will display the model with the image from the "someImage.gif" file.
Source...