package elte.java2_utikalauz5.java3d; import java.awt.*; import java.awt.event.*; import javax.vecmath.*; import javax.media.j3d.*; /** 3D transzformációk szemléltetése. @link.forrásfájl {@docRoot}/../data/java3d/src TransformDemo.java @link.letöltés {@docRoot}/../data/java3d TransformDemo.jar @since Java 2 Útikalauz programozóknak 5.0 */ class TransformDemo extends Frame implements ActionListener { Transform3D tr1 = new Transform3D ( ); Transform3D tr2 = new Transform3D ( ); Transform3D tr3 = new Transform3D ( ); Transform3D tr4 = new Transform3D ( ); TransformGroup transformGroup = new TransformGroup(); /** Verziószám */ private final static long serialVersionUID = 15L; public static void main(String[] args) { new TransformDemo(); } public TransformDemo() { super("TransformDemo"); enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK); GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(new GraphicsConfigTemplate3D()); Canvas3D c = new Canvas3D(config); add(c); MenuBar menuBar = new MenuBar(); Menu menu = new Menu("Transzformáció"); MenuItem m1 = new MenuItem("Identitás"); MenuItem m2 = new MenuItem("Eltolás"); MenuItem m3 = new MenuItem("Forgatás"); MenuItem m4 = new MenuItem("Skálázás"); m1.addActionListener(this); m2.addActionListener(this); m3.addActionListener(this); m4.addActionListener(this); menu.add(m1); menu.add(m2); menu.add(m3); menu.add(m4); menuBar.add(menu); setMenuBar(menuBar); UniverseBuilder u = new UniverseBuilder(c); BranchGroup scene = createSceneGraph(); u.addBranchGraph(scene); setSize(300,300); 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() { tr2.set( new Vector3d( 2., 0., 0. ) ); tr3.set( new AxisAngle4d( 0., 1., 0., Math.PI/4. ) ); tr4.set( 2.0 ); BranchGroup branchGroup = new BranchGroup(); transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); transformGroup.addChild(new ColorCube().getShape()); branchGroup.addChild(transformGroup); return branchGroup; } public void actionPerformed( ActionEvent e) { if (e.getActionCommand().compareTo("Identitás")==0) transformGroup.setTransform(tr1); if (e.getActionCommand().compareTo("Eltolás")==0) transformGroup.setTransform(tr2); if (e.getActionCommand().compareTo("Forgatás")==0) transformGroup.setTransform(tr3); if (e.getActionCommand().compareTo("Skálázás")==0) transformGroup.setTransform(tr4); } class UniverseBuilder { Locale locale; UniverseBuilder(Canvas3D c) { Transform3D t = new Transform3D(); Transform3D t2 = new Transform3D(); t2.setEuler( new Vector3d(-35.0*(Math.PI/180.0),45.0*(Math.PI/180.0),0.0)); t.set(4,new Vector3d(6.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 extends Object { 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; } } }