package elte.java2_utikalauz5.java3d; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import java.awt.image.PixelGrabber; import javax.vecmath.*; import javax.media.j3d.*; /** 3D kockák megjelenítése egy ablakban.
@link.forrásfájl {@docRoot}/../data/java3d/src RasterDemo.java @link.letöltés {@docRoot}/../data/java3d RasterDemo.jar @since Java 2 Útikalauz programozóknak 5.0 */ class RasterDemo extends Frame { int width1 = 100; int height1 = 50; int width2 = 100; int height2 = 50; MyImageComponent2D myImage1 = new MyImageComponent2D("egy_100x50.jpg",width1,height1, BufferedImage.TYPE_INT_RGB,ImageComponent.FORMAT_RGB8); MyImageComponent2D myImage2 = new MyImageComponent2D("ketto_100x50.jpg",width2,height2, BufferedImage.TYPE_INT_RGB,ImageComponent.FORMAT_RGB8); /** Verziószám */ private final static long serialVersionUID = 15L; public static void main(String[] args) { new RasterDemo(); } public RasterDemo() { super("RasterDemo"); 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(300,270); 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(); Transform3D tr1 = new Transform3D(); tr1.set(1,new Vector3d(0.0,1.3,0.0)); Transform3D tr2 = new Transform3D(); tr2.set(1,new Vector3d(0.0,-1.3,0.0)); TransformGroup transformGroup1 = new TransformGroup(tr1); TransformGroup transformGroup2 = new TransformGroup(tr2); Raster raster1 = new Raster(new Point3f(1.0f,1.0f,-1.0f), Raster.RASTER_COLOR, 0, 0,width1,height1, myImage1.getImageComponent2D(), null); Raster raster2 = new Raster(new Point3f(1.0f,1.0f,-1.0f), Raster.RASTER_COLOR, 0, 0,width2,height2, myImage2.getImageComponent2D(), null); transformGroup1.addChild(new Shape3D(raster1)); transformGroup1.addChild(new ColorCube().getShape()); transformGroup2.addChild(new Shape3D(raster2)); transformGroup2.addChild(new ColorCube().getShape()); branchGroup.addChild(transformGroup1); branchGroup.addChild(transformGroup2); return branchGroup; } class UniverseBuilder { Locale locale; UniverseBuilder(Canvas3D c) { Transform3D t = new Transform3D(); Transform3D t2 = new Transform3D(); t2.setEuler( new Vector3d(-30.0*(Math.PI/180.0),45.0*(Math.PI/180.0),0.0)); t.set(4,new Vector3d(7.0,5.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; } } }