Bill Day   <[EMAIL PROTECTED]> wrote:

>Uwe Trostheide <[EMAIL PROTECTED]> wrote:
>> is it possible to get an Image (e.g .jpg or .gif)
>> out of a java3d program?
>>

>There is an entry in the Java 3D FAQ discussing
>how to capture still images from Java 3D:
>
>http://tintoy.ncsa.uiuc.edu/~srp/java3d/howto.html#capture
>
>Once you have the BufferedImage containing the
>bits as outlined above, you can then save them
>to disk as a JPEG using Java 2D.  I describe how to
>do this in my "Media Programming FAQ".  You can
>find the applicable FAQ item by searching for
>"snapshot" in the FAQ, which is at:
>  http://www.billday.com/Work/mediafaq.txt
>

I was trying to do the same thing (get an image), so
I combined the code from the two FAQs, it looked good and compiled
but unfortunately it did not work (I tried WinNT and Linux).
I get a NullPointerException when I try to read the Raster.
What am I doing wrong? Any ideas?

Thanx
        Ingar

This is the exception I get:

Exception occurred during Canvas3D callback:
java.lang.NullPointerException
        at javax.media.j3d.GraphicsContext3D.readRaster(Compiled Code)
        at MyCanvas3D.postSwap(Compiled Code)
        at javax.media.j3d.Renderer.run(Compiled Code)

Some excerpts from my code:
My subclass of Canvas3D:

public class MyCanvas3D extends Canvas3D
{   boolean print = false;

    public MyCanvas3D(GraphicsConfiguration g)
    {   super(g);
    }

    public void activatePrint()
    {    print = true;
    }

    public void postSwap()
    { if (print)
      { // From  java3d FAQ
        // http://tintoy.ncsa.uiuc.edu/~srp/java3d/howto.html#capture
        GraphicsContext3D  ctx = getGraphicsContext3D();
        javax.media.j3d.Raster ras = new javax.media.j3d.Raster();
        ctx.readRaster(ras);

        // Now strip out the image info
        ImageComponent2D img_src = ras.getImage();
        DepthComponent depth = ras.getDepthComponent();
        BufferedImage img = img_src.getImage();

        // From  mediafaq.txt
        // http://www.billday.com/Work/mediafaq.txt
        try
        { OutputStream out = new FileOutputStream("test.jpg");
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          encoder.encode(img);
          out.close();
        }
        catch (Exception ex)
        { System.out.println(ex); }
      }
    }
}

Some lines from the class that calls repaint():
...
  canvas = new MyCanvas3D(c);
...
  frame.add(BorderLayout.CENTER, canvas);
  Button buttonPrint = new Button("Print");
  buttonPrint.addActionListener(new ActionListener() {
        public void  actionPerformed(ActionEvent e)
        {
           canvas.activatePrint();
           canvas.repaint();
           System.out.println("Test, in TextDrucken");
        }
  });
  frame.add(BorderLayout.SOUTH, buttonPrint);
  frame.show();
...
--
Ingar Uhe                              [EMAIL PROTECTED]
Universitaet Koblenz-Landau
Institut fuer Softwaretechnik          Fon: +49 261 287-2702
Rheinau 1, D-56075 Koblenz, Germany    Fax: +49 261 287-2721

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