Problem with print methods

Hi all,

I've developped a class in order to print a 3D shape on a printer. But after printer selection in print dialog box, Java returns a NullPointerException.

Can you help me to find problem with my code ?

The code is below :

import java.awt.Dimension;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.awt.print.*;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.ImageObserver;

import java.awt.geom.AffineTransform;

publicclass Impression_Image3Dimplements Printable, ImageObserver

{

BufferedImage bImage;

staticfinallong serialVersionUID = 1;

private OffScreenCanvas3D offScreenCanvas =null;

public Impression_Image3D()

{

print();

}

publicint print(Graphics g, PageFormat pf,int pageIndex)throws PrinterException

{

if (pageIndex>=1)

return Printable.NO_SUCH_PAGE;

Graphics g2D = (Graphics2D)g;

g2D.translate((int)pf.getImageableX(), (int)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);

Dimension dim =new Dimension(256,256);

BufferedImage image = offScreenCanvas.getOffScreenImage(dim);

try

{

//Image finale qui va 阾re imprim閑

g2D.drawImage(image, 0, 0,null);

//g2D.drawImage(bImage,t2d, this);

}

catch (Exception ex){

ex.printStackTrace();

return Printable.NO_SUCH_PAGE;

}

return Printable.PAGE_EXISTS;

}

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();

}

}

}

publicboolean imageUpdate(Image img,int infoflags,int x,int y,int width,int height)

{

returnfalse;

}

}

[4685 byte] By [David38a] at [2007-10-3 7:50:51]
# 1

Salut compatriote !

Did you tried to do the same with a full 2D image ?

I mean : replace your getOffScreenImage() method code with a true 2D image drawing code to see if it works.

Try a Java printing tutorial, have it works, and then replace the call to the 2D image generation by a call to a 3D rendering method. If it stop working when adding 3D rendering, you'll have to post the 3D rendering code to get more help.

The goal of my reply is to ensure you didn't make a "hidden" blocking error in java.awt.print usage.

Kilwcha at 2007-7-15 2:52:46 > top of Java-index,Security,Cryptography...
# 2

Oh un Fran鏰is ;-)

Thank you for your reply. In fact, I find a solution. So, in order to help some developpers, I give you class code.

It's the following:

import java.awt.Dimension;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.awt.print.*;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.ImageObserver;

import java.awt.geom.AffineTransform;

import java.awt.KeyboardFocusManager;

public class Impression_Image3D implements Printable, ImageObserver

{

BufferedImage bImage;

static final long serialVersionUID = 1;

private OffScreenCanvas3D offScreenCanvas = null;

public Impression_Image3D()

{

//Etape 1

System.out.println(KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow()); //-->Permet d'afficher la JFrame active

print();

}

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

{

//Etape 4

Graphics g2D = (Graphics2D)g;

if (pageIndex>=1)

return Printable.NO_SUCH_PAGE;

//Dimension en pixels de l'image ?imprimer

//Dimension dim = new Dimension(Dessin3D.Dessin3D_ext.canvas3D.getHeight(),Dessin3D.Dessin3D_ext.canvas3D.getWidth());

Dimension dim = new Dimension(500,500);

offScreenCanvas = Dessin3D.Dessin3D_ext.offScreenCanvas;

//On r閏up鑢e l'image (pixmap) rendue par le canvas3D offscreen

bImage = offScreenCanvas.getOffScreenImage(dim);

g2D.drawImage(bImage, 0, 0, null); //image --> bImage

g2D.translate((int)pf.getImageableX(), (int)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);

return Printable.PAGE_EXISTS;

}

void print()

{

//Etape 2

PrinterJob printJob = PrinterJob.getPrinterJob();

PageFormat pageFormat = printJob.defaultPage();

pageFormat.setOrientation(PageFormat.LANDSCAPE);

pageFormat = printJob.validatePage(pageFormat);

printJob.setPrintable(this, pageFormat);

if (printJob.printDialog())

{

try

{

System.out.println("Impression 閠ape 3");

printJob.print();

}

catch (PrinterException ex)

{

ex.printStackTrace();

}

}

}

public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)

{

return false;

}

}

David38a at 2007-7-15 2:52:46 > top of Java-index,Security,Cryptography...