public static void main(String args[]) { JFrame frame = new JFrame("Box and Sphere"); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); canvas.setSize(400, 400); SimpleUniverse universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); BranchGroup group = new BranchGroup(); group.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); // group.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); universe.addBranchGraph(group); frame.setSize(500, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(canvas, BorderLayout.CENTER); frame.setVisible(true); Sphere sphere = new Sphere(1.1f, Sphere.GENERATE_NORMALS | Sphere.GENERATE_TEXTURE_COORDS, null); BranchGroup b = new BranchGroup(); b.addChild(sphere); group.addChild(b); { Sphere sphere2 = new Sphere(1.0f, Sphere.GENERATE_NORMALS | Sphere.GENERATE_TEXTURE_COORDS, null); b = new BranchGroup(); b.addChild(sphere2); Util3D.enablePicking(b); group.addChild(b); } }
public BufferedImage getRenderedImage(Dimension d, boolean withBackground) { if (offscreenCanvas == null) { offscreenCanvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration(), true); } offscreenCanvas.setSize(getSize()); offscreenCanvas.getScreen3D().setSize(getScreen3D().getSize()); offscreenCanvas.getScreen3D().setPhysicalScreenWidth(getScreen3D().getPhysicalScreenWidth()); offscreenCanvas.getScreen3D().setPhysicalScreenHeight(getScreen3D().getPhysicalScreenHeight()); BufferedImage bufImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); ImageComponent2D imgComp = new ImageComponent2D(ImageComponent2D.FORMAT_RGBA, bufImage); BufferedImage transparentImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); ImageComponent2D transparentComp = new ImageComponent2D(ImageComponent2D.FORMAT_RGBA, transparentImage); Color3f tempColor = new Color3f(); background.getColor(tempColor); if (!withBackground) { background.setImage(transparentComp); } view.addCanvas3D(offscreenCanvas); offscreenCanvas.setOffScreenBuffer(imgComp); offscreenCanvas.renderOffScreenBuffer(); offscreenCanvas.waitForOffScreenRendering(); if (!withBackground) { background.setImage(null); } bufImage = offscreenCanvas.getOffScreenBuffer().getImage(); // To release the reference of buffer inside Java 3D. offscreenCanvas.setOffScreenBuffer(null); view.removeCanvas3D(offscreenCanvas); return bufImage; }