J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6263951 |
| 27 | * @summary Text should be B&W, grayscale, and LCD. |
| 28 | * @run main/manual=yesno TextAAHintsTest |
| 29 | */ |
| 30 | import java.awt.*; |
| 31 | import java.awt.geom.*; |
| 32 | import java.awt.image.*; |
| 33 | |
| 34 | public class TextAAHintsTest extends Component { |
| 35 | |
| 36 | String black = "This text should be solid black"; |
| 37 | String gray = "This text should be gray scale anti-aliased"; |
| 38 | String lcd = "This text should be LCD sub-pixel text (coloured)."; |
| 39 | |
| 40 | public void paint(Graphics g) { |
| 41 | |
| 42 | Graphics2D g2d = (Graphics2D)g.create(); |
| 43 | g2d.setColor(Color.white); |
| 44 | g2d.fillRect(0,0,getSize().width, getSize().height); |
| 45 | |
| 46 | drawText(g.create(0, 0, 500, 100)); |
| 47 | bufferedImageText(g.create(0, 100, 500, 100)); |
| 48 | volatileImageText(g.create(0, 200, 500, 100)); |
| 49 | } |
| 50 | |
| 51 | private void drawText(Graphics g) { |
| 52 | |
| 53 | Graphics2D g2d = (Graphics2D)g; |
| 54 | |
| 55 | g2d.setColor(Color.white); |
| 56 | g2d.fillRect(0,0,500,100); |
| 57 | |
| 58 | g2d.setColor(Color.black); |
| 59 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
| 60 | RenderingHints.VALUE_ANTIALIAS_ON); |
| 61 | |
| 62 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
| 63 | RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); |
| 64 | g2d.drawString(black, 10, 20); |
| 65 | |
| 66 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
| 67 | RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); |
| 68 | g2d.drawString(black, 10, 35); |
| 69 | |
| 70 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
| 71 | RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT); |
| 72 | g2d.drawString(gray, 10, 50); |
| 73 | |
| 74 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
| 75 | RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
| 76 | g2d.drawString(gray, 10, 65); |
| 77 | |
| 78 | /* For visual comparison, render grayscale with graphics AA off */ |
| 79 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
| 80 | RenderingHints.VALUE_ANTIALIAS_OFF); |
| 81 | g2d.drawString(gray, 10, 80); |
| 82 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
| 83 | RenderingHints.VALUE_ANTIALIAS_ON); |
| 84 | |
| 85 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
| 86 | RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); |
| 87 | g2d.drawString(lcd, 10, 95); |
| 88 | } |
| 89 | |
| 90 | public void bufferedImageText(Graphics g) { |
| 91 | BufferedImage bi = |
| 92 | new BufferedImage(500, 100, BufferedImage.TYPE_INT_RGB); |
| 93 | Graphics2D g2d = bi.createGraphics(); |
| 94 | |
| 95 | drawText(g2d); |
| 96 | g.drawImage(bi, 0, 0, null); |
| 97 | } |
| 98 | |
| 99 | public VolatileImage getVolatileImage(int w, int h) { |
| 100 | VolatileImage image; |
| 101 | try { |
| 102 | image = createVolatileImage(w, h, new ImageCapabilities(true)); |
| 103 | } catch (AWTException e) { |
| 104 | System.out.println(e); |
| 105 | System.out.println("Try creating non-accelerated VI instead."); |
| 106 | try { |
| 107 | image = createVolatileImage(w, h, |
| 108 | new ImageCapabilities(false)); |
| 109 | } catch (AWTException e1) { |
| 110 | System.out.println("Skipping volatile image test."); |
| 111 | image = null; |
| 112 | } |
| 113 | } |
| 114 | return image; |
| 115 | } |
| 116 | |
| 117 | public void volatileImageText(Graphics g) { |
| 118 | VolatileImage image = getVolatileImage(500, 100); |
| 119 | if (image == null) { |
| 120 | return; |
| 121 | } |
| 122 | boolean painted = false; |
| 123 | while (!painted) { |
| 124 | int status = image.validate(getGraphicsConfiguration()); |
| 125 | if (status == VolatileImage.IMAGE_INCOMPATIBLE) { |
| 126 | image = getVolatileImage(500, 100); |
| 127 | if (image == null) { |
| 128 | return; |
| 129 | } |
| 130 | } |
| 131 | drawText(image.createGraphics()); |
| 132 | g.drawImage(image, 0, 0, null); |
| 133 | painted = !image.contentsLost(); |
| 134 | System.out.println("painted = " + painted); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public Dimension getPreferredSize() { |
| 139 | return new Dimension(500,300); |
| 140 | } |
| 141 | |
| 142 | public static void main(String[] args) throws Exception { |
| 143 | |
| 144 | Frame f = new Frame("Composite and Text Test"); |
| 145 | f.add(new TextAAHintsTest(), BorderLayout.CENTER); |
| 146 | f.pack(); |
| 147 | f.setVisible(true); |
| 148 | } |
| 149 | } |