Re: printing

From: R Shapiro <rshapiro@bbn.com>
Date: Mon Jul 02 2001 - 09:58:33 EDT

Ken Anderson writes:
> I'm not an openmap developer, and I haven't printed myself, but
> Rich Shapiro has.

I have a fairly generic and very small component printing class. It's
included below. I don't know how well it will work with map displays.

import java.awt.print.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import javax.swing.JComponent;

public class ComponentPrinter
  implements Printable, Pageable
{
    private JComponent component;
    private PageFormat format;
    private PrinterJob job;
    private Dimension dimension;
    private Dimension pageDimension;
    private boolean enableScaling = true;
    private double scale;

    private static final double PRINT_RESOLUTION = 72.0;

    public ComponentPrinter(JComponent component,
                            String jobName,
                            boolean enableScaling)
    {
        this.component = component;
        this.enableScaling = enableScaling;
        job = PrinterJob.getPrinterJob();
        if (job == null) {
            System.err.println("Couldn't find a printer!");
        } else {
            format = job.pageDialog(job.defaultPage());
            Dimension size = component.getSize();
            double frameHeight = size.height;
            double frameWidth = size.width;
            double pageHeight = format.getImageableHeight();
            double pageWidth = format.getImageableWidth();
            if (enableScaling) {
                Toolkit tk = Toolkit.getDefaultToolkit();
                scale = PRINT_RESOLUTION/tk.getScreenResolution();
                frameHeight *= scale;
                frameWidth *= scale;
            }
            job.setJobName(jobName);
            pageDimension = new Dimension((int) pageWidth, (int) pageHeight);
            dimension = new Dimension();
            dimension.width = (int) (frameWidth/pageWidth);
            if (frameWidth%pageWidth != 0) dimension.width++;
            dimension.height = (int) (frameHeight/pageHeight);
            if (frameHeight%pageHeight != 0) dimension.height++;
            job.setPageable(this);
        }
    }

    public void enableScaling(boolean flag) {
        enableScaling = flag;
    }

    public boolean isReady() {
        return job != null;
    }

    public void printPages() {
        if (job == null) return;
        try {
            job.print();
        } catch (PrinterException e) {
            System.err.println("Print job failed: " + e);
        }
    }

    // Printable

    public int print(Graphics g, PageFormat format, int page) {
        int x = page%dimension.width;
        int y = page/dimension.width;
        int xTrans = -x*pageDimension.width;
        int yTrans = -y*pageDimension.height;

        // Account for imaging margins.
        // Is this dependent on orientation?
        xTrans += format.getImageableX();
        yTrans += format.getImageableY();

        Graphics2D g2d = (Graphics2D) g;
        if (enableScaling) g2d.scale(scale, scale);
        g2d.translate(xTrans, yTrans);
        
        component.setDoubleBuffered(false);
        component.print(g2d);
        component.setDoubleBuffered(true);

        return Printable.PAGE_EXISTS;
    }

    // Pageable

    public int getNumberOfPages() {
        return dimension.width*dimension.height;
    }

    public PageFormat getPageFormat(int page) {
        return format;
    }

    public Printable getPrintable(int page) {
        return this;
    }

}

-- 
rshapiro@bbn.com
--
[To unsubscribe to this list send an email to "majdart@bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Mon Jul 2 10:02:52 2001

This archive was generated by hypermail 2.1.8 : Thu May 12 2005 - 07:18:31 EDT