Try to get the NCSA Portofolio, it has a RecordableCanvas bound into it.

Steph wrote:

> Hi all!
>
> I am in desperate need of help.  I have written an application that
> creates a 3D bar chart.  Now I want to save that to a .tif file.  I can
> create a rectangle in awt and save it to a .tif file.  However, since
> Java3D doesn't use the paint(Graphics g) method (as far as I can tell),
> I can't figure it out.   I am new at this so any help would be REALLY
> appreciated.  I posted a few questions in the forum and have gotten NO
> responses.  I know the API is new, so there is not many books
> available.  My boss  just bought "The Java 3D API Specification".  Here
> is my code.  It's very messy.
>
> public class Test
>
> {
>  public static Frame displayFrame;
>  protected String filename;
>  public static Image saveImage;
>  public static int Val1 = 0;
>  public static int Val2 = 0;
>  public static int Val3 = 0;
>  public static int highestVal = 0;
>  public static CapturingCanvas3D canvas3D;
>  public static CapturingCanvas3D capIm;
>  public static Image image;
>  public static boolean bTakeShot;
>   public static BufferedImage bi;
>
> public Test(int width, int height)
>
>  {
>  System.out.println("You are in new Test");
>  BranchGroup scene = CreateLighter();
>  SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
>  simpleU.getViewingPlatform().setNominalViewingTransform();
>  simpleU.addBranchGraph(scene);
>  captureImage(canvas3D);
>
>  displayFrame = new Frame("Chart Application");
>  //displayFrame.add(BorderLayout.CENTER, canvas3D);
>  displayFrame.pack();
>
>  }
>
> public static void main(String[] args)
>
> {
>   new Test(500, 500);
>   System.out.println("0");
> }
> public static BranchGroup CreateLighter()
>  {
>
>   System.out.println("1");
>   BranchGroup objRoot = new BranchGroup();
>   System.out.println("2");
>   System.out.println("3");
>   TransformGroup objTrans = new TransformGroup();
>
>   BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
> 100.0);
>
>   Appearance abar1 = new Appearance();
>   Material mbar1 = new Material();
>   AmbientLight light1 = new AmbientLight();
>   light1.setInfluencingBounds(bounds);
>   mbar1.setAmbientColor(1.0f, 0.0f, 0.0f);
>   mbar1.setDiffuseColor(0.7f, 0.7f, 0.7f);
>   mbar1.setLightingEnable(true);
>   abar1.setMaterial(mbar1);
>
>   Appearance abar2 = new Appearance();
>   Material mbar2 = new Material();
>   AmbientLight light2 = new AmbientLight();
>   light2.setInfluencingBounds(bounds);
>   mbar2.setAmbientColor(0.0f, 0.0f, 1.0f);
>   mbar2.setDiffuseColor(0.7f, 0.7f, 0.7f);
>   mbar2.setLightingEnable(true);
>   abar2.setMaterial(mbar2);
>
>   Appearance abar3 = new Appearance();
>   Material mbar3 = new Material();
>   AmbientLight light3 = new AmbientLight();
>   light3.setInfluencingBounds(bounds);
>   mbar3.setAmbientColor(0.0f, 1.0f, 0.0f);
>   mbar3.setDiffuseColor(0.7f, 0.7f, 0.7f);
>   mbar3.setLightingEnable(true);
>   abar3.setMaterial(mbar3);
>
>   Transform3D bar1mat = new Transform3D();
>   Transform3D bar1mat2 = new Transform3D();
>   Box boxObj1 = new Box(0.2f, 0.7f, 0.2f, abar1);
>   //Shape3D shape = new Shape3D(raster);
>   boxObj1.setAppearance(abar1);
>   bar1mat.set(new Vector3d(-0.8, 0.4, -0.9));
>   bar1mat2.set(new Vector3d(-0.8, 0.4, -0.9));
>   TransformGroup maintrans1 = new TransformGroup();
>   Transform3D rotate1 = new Transform3D();
>   Transform3D tempRotate1 = new Transform3D();
>        rotate1.rotX(Math.PI/4.0d);
>   tempRotate1.rotY(Math.PI/3.0d);
>        rotate1.mul(tempRotate1);
>   TransformGroup objRotate1 = new TransformGroup(rotate1);
>   objRotate1.addChild(boxObj1);
>   maintrans1.addChild(light1);
>   maintrans1.addChild(objRotate1);
>   maintrans1.setTransform(bar1mat);
>   maintrans1.setTransform(bar1mat2);
>   objTrans.addChild(maintrans1);
>
>   Transform3D bar2mat = new Transform3D();
>   Transform3D bar2mat2 = new Transform3D();
>   Box boxObj2 = new Box(0.2f, 0.3f, 0.2f, abar2);
>   boxObj2.setAppearance(abar2);
>   bar2mat.set(new Vector3d(-0.4, 0.0, -0.71));
>   bar2mat2.set(new Vector3d(-0.4, 0.0, -0.71));
>   TransformGroup maintrans2 = new TransformGroup();
>   Transform3D rotate2 = new Transform3D();
>   Transform3D tempRotate2 = new Transform3D();
>        rotate2.rotX(Math.PI/4.0d);
>   tempRotate2.rotY(Math.PI/3.0d);
>        rotate2.mul(tempRotate2);
>   TransformGroup objRotate2 = new TransformGroup(rotate2);
>   objRotate2.addChild(boxObj2);
>   maintrans2.addChild(light2);
>   maintrans2.addChild(objRotate2);
>   maintrans2.setTransform(bar2mat);
>   maintrans2.setTransform(bar2mat2);
>   objTrans.addChild(maintrans2);
>
>   Transform3D bar3mat = new Transform3D();
>   Transform3D bar3mat2 = new Transform3D();
>   Box boxObj3 = new Box(0.2f, 0.4f, 0.2f, abar3);
>   boxObj3.setAppearance(abar3);
>   bar3mat.set(new Vector3d(-0.05, -0.05, -0.3));
>   bar3mat2.set(new Vector3d(-0.05, -0.05, -0.3));
>   TransformGroup maintrans3 = new TransformGroup();
>   Transform3D rotate3 = new Transform3D();
>   Transform3D tempRotate3 = new Transform3D();
>        rotate3.rotX(Math.PI/4.0d);
>   tempRotate3.rotY(Math.PI/3.0d);
>        rotate3.mul(tempRotate3);
>   TransformGroup objRotate3 = new TransformGroup(rotate3);
>   objRotate3.addChild(boxObj3);
>   maintrans3.addChild(objRotate3);
>   maintrans3.addChild(light3);
>   maintrans3.setTransform(bar3mat);
>   maintrans3.setTransform(bar3mat2);
>   objTrans.addChild(maintrans3);
>
>   Color3f lightColor = new Color3f(1.0f, 1.0f, 1.0f);
>   Point3f lightDirection = new Point3f(2.0f, 0.0f, 0.5f);
>   PointLight light = new PointLight();
>   light.setPosition(lightDirection);
>   light.setColor(lightColor);
>   light.setInfluencingBounds(bounds);
>   Background backg = new Background(1.0f, 1.0f, 1.0f);
>   backg.setApplicationBounds(bounds);
>   objRoot.addChild(backg);
>   objRoot.addChild(light);
>   objRoot.addChild(objTrans);
>   System.out.println("4");
>   System.out.println("5");
>   objRoot.compile();
>   return objRoot;
>  }
>
> public static void captureImage(CapturingCanvas3D MyCanvas3D)
>      {
>
>   System.out.println("you are here 1 ");
>             MyCanvas3D.writeTIF_ = true;
>   System.out.println("you are here 2");
>        MyCanvas3D.repaint();
>   System.out.println("You are here 3");
>
>      }
>
> }
>
> class CapturingCanvas3D extends Canvas3D
> {
>  public static boolean writeTIF_;
>  private int postSwapCount_;
>  public GraphicsContext3D  ctx;
>  public Raster snapshot;
>
>  public CapturingCanvas3D(GraphicsConfiguration gc)
>  {
>    super(gc);
>         postSwapCount_ = 0;
>    System.out.println("You are in CapturingCanvas3D");
>       }
>
>  public void postSwap()
>  {
>   System.out.println("You are in postSwap");
>         if(writeTIF_) {
>             System.out.println("Writing TIF");
>             ctx = getGraphicsContext3D();
>
>             snapshot = new Raster(
>                    new Point3f(-1.0f,-1.0f,-1.0f),
>                    Raster.RASTER_COLOR,
>                    0,0,
>                    512,512,
>                    new ImageComponent2D(
>                              ImageComponent.FORMAT_RGB,
>                              new BufferedImage(512,512,
>
> BufferedImage.TYPE_INT_RGB)),
>                    null);
>
>             ctx.readRaster(snapshot);
>
>             BufferedImage saveImage = snapshot.getImage().getImage();
>
>             System.out.println("The value of saveImage is "+saveImage);
>
>   String theFile = "C:\\jdk1.2.2\\picture.tif";
>   try
>          {     System.out.println("6");
>      RenderedOp ri = JAI.create("awtimage", saveImage);
>      System.out.println("7");
>      FileOutputStream stream = new FileOutputStream(theFile);
>      //JAI.create("encode", ri, stream, null);
>      JAI.create("filestore", ri, theFile, null);
>      System.out.println("You are past save");
>     }
>     catch (Exception e)
>     {
>             System.out.println("Picture save failed: " +
> e.getMessage());
>     }
>
>             postSwapCount_++;
>         }
>
>  else
>  {
>   System.out.println("Failure writing file");
>  }
> }
>
>  /*public void postRender()
>  {
>         ctx.readRaster(snapshot);
>  }*/
>
> }
>
> One of my main questions is:  When is postSwap called?
>
> I get my NullPointerException at:  MyCanvas3D.repaint();
>
> Thanks so much in advance!
> Stephanie
> [EMAIL PROTECTED]
>
> ===========================================================================
> 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".

--
Best regards,
Dan Todor
---
You can have it done FAST   | \
You can have it done RIGHT  |  | --- Pick any two.
You can have it done CHEAP  | /

===========================================================================
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