/* * Java3D Psuedo Transparent Window Hack * * kishan at hackorama dot com | www.hackoram.com | 2004/2005 */ import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.util.Date; import java.text.DateFormat; import javax.swing.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.geometry.ColorCube; import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.universe.*; public class Glass { private Rectangle baseRect = null; private Robot robot = null; private BufferedImage baseBuffer = null; private static final String J3DURL = "http://java.sun.com/" + "products/java-media/3D" ; public Glass() { try { robot = new Robot(); } catch (Exception e) { exitOnError(e, "Failed to create the Robot"); } int width = 500; int height = 200; Dimension sd = Toolkit.getDefaultToolkit().getScreenSize(); baseRect = new Rectangle(sd.width / 2 - width / 2, sd.height / 2 - height / 2, width, height); baseBuffer = robot.createScreenCapture(baseRect); GlassWin glass = null; try{ glass = new GlassWin(); } catch( Exception e){ exitOnError(e, "Please Instal Java3D from " + J3DURL ); } glass.show(); glass.setBounds(sd.width/2 - width/2, sd.height/2 - height/2, width, height); } public void exitOnError(Exception e, String msg) { System.out.println("\n\n-----------------------------------"); System.out.println("Glass Error: "+ msg); System.out.println("Glass Error: Exception Details"); System.out.println(e); System.out.println("------------------------------------\n\n"); System.exit(1); } class GlassWin extends JWindow { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); public GlassWin() { Canvas3D mycanvas = null; try{ mycanvas = new Canvas3D(config); } catch ( Exception e ) { exitOnError(e, "Looks like you dont have" + " a 3D Capable Display" ); } getContentPane().add(mycanvas, BorderLayout.CENTER); BranchGroup scene = createSceneGraph(); u = new SimpleUniverse(mycanvas); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public void update(Graphics g) { baseBuffer = robot.createScreenCapture(baseRect); } public void paint(Graphics g) { } private SimpleUniverse u = null; public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setScale(0.4); objScale.setTransform(t3d); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objScale.addChild(objTrans); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); Color3f fontColor = new Color3f(1.0f, 0.2f, 0.4f); ColoringAttributes ca = new ColoringAttributes(); ca.setColor(fontColor); Appearance fontapp = new Appearance(); Material mm = new Material(); mm.setLightingEnable(true); fontapp.setMaterial(mm); fontapp.setColoringAttributes(ca); Appearance app = new Appearance(); TransparencyAttributes ta = new TransparencyAttributes(); ta.setTransparencyMode(ta.BLENDED); ta.setTransparency(0.5f); app.setTransparencyAttributes(ta); PolygonAttributes pa = new PolygonAttributes(); pa.setCullFace(pa.CULL_NONE); app.setPolygonAttributes(pa); Color3f objColor = new Color3f(0.7f, 0.8f, 1.0f); app.setMaterial(new Material(objColor, black, objColor, black, 1.0f)); ColorCube myObject = new ColorCube(0.8); myObject.setAppearance(app); objTrans.addChild(myObject); Font3D f3d; DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); String textString = df.format(new Date()); double tessellation = 0.0; if (tessellation > 0.0) { f3d = new Font3D(new Font("TestFont", Font.PLAIN, 1), tessellation, new FontExtrusion()); } else { f3d = new Font3D(new Font("TestFont", Font.PLAIN, 1), new FontExtrusion()); } Text3D txt = new Text3D(f3d, textString, new Point3f(-2.2f, -0.35f, 0.0f)); Shape3D sh = new Shape3D(); sh.setGeometry(txt); sh.setAppearance(fontapp); objTrans.addChild(sh); Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, 4000); 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); TextureLoader bgTexture = new TextureLoader(baseBuffer); Background bg = new Background(bgTexture.getImage()); bg.setApplicationBounds(bounds); objRoot.addChild(bg); Color3f ambientColor = new Color3f(0.3f, 0.3f, 0.3f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f); Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction); light2.setInfluencingBounds(bounds); objRoot.addChild(light2); objRoot.compile(); return objRoot; } } public static void main(String args[]) { System.out.println("Glass - Psuedo Transparent 3D Window "); Glass myGlass = new Glass(); } }