< prev index next >

test/java/awt/print/PrinterJob/Margins.java

Print this page


   1 /*
   2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6543815
  27  * @summary Image should be sent to printer, no exceptions thrown.
  28  *    The 2 printouts should have a rectangle which is the minimum
  29  *    possible margin.

  30  * @run main/manual Margins
  31  */
  32 
  33 import java.awt.*;
  34 import java.awt.print.*;






  35 
  36 public class Margins implements Printable {
  37 
  38     public static void main(String args[]) {
  39         PrinterJob job = PrinterJob.getPrinterJob();
  40         PageFormat pageFormat = job.defaultPage();
  41         Paper paper = pageFormat.getPaper();
  42         double wid = paper.getWidth();
  43         double hgt = paper.getHeight();
  44         paper.setImageableArea(0, -10, wid, hgt);
  45         pageFormat = job.pageDialog(pageFormat);
  46         pageFormat.setPaper(paper);
  47         job.setPrintable(new Margins(), pageFormat);
  48         try {
  49             job.print();
  50         } catch (PrinterException e) {
  51         }
  52 
  53         paper.setImageableArea(0, 0, wid, hgt+72);
  54         pageFormat = job.pageDialog(pageFormat);
  55         pageFormat.setPaper(paper);
  56 
  57         job.setPrintable(new Margins(), pageFormat);
  58         try {
  59            job.print();
  60         } catch (PrinterException e) {
  61         }















  62    }
  63 
  64    public int print(Graphics g, PageFormat pf, int page)
  65        throws PrinterException {
  66 
  67        if (page > 0) {
  68            return NO_SUCH_PAGE;
  69        }
  70        int ix = (int)pf.getImageableX();
  71        int iy = (int)pf.getImageableY();
  72        int iw = (int)pf.getImageableWidth();
  73        int ih = (int)pf.getImageableHeight();
  74        System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih);
  75        if ((ix < 0) || (iy < 0)) {
  76            throw new RuntimeException("Imageable x or y is a negative value.");
  77        }
  78 

  79        Paper paper = pf.getPaper();
  80        double wid = paper.getWidth();
  81        double hgt = paper.getHeight();
  82 







  83        if ((ix+iw > wid) || (iy+ih > hgt)) {
  84            throw new RuntimeException("Printable width or height exceeds paper width or height.");
  85        }
  86 
  87        Graphics2D g2d = (Graphics2D)g;
  88        g2d.translate(ix, iy);
  89        g2d.setColor(Color.black);
  90        g2d.drawRect(1, 1, iw-2, ih-2);
  91 
  92        return PAGE_EXISTS;
  93    }
  94 }
   1 /*
   2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6543815 6601097
  27  * @summary Image should be sent to printer, no exceptions thrown.
  28  *    The 3 printouts should have a rectangle which is the minimum
  29  *    possible margins ie, the margins should be hardware margins
  30  *    and not java default 1 inch margins.
  31  * @run main/manual Margins
  32  */
  33 
  34 import java.awt.print.PrinterJob;
  35 import java.awt.print.Printable;
  36 import java.awt.print.PageFormat;
  37 import java.awt.print.Paper;
  38 import java.awt.print.PrinterException;
  39 import java.awt.Graphics;
  40 import java.awt.Graphics2D;
  41 import java.awt.Color;
  42 
  43 public class Margins implements Printable {
  44 
  45     public static void main(String args[]) {
  46         PrinterJob job = PrinterJob.getPrinterJob();
  47         PageFormat pageFormat = job.defaultPage();
  48         Paper paper = pageFormat.getPaper();
  49         double wid = paper.getWidth();
  50         double hgt = paper.getHeight();
  51         paper.setImageableArea(0, -10, wid, hgt);
  52         pageFormat = job.pageDialog(pageFormat);
  53         pageFormat.setPaper(paper);
  54         job.setPrintable(new Margins(), pageFormat);
  55         try {
  56             job.print();
  57         } catch (PrinterException e) {
  58         }
  59 
  60         paper.setImageableArea(0, 0, wid, hgt+72);
  61         pageFormat = job.pageDialog(pageFormat);
  62         pageFormat.setPaper(paper);
  63 
  64         job.setPrintable(new Margins(), pageFormat);
  65         try {
  66            job.print();
  67         } catch (PrinterException e) {
  68         }
  69 
  70         pageFormat = job.defaultPage();
  71         paper = pageFormat.getPaper();
  72         wid = paper.getWidth();
  73         hgt = paper.getHeight();
  74 
  75         paper.setImageableArea(0, -10, -wid, hgt);
  76         pageFormat = job.pageDialog(pageFormat);
  77         pageFormat.setPaper(paper);
  78 
  79         job.setPrintable(new Margins(), pageFormat);
  80         try {
  81            job.print();
  82         } catch (PrinterException e) {
  83         }
  84     }
  85 
  86    public int print(Graphics g, PageFormat pf, int page)
  87        throws PrinterException {
  88 
  89        if (page > 0) {
  90            return NO_SUCH_PAGE;
  91        }
  92        int ix = (int)pf.getImageableX();
  93        int iy = (int)pf.getImageableY();
  94        int iw = (int)pf.getImageableWidth();
  95        int ih = (int)pf.getImageableHeight();
  96        System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih);
  97        if ((ix < 0) || (iy < 0)) {
  98            throw new RuntimeException("Imageable x or y is a negative value.");
  99        }
 100 
 101 
 102        Paper paper = pf.getPaper();
 103        double wid = paper.getWidth();
 104        double hgt = paper.getHeight();
 105 
 106        // If imageable width/height is -ve, then print was done with 1" margin
 107        // ie ix=72 iy=72 iw=451 ih=697 and wid=595
 108        // but with fix, we get print with hardware margin ie
 109        // ix=12, iy=12, iw=571, ih=817
 110        if ((wid - iw > 72) || (hgt - ih > 72)) {
 111            throw new RuntimeException("Imageable width or height is negative value");
 112        }
 113        if ((ix+iw > wid) || (iy+ih > hgt)) {
 114            throw new RuntimeException("Printable width or height exceeds paper width or height.");
 115        }
 116 
 117        Graphics2D g2d = (Graphics2D)g;
 118        g2d.translate(ix, iy);
 119        g2d.setColor(Color.black);
 120        g2d.drawRect(1, 1, iw-2, ih-2);
 121 
 122        return PAGE_EXISTS;
 123    }
 124 }
< prev index next >