Well, I had landscape printing working up to a point. However it broke
when I tired to scale the image to the printable page. Notice the code
below. I've changed the AffineTransform in print() to set a scaling for
the picture.
Now this works perfectly when printing portrait. And if I send the
returned by myFrame.getImageToPrint() to a Canvas, I can see it all
perfectly so I know the image is fine. However if the image is
landscape, I get only a small portion of it on the page. Change
g2.drawRenderedImage( rendered_image, AffineTransform.getScaleInstance(
scalePrint, scalePrint) );
to
g2.drawRenderedImage( rendered_image, new AffineTransform() );
and I can see the image but of course it's too big.
Any ideas?
public class ImagePrinter implements Printable
{
RenderedOp rendered_image;
JAIFrame myFrame;
double scalePrint;
/** Creates new ImagePrinter */
public ImagePrinter (
JAIFrame frame )
{
this.myFrame = frame;
}
/**
Displays the page setup and print dialogs and prints the image.
*/
public void doPrinting()
{
PrinterJob job = PrinterJob.getPrinterJob();
Book book = new Book();
PageFormat defaultPage = job.defaultPage();
PageFormat pf = job.pageDialog( defaultPage );
// Cancel was pressed.
if ( pf.equals( defaultPage ) )
{
return;
}
book.append( this, pf );
job.setPageable( book );
if ( !job.printDialog() )
{
return;
}
ParameterBlock pb = new ParameterBlock();
pb.addSource( myFrame.getImageToPrint() );
if ( pf.getOrientation() == pf.PORTRAIT )
{
pb.add( (float)pf.getImageableX() );
pb.add( (float)pf.getImageableY() );
}
else
{
pb.add( -(float)pf.getImageableX() );
pb.add( 0.0F );
}
pb.add( myFrame.getInterpolation() );
rendered_image = (RenderedOp)JAI.create( "translate", pb, null
);
double scaleX =
pf.getImageableWidth()/rendered_image.getWidth();
double scaleY =
pf.getImageableHeight()/rendered_image.getHeight();
scalePrint = Math.min( scaleX, scaleY );
try
{
job.print();
}
catch ( PrinterException e )
{
e.printStackTrace();
}
}
public int print (
Graphics g,
PageFormat pf,
int pi )
throws PrinterException
{
if ( pi != 0 )
return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
g2.drawRenderedImage( rendered_image,
AffineTransform.getScaleInstance( scalePrint, scalePrint) );
System.gc();
return PAGE_EXISTS;
}
}
--
------------------------------------------------------------------------
Thad Humphries "...no religious test shall ever be required
Web Development Manager as a qualification to any office or public
Phone: 540/675-3015, x225 trust under the United States." -Article VI
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".