Dear Experts,

I have, probably, a naive for an expert question regarding printing Java3d screens. 
However I hava some difficulties in 2 elementary examples which I draw from Java3d 
print demo examples. Bellow I present a piece of code that exhibits the following 
problems:
1) the size of Buffered image is completely different than Screen3D Object presumably 
drawn on my terminal.
2) After I got Buffered image, I manage successfully send it to a printer.
However, If I use straighforward transformation into a FileOutputStream, all colors 
are completely mismatched.

Any suggesting would be greatly appreaciated.

Here is the code:

public class MyClass
{
    // a piece of code creating a scene and staff
    .......

    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas3D = new Canvas3D( config );
    Universe3D universe = new SimpleUniverse( canvas );
    OffScreenCanvas3D offScreenCanvas3D = new OffScreenCanvas3D(config, true);
    _universe.getViewer().getView().addCanvas3D(_offScreenCanvas3D);
........
.........

   // here is a printing exercise
   public void myPrint()
   {
      Point loc = canvas3D.getLocationOnScreen();
      _offScreenCanvas3D.setOffScreenLocation(loc);
      Dimension dim = canvas3D.getSize();
      Screen3D sOn = canvas3D.getScreen3D();
      Screen3D sOff = offScreenCanvas3D.getScreen3D();

      // Here is the first problem, this method calls
      // do not assign the right size for the screen of offscreen canvas3d
      sOff.setSize(dim);
      sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth());
      sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight());

      BufferedImage bImage =
                _offScreenCanvas3D.doRender(dim.width, dim.height);

      // send this buffered image to printer
      // this works OK
      (new ImagePrinter(bImage)).print();

      // However, this simple code messes all colors of an image
      String path = "c:\Temp\image.jpg";
      FileOutputStream out = new FileOutputStream(path);
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
      encoder.encode(bImage);
      out.close();
}

// To complete this example, here are some helper classes
public class OffScreenCanvas3D extends Canvas3D {
    public OffScreenCanvas3D(GraphicsConfiguration graphicsConfiguration,
                      boolean offScreen) {

        super(graphicsConfiguration, offScreen);
    }

    public BufferedImage doRender(int width, int height) {

        BufferedImage bImage =
            new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        ImageComponent2D buffer =
            new ImageComponent2D(ImageComponent.FORMAT_RGBA, bImage);

        setOffScreenBuffer(buffer);
        renderOffScreenBuffer();
        waitForOffScreenRendering();
        bImage = getOffScreenBuffer().getImage();

        return bImage;
    }

    public void postSwap() {
        // No-op since we always wait for off-screen rendering to complete
    }
}


public class ImagePrinter implements Printable, ImageObserver {
    BufferedImage bImage;


    public ImagePrinter(BufferedImage bImage) {
        this.bImage = bImage;
    }

    public void print() {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        PageFormat pageFormat = printJob.defaultPage();
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        pageFormat = printJob.validatePage(pageFormat);
        printJob.setPrintable(this, pageFormat);
        if (printJob.printDialog()) {
            try {
                printJob.print();
            }
            catch (PrinterException ex) {
                ex.printStackTrace();
            }
        }
    }

    public boolean imageUpdate(Image img,
                               int infoflags,
                               int x,
                               int y,
                               int width,
                               int height) {
        return false;
    }

    public int print(Graphics g, PageFormat pf, int pi)
        throws PrinterException {


        if (pi >= 1) {
            return Printable.NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D)g;
        //g2d.translate(pf.getImageableX(), pf.getImageableY());
        AffineTransform t2d = new AffineTransform();
        t2d.translate(pf.getImageableX(), pf.getImageableY());
        double xscale  = pf.getImageableWidth() / (double)bImage.getWidth();
        double yscale  = pf.getImageableHeight() /
                                                  (double)bImage.getHeight();
        double scale = Math.min(xscale, yscale);
        t2d.scale(scale, scale);
        try {
            g2d.drawImage(bImage,t2d, this);
        }
        catch (Exception ex) {
            ex.printStackTrace();
            return Printable.NO_SUCH_PAGE;
        }
        return Printable.PAGE_EXISTS;
    }
}


Once again, appreaciate greatly any help,

Boris

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