Hi Alessandro,
Thanks a lot. I did combine the several images/icons to be one now.
When I use this code to modify the sample code, it suppose to work. And I
can compile, but once run, show the:
"Exception in thread "Thread-2" java.lang.NullPointerException"
I think the problem is here: "texBufferedImage" should be the combined
image which is retured from the main().
Texture tex = new TextureLoader(java.awt.image.BufferedImage
texBufferedImage).getTexture();
According to the TextureLoader, it should be able to construct the object
through the "texBufferedImage", right ?
Below is the code:
===========================================================================
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.vecmath.*;
public class TextureImage extends Applet {
private java.net.URL texImage = null;
// new added
private BufferedImage texBufferedImage = null;
ImageComponent2D ic;
private SimpleUniverse u = null;
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
// Create appearance object for textured cube
Appearance app = new Appearance();
// Texture tex = new TextureLoader(texImage, this).getTexture();
Texture tex = new TextureLoader(java.awt.image.BufferedImage
texBufferedImage).getTexture();
// Added new one
//ImageComponent2D ic = new ImageComponent2D(ImageComponent.FORMAT_RGB,
texBufferedImage);
//ImageComponent2D ic = null ;
// ic.set(texBufferedImage);
//Texture2D myTexture = new Texture2D();
// myTexture.setImage(0, imgtest02);
// app.setTexture(myTexture);
app.setTexture(tex);
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
app.setTextureAttributes(texAttr);
// Create textured cube and add it to the scene graph.
Box textureCube = new Box(0.4f, 0.4f, 0.4f,
Box.GENERATE_TEXTURE_COORDS, app);
objTrans.addChild(textureCube);
// Create a new Behavior object that will perform the desired
// operation on the specified transform object and add it into
// the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0,
4000, 0, 0,
0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
// Have Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
public TextureImage() {
}
// public TextureImage(java.net.URL url) {
// texImage = url;
// }
// New added
public TextureImage(BufferedImage img) {
texBufferedImage = img;
ImageComponent2D ic = new ImageComponent2D(ImageComponent.FORMAT_RGB,
texBufferedImage);
// BufferedImage imgtest = ic.getImage();
BufferedImage imgtest = null;
if (imgtest == null)
{
System.out.println("I am here");
} ;
imgtest = ic.getImage();
if (imgtest == null)
{
System.out.println("not get the image to here");
}
else{
System.out.println("then get the image to here");
}
;
}
// public BufferedImage TextureBufferedImage(BufferedImage img) {
// texBufferedImage = img;
// }
public void init() {
if (texImage == null) {
// the path to the image for an applet
try {
texImage = new java.net.URL(getCodeBase().toString() +
"../images/1.jpg");
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
}
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph();
u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void destroy() {
u.cleanup();
}
//
// The following allows TextureImage to be run as an application
// as well as an applet
//
// public static void main(String[] args) {
public static void main(String[] args) throws IOException {
java.net.URL url = null;
if (args.length > 0) {
try {
url = new java.net.URL("file:" + args[0]);
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
}
else {
// the path to the image for an application
try {
url = new java.net.URL("file:../images/1.jpg");
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
}
// New added
URL[] urls = {
// new URL("file:/E:/3D/icon_example/images/1.jpg"),
new URL("file:../images/1.jpg"),
new URL("file:../images/2.jpg"),
new URL("file:../images/3.jpg"),
};
BufferedImage[] images = {
ImageIO.read(urls[0]),
ImageIO.read(urls[1]),
ImageIO.read(urls[2]),
};
int w = images[0].getWidth(), h = images[0].getHeight();
BufferedImage total = new BufferedImage(4*w, 4*h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = total.createGraphics();
g.drawImage(images[0], 0, 0, null);
g.drawImage(images[1], w, 0, null);
g.drawImage(images[2], 0, 2*h, null);
// new MainFrame(new TextureImage(url), 256, 256);
new MainFrame(new TextureImage(total), 256, 256);
}
}
> Check at JavaAlmanac http://www.javaalmanac.com/
> about same sample codes (search for image/images etc)
>
> Alessandro Borges <[EMAIL PROTECTED]> wrote:
> Hi, Ding
>
> By using TextureUnitState you can mix your textures at runtime and show
> then. Of course a alpha channel can be nice, but you can create a method
> to add alpha channel , as well mix alpha mask
>
> As Ben told, you can use Java2D to mix images into a single one. And with
> this new image (actually a BufferedImage).
>
> image1 + image2 + imagex -->
> single BufferedImage -> ImageComponent2D -> Texture2D
>
> Alessandro
>
> "H. Ding" <[EMAIL PROTECTED]> wrote:
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".