package elte.java2_utikalauz5.java3d; import java.awt.*; import java.awt.image.*; import javax.vecmath.*; import javax.media.j3d.*; /** Háttérkép használatra példaprogram.

@link.forrásfájl {@docRoot}/../data/java3d/src BackgroundDemo.java @link.letöltés {@docRoot}/../data/java3d BackgroundDemo.jar @since Java 2 Útikalauz programozóknak 5.0 */ class BackgroundDemo extends Frame { int width=200; int height=126; MyImageComponent2D myImage = new MyImageComponent2D("sky_200x126.jpg",width,height, BufferedImage.TYPE_INT_RGB,ImageComponent.FORMAT_RGB8); /** Verziószám */ private final static long serialVersionUID = 15L; public static void main(String[] args) { new BackgroundDemo(); } public BackgroundDemo() { super("Direkt csúnya BackgroundDemo"); enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK); GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(new GraphicsConfigTemplate3D()); Canvas3D c = new Canvas3D(config); add(c); UniverseBuilder u = new UniverseBuilder(c); BranchGroup scene = createSceneGraph(); u.addBranchGraph(scene); setSize(width+100,height+100); setVisible(true); } /** Ablakesemény feldolgozása, bezáráskor kilépés. @param esemény A fellépett esemény */ @Override protected void processWindowEvent(java.awt.event.WindowEvent esemény) { if (esemény.getID() == esemény.WINDOW_CLOSING) System.exit(0); super.processWindowEvent( esemény ); } public BranchGroup createSceneGraph() { BranchGroup branchGroup = new BranchGroup(); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); Background background = new Background(myImage.getImageComponent2D()); background.setApplicationBounds(bounds); Transform3D transform3D = new Transform3D(); transform3D.set(0.6f); TransformGroup transformGroup = new TransformGroup(transform3D); transformGroup.addChild(new ColorCube().getShape()); branchGroup.addChild(transformGroup); branchGroup.addChild(background); return branchGroup; } class UniverseBuilder { Locale locale; UniverseBuilder(Canvas3D c) { Transform3D t = new Transform3D(); Transform3D t2 = new Transform3D(); t2.setEuler( new Vector3d(-15.0*(Math.PI/180.0),45.0*(Math.PI/180.0),0.0)); t.set(4,new Vector3d(6.0,3.0,6.0)); t.mul(t,t2); VirtualUniverse universe = new VirtualUniverse(); locale = new Locale(universe); PhysicalBody body = new PhysicalBody(); PhysicalEnvironment environment = new PhysicalEnvironment(); BranchGroup viewPlatformBranchGroup = new BranchGroup(); TransformGroup viewPlatformTransformGroup = new TransformGroup(t); ViewPlatform viewPlatform = new ViewPlatform(); View view = new View(); view.addCanvas3D(c); view.setPhysicalBody(body); view.setPhysicalEnvironment(environment); view.attachViewPlatform(viewPlatform); viewPlatformTransformGroup.addChild(viewPlatform); viewPlatformBranchGroup.addChild(viewPlatformTransformGroup); locale.addBranchGraph(viewPlatformBranchGroup); } void addBranchGraph(BranchGroup bg) { locale.addBranchGraph(bg); } } class ColorCube { double verts[] = { // elso lap 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, // hatso lap -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, // jobb lap 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, // bal lap -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, // fedo lap 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, // alaplap -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0 }; float[] colors = { // elso lap (piros) 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // hatso lap (zold) 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // jobb lap (kek) 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // bal lap (sarga) 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, // fedo lap (magenta) 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, // alaplap (cian) 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f }; private Shape3D shape; public ColorCube() { QuadArray cube = new QuadArray(24, QuadArray.COORDINATES | QuadArray.COLOR_3); cube.setCoordinates(0, verts); cube.setColors(0, colors); shape = new Shape3D(cube, new Appearance()); } public Shape3D getShape() { return shape; } } class MyImageComponent2D { ImageComponent2D imageComponent2D = null; public MyImageComponent2D(String fname, int w, int h, int format1, int format2) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage(getClass().getResource( fname )); BufferedImage bImage = new BufferedImage(w, h, format1); int[] intPixels =((DataBufferInt)bImage.getRaster().getDataBuffer()).getData(); PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, intPixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) {;} imageComponent2D = new ImageComponent2D(format2, bImage); imageComponent2D.setCapability(ImageComponent.ALLOW_SIZE_READ); imageComponent2D.setCapability(ImageComponent.ALLOW_FORMAT_READ); imageComponent2D.setCapability(ImageComponent.ALLOW_IMAGE_READ); } public ImageComponent2D getImageComponent2D() { return imageComponent2D; } } }