// // (c) 2017 Alexei A. Morozov // import javax.media.j3d.Alpha; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.GraphicsConfigTemplate3D; import javax.media.j3d.ImageComponent; import javax.media.j3d.ImageComponent2D; import javax.media.j3d.RotationInterpolator; import javax.media.j3d.Screen3D; import javax.media.j3d.TransformGroup; import javax.media.j3d.Transform3D; import javax.vecmath.Point3d; import com.sun.j3d.utils.geometry.ColorCube; import com.sun.j3d.utils.universe.SimpleUniverse; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.image.BufferedImage; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JScrollPane; public class OffScreenTest5 { public static void main(String argv[]) { JFrame frame= new JFrame(); frame.setTitle("Test Java3D"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(350,350)); JDesktopPane d= new JDesktopPane(); d.setBackground(Color.GREEN); frame.setContentPane(d); frame.setVisible(true); JInternalFrame i1= new JInternalFrame(); JInternalFrame i2= new JInternalFrame(); d.add(i1); d.add(i2); i1.setSize(new Dimension(150,150)); i2.setSize(new Dimension(150,150)); i2.setLocation(150,150); i1.setVisible(true); i2.setVisible(true); GraphicsConfiguration configuration= refineGraphicsConfiguration(i1.getGraphicsConfiguration()); OnScreenCanvas3D onScreenCanvas3D= new OnScreenCanvas3D(configuration); DemoCanvas demoCanvas= new DemoCanvas(configuration); OffScreenCanvas3D offScreenCanvas3D= new OffScreenCanvas3D(configuration); Screen3D screen1= onScreenCanvas3D.getScreen3D(); Screen3D screen2= offScreenCanvas3D.getScreen3D(); Dimension dimension= screen1.getSize(); screen2.setSize(dimension); screen2.setPhysicalScreenWidth(screen1.getPhysicalScreenWidth()); screen2.setPhysicalScreenHeight(screen1.getPhysicalScreenHeight()); SimpleUniverse u= onScreenCanvas3D.getUniverse(); u.getViewer().getView().addCanvas3D(offScreenCanvas3D); onScreenCanvas3D.setBackground(Color.MAGENTA); demoCanvas.setBackground(Color.GRAY); JScrollPane s1= new JScrollPane(onScreenCanvas3D); JScrollPane s2= new JScrollPane(demoCanvas); i1.add(s1); i2.add(s2); i1.revalidate(); for (int k=0; k < 1000000; k++) { BufferedImage b1= offScreenCanvas3D.renderImage(100,100); // System.out.printf("BufferedImage: %s\n",b1); // Image output is not necessary for this examination: demoCanvas.setImage(b1); } } public static GraphicsConfiguration refineGraphicsConfiguration(GraphicsConfiguration dialogGraphicsConfiguration) { GraphicsConfigTemplate3D gct3D= new GraphicsConfigTemplate3D(); gct3D.setSceneAntialiasing(GraphicsConfigTemplate3D.REQUIRED); GraphicsDevice device; if (dialogGraphicsConfiguration==null) { device= java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); } else { device= dialogGraphicsConfiguration.getDevice(); }; GraphicsConfiguration bestGraphicsConfiguration= device.getBestConfiguration(gct3D); return bestGraphicsConfiguration; } } class OnScreenCanvas3D extends Canvas3D { protected SimpleUniverse universe; public OnScreenCanvas3D(GraphicsConfiguration configuration) { super(configuration); BranchGroup scene= createSceneGraph(); universe= new SimpleUniverse(this,0); universe.getViewingPlatform().setNominalViewingTransform(); universe.addBranchGraph(scene); } public SimpleUniverse getUniverse() { return universe; } public BranchGroup createSceneGraph() { BranchGroup objRoot= new BranchGroup(); TransformGroup objTrans= new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); objTrans.addChild(new ColorCube(0.5)); Transform3D yAxis= new Transform3D(); Alpha rotationAlpha= new Alpha(-1,1000000); RotationInterpolator rotator= new RotationInterpolator( rotationAlpha,objTrans,yAxis,0.0f,(float)Math.PI*1000f); BoundingSphere bounds= new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); rotator.setSchedulingBounds(bounds); objRoot.addChild(rotator); objRoot.compile(); return objRoot; } } class OffScreenCanvas3D extends Canvas3D { public OffScreenCanvas3D(GraphicsConfiguration configuration) { super(configuration,true); } public BufferedImage renderImage(int width, int height) { if (width <= 0 || height <= 0) { return null; }; BufferedImage image= new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB); ImageComponent2D buffer= new ImageComponent2D(ImageComponent.FORMAT_RGBA,image); setOffScreenBuffer(buffer); renderOffScreenBuffer(); waitForOffScreenRendering(); image= getOffScreenBuffer().getImage(); return image; } public void postSwap() { } } class DemoCanvas extends Canvas { protected BufferedImage image; public DemoCanvas(GraphicsConfiguration configurartion) { super(configurartion); } public void setImage(BufferedImage i) { synchronized (this) { image= i; repaint(); } } public void paint(Graphics g) { super.paint(g); synchronized (this) { if (image != null) { g.drawImage(image,0,0,null); } } } }