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 
main(String args[])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 
print(Graphics g, PageFormat pf, int page)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 }
95