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 TriangleFanArrayDemo.java @link.letöltés {@docRoot}/../data/java3d TriangleFanArrayDemo.jar @since Java 2 Útikalauz programozóknak 5.0 */ class TriangleFanArrayDemo extends Frame { /** Verziószám */ private final static long serialVersionUID = 15L; public static void main(String[] args) { new TriangleFanArrayDemo(); } public TriangleFanArrayDemo() { super("TriangleFanArrayDemo"); 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(4.0,3.0,4.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[] = { 0.0, 0.0, 0.0, -1.0,1.0,0.0, -1.0,1.0,1.0, -1.0,0.0,1.0, 0.0,0.0,1.0, 0.5,-1.0,1, 2.0, 1.0, 0.0, 1.0,2.0,0.0, 1.0,2.0,1.0, 1.0,1.0,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, }; int[] strips = { 6,4 }; private Shape3D shape; public Triangles() { TriangleFanArray triangle = new TriangleFanArray(10, TriangleFanArray.COORDINATES | TriangleFanArray.COLOR_3,strips ); triangle.setCoordinates(0, verts); triangle.setColors(0, colors); PolygonAttributes polygonAttributes = new PolygonAttributes(); polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE); Appearance appearance = new Appearance(); appearance.setPolygonAttributes(polygonAttributes); shape = new Shape3D(triangle, appearance); } public Shape3D getShape() { return shape; } } }