3535N/A/*
3535N/A * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3535N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3535N/A *
3535N/A * This code is free software; you can redistribute it and/or modify it
3535N/A * under the terms of the GNU General Public License version 2 only, as
3535N/A * published by the Free Software Foundation.
3535N/A *
3535N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3535N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3535N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3535N/A * version 2 for more details (a copy is included in the LICENSE file that
3535N/A * accompanied this code).
3535N/A *
3535N/A * You should have received a copy of the GNU General Public License version
3535N/A * 2 along with this work; if not, write to the Free Software Foundation,
3535N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3535N/A *
3535N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3535N/A * or visit www.oracle.com if you need additional information or have any
3535N/A * questions.
3535N/A */
3535N/A
3535N/A/**
3535N/A *
3535N/A * @test
3535N/A * @bug 4521945 7006865
3535N/A * @summary Test printing images of different types.
3535N/A * @author prr
3535N/A * @run main/manual=yesno/timeout=900 ImageTypes
3535N/A */
3535N/A
3535N/Aimport java.io.*;
3535N/Aimport static java.awt.Color.*;
3535N/Aimport java.awt.*;
3535N/Aimport java.awt.geom.*;
3535N/Aimport java.awt.event.*;
3535N/Aimport java.awt.print.*;
3535N/Aimport java.awt.image.*;
3535N/Aimport static java.awt.image.BufferedImage.*;
3535N/Aimport javax.print.*;
3535N/Aimport javax.print.attribute.*;
3535N/Aimport javax.print.attribute.standard.*;
3535N/A
3535N/Apublic class ImageTypes extends Frame implements ActionListener {
3535N/A
3535N/A private ImageCanvas c;
3535N/A
3535N/A public static void main(String args[]) {
3535N/A
3535N/A ImageTypes f = new ImageTypes();
3535N/A f.show();
3535N/A }
3535N/A
3535N/A public ImageTypes () {
3535N/A super("Image Types Printing Test");
3535N/A c = new ImageCanvas();
3535N/A add("Center", c);
3535N/A
3535N/A Button printThisButton = new Button("Print");
3535N/A printThisButton.addActionListener(this);
3535N/A Panel p = new Panel();
3535N/A p.add(printThisButton);
3535N/A add("South", p);
3535N/A add("North", getInstructions());
3535N/A addWindowListener(new WindowAdapter() {
3535N/A public void windowClosing(WindowEvent e) {
3535N/A System.exit(0);
3535N/A }
3535N/A });
3535N/A
3535N/A pack();
3535N/A }
3535N/A
3535N/A private TextArea getInstructions() {
3535N/A TextArea ta = new TextArea(10, 60);
3535N/A ta.setFont(new Font("Dialog", Font.PLAIN, 11));
3535N/A ta.setText
3535N/A ("This is a manual test as it requires that you compare "+
3535N/A "the on-screen rendering with the printed output.\n"+
3535N/A "Select the 'Print' button to print out the test.\n"+
3535N/A "For each image compare the printed one to the on-screen one.\n"+
3535N/A "The test PASSES if the onscreen and printed rendering match.");
3535N/A return ta;
3535N/A }
3535N/A
3535N/A public void actionPerformed(ActionEvent e) {
3535N/A PrinterJob pj = PrinterJob.getPrinterJob();
3535N/A
3535N/A PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
3535N/A if (pj != null && pj.printDialog(attrs)) {
3535N/A pj.setPrintable(c);
3535N/A try {
3535N/A pj.print(attrs);
3535N/A } catch (PrinterException pe) {
3535N/A pe.printStackTrace();
3535N/A throw new RuntimeException("Exception whilst printing.");
3535N/A } finally {
3535N/A System.out.println("PRINT RETURNED OK.");
3535N/A }
3535N/A }
3535N/A }
3535N/A}
3535N/A
3535N/Aclass ImageCanvas extends Component implements Printable {
3535N/A
3535N/A IndexColorModel icm2 = null;
3535N/A IndexColorModel icm4 = null;
3535N/A BufferedImage opaqueImg = null;
3535N/A BufferedImage transImg = null;
3535N/A int sw=99, sh=99;
3535N/A
3535N/A void paintImage(BufferedImage bi, Color c1, Color c2) {
3535N/A
3535N/A GradientPaint tp= new GradientPaint(0.0f, 0.0f, c1, 10f, 8f, c2, true);
3535N/A Graphics2D g2d = (Graphics2D)bi.getGraphics();
3535N/A g2d.setPaint(tp);
3535N/A g2d.fillRect(0, 0, sw, sh);
3535N/A g2d.setColor(gray);
3535N/A int cnt=0;
3535N/A Font font = new Font("Serif", Font.PLAIN, 11);
3535N/A g2d.setFont(font);
3535N/A FontMetrics fm = g2d.getFontMetrics();
3535N/A for (int y=12;y<sh;y+=12) {
3535N/A int x = 0;
3535N/A while (x < sw) {
3535N/A String s = (new Integer(++cnt)).toString();
3535N/A g2d.drawString(s, x, y);
3535N/A x+= fm.stringWidth(s);
3535N/A }
3535N/A }
3535N/A }
3535N/A
3535N/A ImageCanvas() {
3535N/A
3535N/A opaqueImg = new BufferedImage(sw, sh, TYPE_INT_RGB);
3535N/A Color o1 = new Color(0, 0, 0);
3535N/A Color o2 = new Color(255, 255, 255);
3535N/A paintImage(opaqueImg, o1, o2);
3535N/A
3535N/A transImg = new BufferedImage(sw, sh, TYPE_INT_ARGB);
3535N/A Color t1 = new Color(255, 255, 255, 220);
3535N/A Color t2 = new Color(255, 200, 0, 220);
3535N/A paintImage(transImg, t1, t2);
3535N/A
3535N/A /* greyscale 2bpp */
3535N/A byte[] arr2bpp = {(byte)0, (byte)0x55, (byte)0xaa, (byte)0xff};
3535N/A icm2 = new IndexColorModel(2, 4, arr2bpp, arr2bpp, arr2bpp);
3535N/A
3535N/A /* color 4bpp */
3535N/A int[] cmap = new int[16];
3535N/A cmap[0] = black.getRGB();
3535N/A cmap[1] = white.getRGB();
3535N/A cmap[2] = gray.getRGB();
3535N/A cmap[3] = lightGray.getRGB();
3535N/A cmap[4] = red.getRGB();
3535N/A cmap[5] = green.getRGB();
3535N/A cmap[6] = blue.getRGB();
3535N/A cmap[7] = yellow.getRGB();
3535N/A cmap[8] = cyan.getRGB();
3535N/A cmap[9] = magenta.getRGB();
3535N/A cmap[10] = orange.getRGB();
3535N/A cmap[11] = pink.getRGB();
3535N/A cmap[12] = darkGray.getRGB();
3535N/A cmap[13] = 192 << 16 ; // dark red.
3535N/A cmap[14] = 192 << 8; // dark green
3535N/A cmap[15] = 192; // dark blue
3535N/A
3535N/A icm4 = new IndexColorModel(4, 16, cmap, 0, false, -1,
3535N/A DataBuffer.TYPE_BYTE);
3535N/A
3535N/A }
3535N/A
3535N/A
3535N/A public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
3535N/A
3535N/A if (pgIndex > 0) {
3535N/A return Printable.NO_SUCH_PAGE;
3535N/A }
3535N/A Graphics2D g2d = (Graphics2D)g;
3535N/A g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
3535N/A paint(g2d);
3535N/A return Printable.PAGE_EXISTS;
3535N/A }
3535N/A
3535N/A private void drawImage(Graphics g, int biType, IndexColorModel icm) {
3535N/A
3535N/A BufferedImage bi;
3535N/A if (icm != null) {
3535N/A bi = new BufferedImage(sw, sh, biType, icm);
3535N/A } else {
3535N/A bi = new BufferedImage(sw, sh, biType);
3535N/A }
3535N/A
3535N/A Graphics big = bi.getGraphics();
3535N/A if (bi.getColorModel().getPixelSize() <=2) {
3535N/A big.drawImage(opaqueImg, 0, 0, null);
3535N/A } else {
3535N/A big.drawImage(transImg, 0, 0, null);
3535N/A }
3535N/A g.drawImage(bi, 0, 0, null);
3535N/A }
3535N/A
3535N/A public void paint(Graphics g) {
3535N/A
3535N/A int incX = sw+10, incY = sh+10;
3535N/A
3535N/A g.translate(10, 10);
3535N/A
3535N/A drawImage(g, TYPE_INT_RGB, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_INT_BGR, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_INT_ARGB, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_INT_ARGB_PRE, null);
3535N/A g.translate(-3*incX, incY);
3535N/A
3535N/A drawImage(g, TYPE_3BYTE_BGR, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_4BYTE_ABGR, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_4BYTE_ABGR_PRE, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_USHORT_555_RGB, null);
3535N/A g.translate(-3*incX, incY);
3535N/A
3535N/A drawImage(g, TYPE_USHORT_555_RGB, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_USHORT_GRAY, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_BYTE_GRAY, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_BYTE_INDEXED, null);
3535N/A g.translate(-3*incX, incY);
3535N/A
3535N/A drawImage(g, TYPE_BYTE_BINARY, null);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_BYTE_BINARY, icm2);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_BYTE_BINARY, icm4);
3535N/A g.translate(incX, 0);
3535N/A
3535N/A drawImage(g, TYPE_BYTE_INDEXED, icm2);
3535N/A g.translate(-3*incX, incY);
3535N/A
3535N/A drawImage(g, TYPE_BYTE_INDEXED, icm4);
3535N/A g.translate(incX, 0);
3535N/A }
3535N/A
3535N/A
3535N/A
3535N/A /* Size is chosen to match default imageable width of a NA letter
3535N/A * page. This means there will be clipping, what is clipped will
3535N/A * depend on PageFormat orientation.
3535N/A */
3535N/A public Dimension getPreferredSize() {
3535N/A return new Dimension(468, 600);
3535N/A }
3535N/A
3535N/A}