When i try to use this code in two times or i call the class "MapaAltura"
in two times, i receive the OutOfMemory message. I dont know what to do,
to clean up or clear the SimpleUniverse when i call the class for second
time.

this is the code. Urgent please!!!


//Main Class
public class MainMapa {
 public static void main(String args[]){
   FrameMapa f = new FrameMapa();

 }

}

//Frame
import com.sun.j3d.utils.applet.JMainFrame;
import java.awt.*;
import java.awt.event.*;
public class FrameMapa {

  private MapaAltura m;

  public FrameMapa() {

    Frame frame = new Frame();
    frame.setSize(100,100);
    Button a = new Button("Apply");
    Button r = new Button("Remove");
    a.addActionListener
         (new ActionListener() {
     public void actionPerformed(ActionEvent e){
     execute();
     }});
    r.addActionListener
         (new ActionListener() {
     public void actionPerformed(ActionEvent e){
     remove();
     }});
     frame.add("North", a);
     frame.add("Center", r);
     frame.setVisible(true);
}

  public void execute(){
    try{
    m = new MapaAltura();
    JMainFrame j = new JMainFrame(m,400,400);}
    catch(Exception e){e.printStackTrace();}
  }

  public void remove(){
    m.killAll();

  }
}


//Class where i create the 3D image
import javax.media.j3d.*;
import javax.vecmath.*;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;

import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.behaviors.vp.*;

import com.sun.j3d.utils.universe.ViewingPlatform;

public class MapaAltura extends Applet {

    private OrbitBehavior orbit = null;
    public Shape3D shape = null;
    public DirectionalLight light= null;
    public Color3f lightColor=null;
    public Bounds  banda=null;
    public BranchGroup branch = null;
    public Material mat = null;
    private Transform3D mov, rotarX, rotarZ;
    private TransformGroup rotatingGroup=null;
    public SimpleUniverse simpleU=null;
    public AlternateAppearance altApp;

  public MapaAltura() {

        branch = new BranchGroup();
        branch.setCapability(BranchGroup.ALLOW_DETACH);
        shape = new MatrizMapa();
        shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);

        Color3f objColor = new Color3f(1f, 1f, 1f);
        Color3f black = new Color3f(0f, 0f, 0f);
        Appearance app = new Appearance();
        mat = new Material(objColor, black, objColor, black, 1f);
        mat.setCapability(Material.ALLOW_COMPONENT_WRITE);

        app.setMaterial(mat);
        shape.setAppearance(app);

        banda =  new BoundingBox(new Point3d(-10, -10, -10),
                new Point3d(10, 10, 10));

        Appearance otherApp = new Appearance();
        Material altMat = new Material();
        altMat.setCapability(Material.ALLOW_COMPONENT_WRITE);
        altMat.setDiffuseColor( new Color3f( 0.0f, 1.0f, 0.0f ) );
        otherApp.setMaterial(altMat);

        altApp = new AlternateAppearance();
        altApp.setAppearance(otherApp);
        altApp.setCapability(AlternateAppearance.ALLOW_SCOPE_WRITE);
        altApp.setCapability(AlternateAppearance.ALLOW_SCOPE_READ);
        altApp.setInfluencingBounds(banda);
        branch.addChild(altApp);

        Background bg = new Background(new Color3f(1f, 1f, 1f));
        bg.setApplicationBounds(banda);
        branch.addChild(bg);

        rotarZ = new Transform3D();
        rotarZ.rotZ(Math.PI);

        rotarX = new Transform3D();
        rotarX.rotX(Math.PI / 180 * -30);

        rotarX.mul(rotarZ);

        mov = new Transform3D();
        mov.set(new Vector3f(0f, 0f, 0f));

        rotarX.mul(mov);

     rotatingGroup = new TransformGroup(rotarX);
        rotatingGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        rotatingGroup.addChild(shape);

        branch.addChild(rotatingGroup);

        lightColor = new Color3f(.5f, .5f, .5f);
        Vector3f lightDirection = new Vector3f(0f, -.5f, -1f);
        light = new DirectionalLight(lightColor, lightDirection);

        light.setCapability
(DirectionalLight.ALLOW_INFLUENCING_BOUNDS_WRITE );
        light.setCapability(DirectionalLight.ALLOW_BOUNDS_WRITE );
        light.setCapability(DirectionalLight.ALLOW_COLOR_WRITE);

        light.setInfluencingBounds(banda);
        branch.addChild(light);

        branch.compile();

        setLayout(new BorderLayout());
        GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();

        Canvas3D canvas3D = new Canvas3D(config);
        add("Center", canvas3D);

        simpleU = new SimpleUniverse(canvas3D);
        ViewingPlatform viewingPlatform = new ViewingPlatform();
        viewingPlatform.setCapability(ViewingPlatform.ALLOW_DETACH);
        viewingPlatform = simpleU.getViewingPlatform();

        viewingPlatform.setNominalViewingTransform();

        orbit = new OrbitBehavior(canvas3D, OrbitBehavior.REVERSE_ALL);
        orbit.setRotationCenter(new Point3d(-1.0, -1.0, -1.0));

        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0, -1.0, -1.0), 0.0);
                orbit.setSchedulingBounds(banda);
                viewingPlatform.setViewPlatformBehavior(orbit);

        simpleU.getViewingPlatform().setNominalViewingTransform();
        simpleU.addBranchGraph(branch);
      }

// i try to clean the SimpleUniverse with this code, but is not working
      public void killAll(){
        shape.setGeometry(null);
//        branch.detach();
//        simpleU.getViewer().clearViewerMap();
//        simpleU.getViewer().getUniverse().cleanup();
//        simpleU.getViewingPlatform().detach();
//        simpleU.getLocale().removeBranchGraph(branch);
        simpleU.removeAllLocales();
        simpleU.cleanup();
        System.gc();
        System.gc();
        System.gc();
        System.gc();
        System.gc();
        System.gc();

      }
}


//Here i create the Geometry. IndexedTriangleArray
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Random;

public class MatrizMapa extends Shape3D {

  private Vector3f v1 = new Vector3f();
  private Vector3f v2 = new Vector3f();
  public Material mat;
  public IndexedTriangleArray tria;

  private Vector3f getNormal(Point3f p0, Point3f p1, Point3f p2) {

                  v1.sub(p1, p0);
                  v2.sub(p2, p0);
                  Vector3f normal = new Vector3f();

                  normal.cross(v1, v2);
                  normal.normalize();
                  return normal;
          }

  public MatrizMapa() {

    int imagenEs[][] = new int[500][500];
    int tamX = imagenEs.length;

    int tamY = imagenEs[0].length;
    int rectas = (tamX - 1) * (tamY -1);
    int puntos = tamX * tamY;

    setCapability(Shape3D.ALLOW_GEOMETRY_WRITE );

    tria = new IndexedTriangleArray(puntos,
                        IndexedTriangleArray.COORDINATES |
IndexedTriangleArray.NORMALS, 6 * rectas);


    float sX = 2f / (float)(tamX - 1) ;
    float sY = 2f / (float)(tamY - 1);

    int punto = 0;
    int max = 5000;

    for (int y = 0; y < tamY ; y++ ){
      for(int x = 0; x < tamX ; x++){
        tria.setCoordinate(punto,
                           new Point3f((float) x * sX - 1f,
                           (float) y * sY - 1f,
                           (float) imagenEs[x][y]));
        punto++;
       }
    }

    Point3f p0 = new Point3f();
    Point3f p1 = new Point3f();
    Point3f p2 = new Point3f();
    Point3f p3 = new Point3f();
    Vector3f triNormals[] = new Vector3f[2 * rectas];

    int pointCounter = 0;

    int triCounter = 0;

    int i = 0;

    for (int y = 0; y < tamY - 1; y++) {
       for (int x = 0; x < tamX - 1; x++) {
                                tria.setCoordinateIndex(i, pointCounter);
                                tria.setNormalIndex(i++, pointCounter);
                                tria.setCoordinateIndex(i, pointCounter +
1);
                                tria.setNormalIndex(i++, pointCounter + 1);
                                tria.setCoordinateIndex(i, pointCounter +
tamX + 1);
                                tria.setNormalIndex(i++, pointCounter +
tamX + 1);

                                tria.setCoordinateIndex(i, pointCounter);
                                tria.setNormalIndex(i++, pointCounter);
                                tria.setCoordinateIndex(i, pointCounter +
tamX + 1);
                                tria.setNormalIndex(i++, pointCounter +
tamX + 1);
                                tria.setCoordinateIndex(i, pointCounter +
tamX);
                                tria.setNormalIndex(i++, pointCounter +
tamX);

                                tria.getCoordinate(pointCounter, p0);
                                tria.getCoordinate(pointCounter + 1, p1);
                                tria.getCoordinate(pointCounter + tamX +
1, p2);
                                tria.getCoordinate(pointCounter + tamX,
p3);

                                triNormals[triCounter++] = getNormal(p0,
p1, p2);
                                triNormals[triCounter++] = getNormal(p0,
p2, p3);

                                pointCounter++;
                        }

                        pointCounter++;
                }

                triCounter = pointCounter = 0;

                int triOfsX[] = { 0, 1, -2, -1, -2, 1 };
                int triOfsY[] =
                        { 0, 0, 0, -2 * tamX + 2, -2 * tamX + 2, -2 * tamX
+ 2 };

                for (int y = 0; y < tamY; y++) {
                        for (int x = 0; x < tamX; x++) {

                                Vector3f normal = new Vector3f(0f, 0f, 0f);
                                int div = 0;

                                for (int t = 0; t < 6; t++) {
                                        if (triOfsX[t] < 0 && x == 0)
                                                continue;
                                        if (triOfsX[t] >= 0 && x == tamX -
1)
                                                continue;
                                        if (triOfsY[t] < 0 && y == 0)
                                                continue;
                                        if (triOfsY[t] >= 0 && y == tamY -
1)
                                                continue;

                                        normal.add(triNormals[triCounter +
triOfsX[t] + triOfsY[t]]);
                                        div++;
                                }

                                normal.scale(1f / ((float) div));
                                normal.normalize();
                                tria.setNormal(pointCounter, normal);

                                triCounter += 2;

                                pointCounter++;
                        }

                        triCounter -= 2;
                }
                setGeometry(tria);
  }
}

Thanks

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to