Printing a GLCanvas

Hello !

It’s very nice, my application works very well with JOGL. But, 'cause I’m perfectionnist (nobody’s perfect) I would like to implement a button to print my GLCanvas.
My GLCanvas is in a JPanel. I’ve tried to print such I did the JPanel but, I’ve got a great gray rectangle >:(
So, how do you print your GLCanvas ?

Thank you very much for your answers.


  void SaveScreenshot(GL gl){

    try{
      FileOutputStream screenShot = new FileOutputStream("screenshot" + imageCount++ + ".tga");
      byte    TGAheader[]  ={0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //Uncompressed TGA header
      byte    infoHeader[] = new byte[6];
      int     screenSize[] = new int[4];
      gl.glGetIntegerv(gl.GL_VIEWPORT,screenSize);
      byte[]  data=  new byte[4*screenSize[2]*screenSize[3]];
      //read in the screen data
      gl.glReadPixels(0, 0, screenSize[2], screenSize[3], gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, data);

      //data needs to be in BGR format
      //swap b and r
      for(int i=0; i< data.length; i+=4){
        byte temp = data[i+2];
        data[i+2] = data[i  ];
        data[i  ] = temp;
      }

      screenShot.write(TGAheader);
      //save info header
      infoHeader[0]= (byte)(screenSize[2] & 0x00FF);
      infoHeader[1]= (byte)((screenSize[2] & 0xFF00) >> 8);
      infoHeader[2]= (byte)(screenSize[3] & 0x00FF);
      infoHeader[3]= (byte)((screenSize[3] & 0xFF00) >> 8);
      infoHeader[4]=  32;
      infoHeader[5]=  0;

      //save info header
      screenShot.write(infoHeader);
      screenShot.write(data);
      screenShot.close();
    }
    catch(Exception e){
      JOptionPane.showMessageDialog(null,"Error writing screenshot to disk",
                                         "Error",
                                         JOptionPane.ERROR_MESSAGE);
    }
  }

Thanks for your answer Java Cool Dude. But my problem is not that I can not save my GLCanvas into a picture file (that’s done in JPG, thanks to this forum). May be I expressed it in a bad way. My problem is that I don’t know how to implement a GLCanvas with Printable implementation.
Any one has an idea ?

What “Java Cool Dude” meant is to first copy the GLCanvas stuff to a buffered image by modifying his screenshot method and then print that buffered image.

You can’t directly print the GLCanvas because your program never sees the image. The video card is doing all the drawing, all your program did is tell the video card to draw in some area of the screen and then it streamed commands to the video card. In order to actually get the image, you have to ask the video card for it (that glReadPixels function).