您好我尝试将http://www.java2s.com/Code/Java/3D/OffScreenTest.htm改编为一个简单的测试,该测试会将彩色多维数据集呈现为文件。
但是我的img.png只包含黑色,程序永远不会退出。
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Screen3D;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class HelloJava3Db {
public static void main(String[] args) {
// create canvas
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config, true);
// create universe
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
// create scene
BranchGroup scene = new BranchGroup();
scene.addChild(new ColorCube(0.4));
scene.compile();
simpleU.addBranchGraph(scene);
// set off screen size
Screen3D sOff = canvas3D.getScreen3D();
sOff.setSize(new Dimension(200, 200));
sOff.setPhysicalScreenWidth(200);
sOff.setPhysicalScreenHeight(200);
// render to off screen
BufferedImage bImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
ImageComponent2D buffer = new ImageComponent2D(ImageComponent.FORMAT_RGBA, bImage);
buffer.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);
canvas3D.setOffScreenBuffer(buffer);
canvas3D.renderOffScreenBuffer();
canvas3D.waitForOffScreenRendering();
// write to file
File output = new File("img.png");
try {
ImageIO.write(bImage, "png", output);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(output.getAbsolutePath());
simpleU.cleanup();
}
}
答案 0 :(得分:1)
确定需要进行两项更改才能使其正常工作:
工作结果:
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Screen3D;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class HelloJava3Db {
public static void main(String[] args) {
// create canvas
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config, true);
// create universe
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
// create scene
BranchGroup scene = new BranchGroup();
scene.addChild(new ColorCube(0.4));
scene.compile();
simpleU.addBranchGraph(scene);
// set off screen size
Screen3D sOff = canvas3D.getScreen3D();
sOff.setSize(new Dimension(200, 200));
sOff.setPhysicalScreenWidth(1);
sOff.setPhysicalScreenHeight(1);
// render to off screen
BufferedImage bImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
ImageComponent2D buffer = new ImageComponent2D(ImageComponent.FORMAT_RGBA, bImage);
buffer.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);
canvas3D.setOffScreenBuffer(buffer);
canvas3D.renderOffScreenBuffer();
canvas3D.waitForOffScreenRendering();
// write to file
File output = new File("img.png");
try {
ImageIO.write(canvas3D.getOffScreenBuffer().getRenderedImage(), "png", output);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(output.getAbsolutePath());
simpleU.cleanup();
}
}