1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.PrintJob;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/*
* @test
* @bug 4242308 4255603
* @key printer
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @summary Tests printing of images
* @run main/manual ImageTest
*/
public final class ImageTest {
private static final class ImageFrame extends Frame {
final Image img;
PrintJob pjob;
private ImageFrame() {
super("ImageFrame");
img = getToolkit().getImage("image.gif");
}
@Override
public void paint(Graphics g) {
int width = img.getWidth(this);
int height = img.getHeight(this);
if (pjob != null) {
System.out.println("Size " + pjob.getPageDimension());
Dimension dim = pjob.getPageDimension();
if (width > dim.width) {
width = dim.width - 30; // take care of paper margin
}
if (height > dim.height) {
height = dim.height - 30;
}
}
g.drawImage(img, 10, 75, width, height, this);
}
private void setPrintJob(PrintJob pj) {
pjob = pj;
}
@Override
public boolean imageUpdate(Image img, int infoflags,
int x, int y, int w, int h) {
if ((infoflags & ALLBITS) != 0) {
repaint();
return false;
}
return true;
}
}
private static Frame init() {
ImageFrame f = new ImageFrame();
f.setLayout(new FlowLayout());
Button b = new Button("Print");
b.addActionListener(e -> {
PrintJob pj = Toolkit.getDefaultToolkit()
.getPrintJob(f, "ImageTest", null);
if (pj != null) {
f.setPrintJob(pj);
Graphics pg = pj.getGraphics();
f.paint(pg);
pg.dispose();
pj.end();
}
});
f.add(b);
f.setSize(700, 350);
return f;
}
private static void createImage() throws IOException {
final BufferedImage bufferedImage =
new BufferedImage(600, 230, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(new Color(0xE7E7E7));
g2d.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
g2d.setColor(Color.YELLOW);
g2d.fillRect(0, 6, 336, 40);
g2d.setColor(Color.BLACK);
g2d.drawString("Yellow rectangle", 10, 30);
g2d.setColor(Color.CYAN);
g2d.fillRect(132, 85, 141, 138);
g2d.setColor(Color.BLACK);
g2d.drawString("Cyan rectangle", 142, 148);
g2d.setColor(Color.MAGENTA);
g2d.fillRect(432, 85, 141, 138);
g2d.setColor(Color.BLACK);
g2d.drawString("Magenta rectangle", 442, 148);
g2d.dispose();
ImageIO.write(bufferedImage, "gif", new File("image.gif"));
}
private static final String INSTRUCTIONS = """
Click the Print button on the Frame. Select a printer from the
print dialog and click 'OK'. Verify that the image displayed
in the Frame is correctly printed. Test printing in both Color
and Monochrome.
""";
public static void main(String[] args) throws Exception {
if (PrinterJob.lookupPrintServices().length == 0) {
throw new RuntimeException("Printer not configured or available.");
}
createImage();
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(45)
.testUI(ImageTest::init)
.build()
.awaitAndCheck();
}
}
|