Re: printing

From: Tennessee Leeuwenburg <t.leeuwenburg@bom.gov.au>
Date: Sun Jul 01 2001 - 20:44:43 EDT

I think the problem with my printing relates to some kind of incorrect
measurement of the printable image size.

I find that (usually) the full height of my image is printed, but only
about 2/3 of the width, with the other 1/3 being clipped. When I get a 1/2
size version of my image, and print that instead, I can see everything -
but of course the image is half the size I want it to be. I can't work out
why it is that I'm not getting the right numbers for my printable image
width. Here's my code. If you can help, thanks!

-T

public class PrintImageMenuItem extends JMenuItem
implements ActionListener, Printable {

        // Stuff for building the image
        BeanContext mapHandler = null;
     PrintSunJPEGFormatter formatter = null;

     // Stuff for printing
     private final double inch = 72;
     PrinterJob printJob = PrinterJob.getPrinterJob();

     /**
      * @param String A String that will be displayed when this
      * menuitem is shown in GUI
      * @param AbstractImageFormatter A formatter that knows how to
      * generate an image from MapBean.
      */
     public PrintImageMenuItem(String display, PrintSunJPEGFormatter
in_formatter) {
                this(display);
                formatter = in_formatter;
     }

     public PrintImageMenuItem(String display) {
                super(display);
                addActionListener(this);
                printJob.setPrintable(this);
        }

        public int print(Graphics g, PageFormat pageFormat, int pageIndex) {

                System.out.println("trying really hard");

                int i;
                Graphics2D g2d = (Graphics2D)g;

                //// Print out maps in landscape
                pageFormat.setOrientation(PageFormat.LANDSCAPE);

                if(pageIndex == 0 ) {

                        Iterator it = mapHandler.iterator();
                        Object someObj;

                        while(it.hasNext()) {
                            someObj = it.next();
                         if(someObj instanceof MapBean) {

                                        if(formatter == null) { return 0; }

                                        // Get the printable area of the page
                                        g.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
                                        int pageWidth = (int)pageFormat.getImageableWidth();
                                        int pageHeight = (int)pageFormat.getImageableHeight();

                                        // Get the image
                                        Image image =
formatter.getScaledActualImageFromMapBean((MapBean)someObj, (int)(pageWidth
* 1), (int)(pageHeight * 1));
                                        int imageWidth = image.getWidth(this);
                                        int imageHeight = image.getHeight(this);

                                        System.out.println(pageWidth + ", " + imageWidth);
                                        System.out.println(pageHeight + ", " + imageHeight);
                                        System.out.println((int)pageFormat.getWidth() + ", " +
(int)pageFormat.getHeight());

/*
                                        // Scalars etc
                                        int nCol = Math.max( (int)Math.ceil( (double)imageWidth/pageWidth), 1);
                                        int nRow = Math.max( (int)Math.ceil( (double)imageHeight/pageHeight), 1);

                                        int iCol = pageIndex % nCol;
                                        int iRow = pageIndex / nCol;
                                        int x = iCol * pageWidth;
                                        int y = iRow * pageHeight;
                                        int wImage = Math.min(pageWidth, imageWidth - x);
                                        int hImage = Math.min(pageHeight, imageHeight - y);

                                        // Stuff for drawing the image
                                        g2d.drawImage (
                                                image,
                                                0, // initial co-ordinates
                                                0, // initial co-ordinates
                                                wImage,
                                                hImage,
                                                x,
                                                y,
                                                x + wImage,
                                                y + hImage,
                                                this
                                        );
*/

                                        // Stuff for drawing the image
                                        g2d.drawImage (image,
                                                0, // initial co-ordinates
                                                0, // initial co-ordinates
                                                imageWidth, // size of viewing window
                                                imageHeight,
                                                this
                                        );

                                }
                 }

                        return(PAGE_EXISTS);

                }
                else { return (NO_SUCH_PAGE); }

        }

        /** This method will initiate a print */
     public void actionPerformed(ActionEvent ae) {

                // If there's nothing to print, don't try
                if(mapHandler == null) {
                        System.out.println("null");
                        return; }

                        System.out.println("trying");

                if(printJob.printDialog()) {
                        System.out.println("check this out");
                        try { printJob.print(); }
                        catch (Exception e) { e.printStackTrace(); }
                }
        }

        /** Method to set the map handler */
     public void setMapHandler(BeanContext in_mapHandler) {
                System.out.println("setting maphandler");
                Debug.message("printimagemenuitem","Setting mapHandler " + in_mapHandler);
                mapHandler= in_mapHandler;
     }

}
==============================================
This email contains information which may be useful. The views
expressed within may or may not be the views of the author.
==============================================

-Tennessee Leeuwenburg, Silver Stream Pty Ltd
0418 300 881

--
[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 Sun Jul 1 20:48:28 2001

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