📄 texturebyreference.java
字号:
JLabel delayL = new JLabel("frame delay"); javax.swing.Box delayBox = new javax.swing.Box(BoxLayout.X_AXIS); delayBox.add(delayL); delayBox.add(frameDelay); animationB = new JButton(" stop animation "); animationB.addActionListener(this); JLabel texInfo1 = new JLabel("*To use ImageComponent by reference feature, use TYPE_4BYTE_ABGR on Solaris"); JLabel texInfo2 = new JLabel("and TYPE_3BYTE_BGR on Windows"); JPanel buttonP = new JPanel(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); buttonP.setLayout(gridbag); c.anchor = GridBagConstraints.CENTER; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(topBox, c); buttonP.add(topBox); gridbag.setConstraints(delayBox, c); buttonP.add(delayBox); gridbag.setConstraints(animationB, c); buttonP.add(animationB); gridbag.setConstraints(texInfo1, c); buttonP.add(texInfo1); gridbag.setConstraints(texInfo2, c); buttonP.add(texInfo2); return buttonP; } public BranchGroup createSceneGraph() { // create the root of the branch group BranchGroup objRoot = new BranchGroup(); // create the transform group node and initialize it // enable the TRANSFORM_WRITE capability so that it can be modified // at runtime. Add it to the root of the subgraph Transform3D rotate = new Transform3D(); TransformGroup objTrans = new TransformGroup(rotate); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); // bounds BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); // set up some light Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f); Vector3f lDir1 = new Vector3f(-1.0f, -0.5f, -1.0f); Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f); AmbientLight aLgt = new AmbientLight(alColor); aLgt.setInfluencingBounds(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); objRoot.addChild(aLgt); objRoot.addChild(lgt1); Appearance appearance = new Appearance(); // enable the TEXTURE_WRITE so we can modify it at runtime appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE); // load the first texture TextureLoader loader = new TextureLoader(urls[0], TextureLoader.BY_REFERENCE | TextureLoader.Y_UP, this); // get the texture from the loader Texture2D tex = (Texture2D)loader.getTexture(); // get the BufferedImage to convert to TYPE_4BYTE_ABGR and flip // get the ImageComponent because we need it anyway ImageComponent2D imageComp = (ImageComponent2D)tex.getImage(0); BufferedImage bImage = imageComp.getImage(); // convert the image bImage = ImageOps.convertImage(bImage, BufferedImage.TYPE_4BYTE_ABGR); // flip the image ImageOps.flipImage(bImage); imageComp.set(bImage); tex.setCapability(Texture.ALLOW_IMAGE_WRITE); tex.setBoundaryModeS(Texture.CLAMP); tex.setBoundaryModeT(Texture.CLAMP); tex.setBoundaryColor(1.0f, 1.0f, 1.0f, 1.0f); // set the image of the texture tex.setImage(0, imageComp); // set the texture on the appearance appearance.setTexture(tex); // set texture attributes TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.MODULATE); appearance.setTextureAttributes(texAttr); // set material properties Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); appearance.setMaterial(new Material(white, black, white, black, 1.0f)); // create a scale transform Transform3D scale = new Transform3D(); scale.set(.6); TransformGroup objScale = new TransformGroup(scale); objTrans.addChild(objScale); tetra = new Tetrahedron(true); tetra.setAppearance(appearance); objScale.addChild(tetra); // create the behavior animate = new AnimateTexturesBehavior(tex, urls, appearance, this); animate.setSchedulingBounds(bounds); objTrans.addChild(animate); // add a rotation behavior so we can see all sides of the tetrahedron Transform3D yAxis = new Transform3D(); Alpha rotorAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0); RotationInterpolator rotator = new RotationInterpolator(rotorAlpha, objTrans, yAxis, 0.0f, (float) Math.PI*2.0f); rotator.setSchedulingBounds(bounds); objTrans.addChild(rotator); // have java3d perform optimizations on this scene graph objRoot.compile(); return objRoot; } // callback for the animation button and delay text field public void actionPerformed(ActionEvent e) { Object o = e.getSource(); // for the animation button if (o == animationB) { if (animate.getEnable()) { animate.setEnable(false); animationB.setText("start animation"); } else { animate.setEnable(true); animationB.setText(" stop animation "); } } // for the texByRef button else if (o == texByRef && texByRef.isSelected()) { animate.setByReference(true); } // texByCopy button else if (o == texByCopy && texByCopy.isSelected()) { animate.setByReference(false); } // yUp button else if (o == yUp && yUp.isSelected()) { animate.setYUp(true); } // ydown button else if (o == yDown && yDown.isSelected()) { animate.setYUp(false); } //geomByRef button else if (o == geomByRef) { tetra.setByReference(true); } // geomByCopy button else if (o == geomByCopy) { tetra.setByReference(false); } // TYPE_INT_ARGB else if (o == imgIntARGB) { animate.setImageType(BufferedImage.TYPE_INT_ARGB); } // TYPE_4BYTE_ABGR else if (o == img4ByteABGR) { animate.setImageType(BufferedImage.TYPE_4BYTE_ABGR); } // TYPE_3BYTE_BGR else if (o == img3ByteBGR) { animate.setImageType(BufferedImage.TYPE_3BYTE_BGR); } // TYPE_CUSTOM RGBA else if (o == imgCustomRGBA) { animate.setImageTypeCustomRGBA(); } // TYPE_CUSTOM RGB else if (o == imgCustomRGB) { animate.setImageTypeCustomRGB(); } } // callback for the checkboxes public void itemStateChanged(ItemEvent e) { Object o = e.getSource(); // for the flip checkbox if (o == flipB) { if (e.getStateChange() == ItemEvent.DESELECTED) { animate.setFlipImages(false); } else animate.setFlipImages(true); } } // callback for the slider public void stateChanged(ChangeEvent e) { Object o = e.getSource(); // for the frame delay if (o == frameDelay) { animate.setFrameDelay(frameDelay.getValue()); } } // allows TextureByReference to be run as an application as well as an applet public static void main(String[] args) { java.net.URL fnames[] = null; if (args.length > 1) { fnames = new java.net.URL[args.length]; for (int i = 0; i < args.length; i++) { try { fnames[i] = new java.net.URL("file:" + args[i]); } catch (java.net.MalformedURLException ex) { System.out.println(ex.getMessage()); } } } else { fnames = new java.net.URL[TextureByReference.defaultFiles.length]; for (int i = 0; i < TextureByReference.defaultFiles.length; i++) { fnames[i] = Resources.getResource(defaultFiles[i]); if (fnames[i] == null) { System.err.println(TextureByReference.defaultFiles[i] + " not found"); System.exit(1); } /* try { fnames[i] = new java.net.URL("file:" + TextureByReference.defaultFiles[i]); } catch (java.net.MalformedURLException ex) { System.out.println(ex.getMessage()); System.exit(1); } */ } } new MainFrame((new TextureByReference(fnames)), 650, 750); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -