package elte.java2_utikalauz5.java3d; import java.awt.*; import javax.vecmath.*; import javax.media.j3d.*; /** Háromszögek megjelenítése egy ablakban. @link.forrásfájl {@docRoot}/../data/java3d/src TriangleArrayDemo.java @link.letöltés {@docRoot}/../data/java3d TriangleArrayDemo.jar @since Java 2 Útikalauz programozóknak 5.0 */ class TriangleArrayDemo extends Frame { /** Verziószám */ private final static long serialVersionUID = 15L; public static void main(String[] args) { new TriangleArrayDemo(); } public TriangleArrayDemo() { super("TriangleArrayDemo"); 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,400); 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(); TransformGroup transformGroup = new TransformGroup(); transformGroup.addChild(new Triangles().getShape()); branchGroup.addChild(transformGroup); return branchGroup; } 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 Triangles { double verts[] = { 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.5, 1.0, 1.0, 1.5, -1.0, -1.0, 1.5, 1.0, }; float[] colors = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, }; private Shape3D shape; public Triangles() { TriangleArray triangle = new TriangleArray(12, TriangleArray.COORDINATES | TriangleArray.COLOR_3 ); triangle.setCoordinates(0, verts); triangle.setColors(0, colors); shape = new Shape3D(triangle, new Appearance()); } public Shape3D getShape() { return shape; } } }