Curso Java 3D Modulo 16-LibroCodigo-DemoSinusoide
Curso Java 3D Modulo 16-LibroCodigo-DemoSinusoide
Curso Java 3D Modulo 16-LibroCodigo-DemoSinusoide
Demo Sinusoide
Prof. Andrés Eloy Castillo R.
Código de la clase Sinuoside
Sinusoide.java 21/02/2009 10:54 a.m.
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.vecmath.*;
class Sinusoide extends BranchGroup
{
static final int ROJO =1, AZUL = 2, VERDE = 3;
TransformGroup rotX,rotY,rotZ,traslacion;
Transform3D girarX,girarY,girarZ;
public Sinusoide(float amplitud, float longitud, int muestras,int nroCiclos, int color)
{
super();
rotX = new TransformGroup();
rotY = new TransformGroup();
rotZ = new TransformGroup();
girarX = new Transform3D();
girarY = new Transform3D();
girarZ = new Transform3D();
rotX.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
rotX.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
rotY.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
rotY.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
rotZ.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
rotZ.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
traslacion = new TransformGroup();
addChild(traslacion);
traslacion.addChild(rotX);
rotX.addChild(rotY);
rotY.addChild(rotZ);
float periodo = longitud;
float pi = (float) Math.PI;
float delta = nroCiclos*longitud/(muestras);
Point3f[] arregloPuntos = new Point3f[muestras];
int nroVertices = muestras;
int[] verticesxTira = {muestras};
int formato = LineStripArray.COORDINATES|LineStripArray.COLOR_3;
float x,y,z;
Color3f colorCurva;
Color3f[] arregloColor = new Color3f[muestras];
switch (color)
{
case ROJO:
colorCurva = new Color3f(1.0f,0.0f,0.0f);
break;
case AZUL:
colorCurva = new Color3f(0.0f,0.0f,1.0f);
break;
case VERDE:
colorCurva = new Color3f(0.0f,0.0f,1.0f);
break;
default:
colorCurva = new Color3f(1.0f,1.0f,1.0f);
}
for (int i = 0;i < muestras ;i++ )
{
x = 0.0f;
y = i*delta;
z = (float)(amplitud*Math.sin((2*pi/longitud)*(delta*i)));
arregloPuntos[i] = new Point3f(x,y,z);
arregloColor[i] = new Color3f(colorCurva);
}
LineStripArray sinusoideGeo = new LineStripArray(muestras,formato,verticesxTira);
sinusoideGeo.setCoordinates(0,arregloPuntos);
sinusoideGeo.setColors(0,arregloColor);
Shape3D curvaSeno = new Shape3D();
Page 1 of 2
Sinusoide.java 21/02/2009 10:54 a.m.
curvaSeno.setGeometry(sinusoideGeo);
rotZ.addChild(curvaSeno);
}
public void rotarEnX(float anguloRotacionGrados)
{
girarX.rotX(Math.toRadians((double) anguloRotacionGrados));
rotX.setTransform(girarX);
}
public void rotarEnY(float anguloRotacionGrados)
{
girarY.rotY(Math.toRadians((double) anguloRotacionGrados));
rotY.setTransform(girarY);
}
public void rotarEnZ(float anguloRotacionGrados)
{
girarZ.rotZ(Math.toRadians((double) anguloRotacionGrados));
rotZ.setTransform(girarZ);
}
Page 2 of 2
Código de la clase EjeXYZ
EjeXYZ.java 21/02/2009 10:57 a.m.
1 import javax.media.j3d.*;
2 import javax.vecmath.*;
3
4 class EjeXYZ extends BranchGroup
5 {
6 public EjeXYZ()
7 {
8 super();
9 // Creando las geometrias.
10 int formatoVertices,nroVertices;
11 LineArray geoEjeX,geoEjeY,geoEjeZ;
12 nroVertices = 2;
13
14 formatoVertices = GeometryArray.COORDINATES |GeometryArray.COLOR_3;
15 geoEjeX = new LineArray(nroVertices,formatoVertices);
16 geoEjeY = new LineArray(nroVertices,formatoVertices);
17 geoEjeZ = new LineArray(nroVertices,formatoVertices);
18 // ****************************************
19 //*****************************************
20
21 //Creando los elementos coordenadas y color.
22 //******************************************
23 Color3f rojo, verde,azul;
24 rojo = new Color3f(1.0f,0.0f,0.0f);
25 verde = new Color3f(0.0f,1.0f,0.0f);
26 azul = new Color3f(0.0f,0.0f,1.0f);
27
28 Point3f punto1,punto2,punto3,punto4,punto5,punto6;
29
30 punto1 = new Point3f(-15.0f,0.0f,0.0f);
31 punto2 = new Point3f(15.0f,0.0f,0.0f);
32 punto3 = new Point3f(0.0f,-15.0f,0.0f);
33 punto4 = new Point3f(0.0f,15.0f,0.0f);
34 punto5 = new Point3f(0.0f,0.0f,-15.0f);
35 punto6 = new Point3f(0.0f,0.0f,15.0f);
36
37 //***********************************************
38
39 // Agregando coordenadas y color.
40 geoEjeX.setCoordinate(0,punto1);
41 geoEjeX.setCoordinate(1,punto2);
42 geoEjeX.setColor(0,rojo);
43 geoEjeX.setColor(1,rojo);
44
45 geoEjeY.setCoordinate(0,punto3);
46 geoEjeY.setCoordinate(1,punto4);
47 geoEjeY.setColor(0,verde);
48 geoEjeY.setColor(1,verde);
49
50 geoEjeZ.setCoordinate(0,punto5);
51 geoEjeZ.setCoordinate(1,punto6);
52 geoEjeZ.setColor(0,azul);
53 geoEjeZ.setColor(1,azul);
54
55 // **************************************************
56 // Creando los elementos Shape3D
57 Shape3D ejeX,ejeY,ejeZ;
58 ejeX = new Shape3D();
59 ejeY = new Shape3D();
60 ejeZ = new Shape3D();
61
62 // Agregando la geometria a los Shape3D
63 ejeX.setGeometry(geoEjeX);
64 ejeY.setGeometry(geoEjeY);
Page 1 of 2
EjeXYZ.java 21/02/2009 10:57 a.m.
65 ejeZ.setGeometry(geoEjeZ);
66
67 // Agregando los Shape3D, al brazo
68 addChild(ejeX);
69 addChild(ejeY);
70 addChild(ejeZ);
71
72 // Compilando el brazo de volumenes.
73 compile();
74 }
75 }
76
Page 2 of 2
Código de la clase EscenaGrafica
EscenaGrafica.java 21/02/2009 11:01 a.m.
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.vecmath.*;
import java.awt.*;
class EscenaGrafica
{
Canvas3D canvas3D;
EscenaGrafica()
{
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas3D = new Canvas3D(config);
SimpleUniverse su = new SimpleUniverse(canvas3D);
double x,y,z;
double r,phi,tetha;
r = 30.0;
phi = 60.0;
tetha =35.0;
x = r*Math.cos(Math.toRadians(tetha))*Math.sin(Math.toRadians(phi));
y = r*Math.cos(Math.toRadians(tetha))*Math.cos(Math.toRadians(phi));
z = r*Math.sin(Math.toRadians(tetha));
Transform3D t3d = new Transform3D();
t3d.lookAt( new Point3d(x,y,z),new Point3d(0.0,0.0,0.0), new Vector3d(0.0,0.0,1.0));
t3d.invert();
su.getViewingPlatform().getViewPlatformTransform().setTransform(t3d);
BranchGroup bg = new BranchGroup();
//**************************************
//**************************************
Point3f punto1,punto2,punto3,punto4,punto5,punto6;
//***********************************************
Page 1 of 2
EscenaGrafica.java 21/02/2009 11:01 a.m.
sinusoide02.rotarEnY(90.0f);
EjeXYZ ejeXYZ = new EjeXYZ();
bg.addChild(ejeXYZ);
bg.addChild(sinusoide01);
bg.addChild(sinusoide02);
// Compilando el brazo de volumenes.
bg.compile();
Page 2 of 2
Código de la clase DemoSinusoidal
DemoSinusoidal.java 21/02/2009 11:04 a.m.
1 import javax.swing.*;
2 class DemoSinusoidal extends JFrame
3 {
4 DemoSinusoidal()
5 {
6 super("Demo Seno");
7 EscenaGrafica eg = new EscenaGrafica();
8 setSize(700,500);
9 setLocation(200,200);
10 getContentPane().add("Center",eg.getCanvas3D());
11 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12 setVisible(true);
13 }
14 public static void main(String[] args)
15 {
16 DemoSinusoidal demo = new DemoSinusoidal();
17 }
18 }
19
Page 1 of 1