|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | +import java.awt.BasicStroke; |
| 25 | +import java.awt.Color; |
| 26 | +import java.awt.Graphics; |
| 27 | +import java.awt.Graphics2D; |
| 28 | +import java.awt.event.WindowAdapter; |
| 29 | +import java.awt.event.WindowEvent; |
| 30 | +import java.awt.print.Book; |
| 31 | +import java.awt.print.PageFormat; |
| 32 | +import java.awt.print.Paper; |
| 33 | +import java.awt.print.Printable; |
| 34 | +import java.awt.print.PrinterException; |
| 35 | +import java.awt.print.PrinterJob; |
| 36 | +import javax.print.attribute.HashPrintRequestAttributeSet; |
| 37 | +import javax.print.attribute.PrintRequestAttributeSet; |
| 38 | +import javax.print.attribute.standard.Sides; |
| 39 | +import javax.swing.JFrame; |
| 40 | + |
| 41 | +import static java.awt.print.PageFormat.LANDSCAPE; |
| 42 | +import static java.awt.print.PageFormat.PORTRAIT; |
| 43 | +import static java.awt.print.PageFormat.REVERSE_LANDSCAPE; |
| 44 | + |
| 45 | +/* |
| 46 | + * @test |
| 47 | + * @bug 8307246 |
| 48 | + * @key printer |
| 49 | + * @library ../../../regtesthelpers |
| 50 | + * @build PassFailJFrame |
| 51 | + * @summary Test for comparing offsets of images drawn with opaque |
| 52 | + * and translucent colors printed in all orientations |
| 53 | + * @run main/manual AlphaPrintingOffsets |
| 54 | + */ |
| 55 | + |
| 56 | +public class AlphaPrintingOffsets { |
| 57 | + private static final String INSTRUCTIONS = |
| 58 | + "Tested bug occurs only on-paper printing " + |
| 59 | + "so you mustn't use PDF printer\n\n" + |
| 60 | + "1. Java print dialog should appear.\n" + |
| 61 | + "2. Press the Print button on the Java Print dialog.\n" + |
| 62 | + "3. Please check the page margin rectangle are properly " + |
| 63 | + "drawn and visible on all sides on all pages.\n" + |
| 64 | + "If so, press PASS, else press FAIL.\n\n" + |
| 65 | + "Also you may run this test in paper-saving mode. Due " + |
| 66 | + "to tested bug affects pages printed with transparency " + |
| 67 | + "in LANDSCAPE and REVERSE_LANDSCAPE orientations there " + |
| 68 | + "is an option to print only 2 pages affected. " + |
| 69 | + "To do it pass PaperSavingMode parameter."; |
| 70 | + |
| 71 | + private static boolean isPaperSavingMode = false; |
| 72 | + |
| 73 | + public static void main(String[] args) throws Exception { |
| 74 | + if (PrinterJob.lookupPrintServices().length > 0) { |
| 75 | + |
| 76 | + String instructionsHeader = "This test prints 6 pages with page " + |
| 77 | + "margin rectangle and a text message. \n"; |
| 78 | + if (args.length > 0) { |
| 79 | + isPaperSavingMode = args[0].equals("PaperSavingMode"); |
| 80 | + } |
| 81 | + if (isPaperSavingMode) { |
| 82 | + instructionsHeader = "This test prints 2 pages with page " + |
| 83 | + "margin rectangle and a text message. \n"; |
| 84 | + } |
| 85 | + PassFailJFrame.builder() |
| 86 | + .instructions(instructionsHeader + INSTRUCTIONS) |
| 87 | + .rows(15) |
| 88 | + .testUI(() -> createTestUI()) |
| 89 | + .build() |
| 90 | + .awaitAndCheck(); |
| 91 | + |
| 92 | + } else { |
| 93 | + throw new RuntimeException("Test failed : Printer not configured or available."); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + public static JFrame createTestUI() { |
| 99 | + JFrame testUI = new JFrame("Print images"); |
| 100 | + testUI.setSize(1, 1); |
| 101 | + testUI.addWindowListener(new WindowAdapter() { |
| 102 | + @Override |
| 103 | + public void windowOpened(WindowEvent e) { |
| 104 | + super.windowOpened(e); |
| 105 | + print(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void windowActivated(WindowEvent e) { |
| 110 | + super.windowActivated(e); |
| 111 | + testUI.setVisible(false); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + return testUI; |
| 116 | + } |
| 117 | + |
| 118 | + private static void print() { |
| 119 | + PrinterJob printerJob = PrinterJob.getPrinterJob(); |
| 120 | + PageFormat pageFormatP = printerJob.defaultPage(); |
| 121 | + |
| 122 | + Paper paper = pageFormatP.getPaper(); |
| 123 | + paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); |
| 124 | + pageFormatP.setPaper(paper); |
| 125 | + |
| 126 | + PageFormat pageFormatL = (PageFormat) pageFormatP.clone(); |
| 127 | + PageFormat pageFormatRL = (PageFormat) pageFormatP.clone(); |
| 128 | + |
| 129 | + pageFormatL.setOrientation(LANDSCAPE); |
| 130 | + pageFormatRL.setOrientation(REVERSE_LANDSCAPE); |
| 131 | + |
| 132 | + Printable printableTransparent = new CustomPrintable(254); |
| 133 | + if (isPaperSavingMode) { |
| 134 | + Book bookPageSavingTest = new Book(); |
| 135 | + bookPageSavingTest.append(printableTransparent, pageFormatL); |
| 136 | + bookPageSavingTest.append(printableTransparent, pageFormatRL); |
| 137 | + printerJob.setPageable(bookPageSavingTest); |
| 138 | + } else { |
| 139 | + Printable printableOpaque = new CustomPrintable(255); |
| 140 | + Book bookDefaultTest = new Book(); |
| 141 | + bookDefaultTest.append(printableOpaque, pageFormatP); |
| 142 | + bookDefaultTest.append(printableTransparent, pageFormatP); |
| 143 | + bookDefaultTest.append(printableOpaque, pageFormatL); |
| 144 | + bookDefaultTest.append(printableTransparent, pageFormatL); |
| 145 | + bookDefaultTest.append(printableOpaque, pageFormatRL); |
| 146 | + bookDefaultTest.append(printableTransparent, pageFormatRL); |
| 147 | + printerJob.setPageable(bookDefaultTest); |
| 148 | + } |
| 149 | + PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); |
| 150 | + aset.add(Sides.ONE_SIDED); |
| 151 | + |
| 152 | + if (printerJob.printDialog()) { |
| 153 | + try { |
| 154 | + printerJob.print(aset); |
| 155 | + } catch (PrinterException e) { |
| 156 | + e.printStackTrace(); |
| 157 | + throw new RuntimeException("Exception whilst printing."); |
| 158 | + } |
| 159 | + } else { |
| 160 | + throw new RuntimeException("Test failed : " + |
| 161 | + "User selected 'Cancel' button on the print dialog"); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + static class CustomPrintable implements Printable { |
| 166 | + private static final int THICKNESS = 10; |
| 167 | + private static final int MARGIN = 15; |
| 168 | + private static final int SMALL_RECTANGLE_SIZE = 5; |
| 169 | + private final int alphaValue; |
| 170 | + |
| 171 | + public CustomPrintable(int alpha) { |
| 172 | + alphaValue = alpha; |
| 173 | + } |
| 174 | + |
| 175 | + private String getOrientStr(int orient) { |
| 176 | + switch (orient) { |
| 177 | + case PORTRAIT: |
| 178 | + return "PORTRAIT"; |
| 179 | + case LANDSCAPE: |
| 180 | + return "LANDSCAPE"; |
| 181 | + case REVERSE_LANDSCAPE: |
| 182 | + return "REVERSE_LANDSCAPE"; |
| 183 | + default: |
| 184 | + return "BAD Orientation"; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public int print(Graphics g, PageFormat pageFormat, int pageIndex) { |
| 190 | + |
| 191 | + if (pageIndex > 5) { |
| 192 | + return Printable.NO_SUCH_PAGE; |
| 193 | + } |
| 194 | + |
| 195 | + drawRectangle(g, pageFormat.getImageableX(), pageFormat.getImageableY(), |
| 196 | + pageFormat.getImageableWidth(), pageFormat.getImageableHeight()); |
| 197 | + |
| 198 | + drawSmallRectangle(g, pageFormat.getImageableX(), pageFormat.getImageableY(), |
| 199 | + pageFormat.getImageableWidth(), pageFormat.getImageableHeight()); |
| 200 | + |
| 201 | + drawMsg(g, 300, 300, pageFormat.getOrientation()); |
| 202 | + return Printable.PAGE_EXISTS; |
| 203 | + } |
| 204 | + |
| 205 | + private void drawRectangle(Graphics g, double x, double y, double width, double height) { |
| 206 | + Graphics2D g2d = (Graphics2D) g; |
| 207 | + g2d.setStroke(new BasicStroke(THICKNESS)); |
| 208 | + |
| 209 | + // Draw rectangle with thick border lines |
| 210 | + g2d.drawRect((int) x + MARGIN, (int) y + MARGIN, |
| 211 | + (int) width - MARGIN * 2, (int) height - MARGIN * 2); |
| 212 | + } |
| 213 | + |
| 214 | + private void drawSmallRectangle(Graphics g, double x, double y, double width, double height) { |
| 215 | + Graphics2D g2d = (Graphics2D) g; |
| 216 | + Color originalColor = g2d.getColor(); |
| 217 | + |
| 218 | + g2d.setColor(new Color(0, 0, 0, alphaValue)); |
| 219 | + // Calculate the position to center the smaller rectangle |
| 220 | + double centerX = x + (width - SMALL_RECTANGLE_SIZE) / 2; |
| 221 | + double centerY = y + (height - SMALL_RECTANGLE_SIZE) / 2; |
| 222 | + |
| 223 | + g2d.fillRect((int) centerX, (int) centerY, SMALL_RECTANGLE_SIZE, SMALL_RECTANGLE_SIZE); |
| 224 | + |
| 225 | + g2d.setColor(originalColor); |
| 226 | + } |
| 227 | + |
| 228 | + private void drawMsg(Graphics g, int x, int y, int orient) { |
| 229 | + Graphics2D g2d = (Graphics2D) g; |
| 230 | + |
| 231 | + String msg = "Orient= " + getOrientStr(orient); |
| 232 | + msg += " Color=" + (alphaValue != 255 ? " ALPHA" : " OPAQUE"); |
| 233 | + g2d.drawString(msg, x, y); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | + |
0 commit comments