jdk/test/java/awt/Graphics/TextAAHintsTest.java
author cl
Fri, 18 Feb 2011 14:23:48 -0800
changeset 8267 6a5f458575cf
parent 5506 202f599c92aa
permissions -rw-r--r--
Added tag jdk7-b130 for changeset 8924242a88c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6263951
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Text should be B&W, grayscale, and LCD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/manual=yesno TextAAHintsTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.geom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class TextAAHintsTest extends Component {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    String black = "This text should be solid black";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    String gray  = "This text should be gray scale anti-aliased";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    String lcd   = "This text should be LCD sub-pixel text (coloured).";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        Graphics2D g2d = (Graphics2D)g.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        g2d.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        g2d.fillRect(0,0,getSize().width, getSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        drawText(g.create(0, 0, 500, 100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        bufferedImageText(g.create(0, 100, 500, 100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        volatileImageText(g.create(0, 200, 500, 100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private void drawText(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Graphics2D g2d = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        g2d.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        g2d.fillRect(0,0,500,100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        g2d.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                             RenderingHints.VALUE_ANTIALIAS_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                             RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        g2d.drawString(black, 10, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                             RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        g2d.drawString(black, 10, 35);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                             RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        g2d.drawString(gray, 10, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                             RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        g2d.drawString(gray, 10, 65);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        /* For visual comparison, render grayscale with graphics AA off */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                             RenderingHints.VALUE_ANTIALIAS_OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        g2d.drawString(gray, 10, 80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                             RenderingHints.VALUE_ANTIALIAS_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                             RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        g2d.drawString(lcd, 10, 95);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void bufferedImageText(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        BufferedImage bi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                 new BufferedImage(500, 100, BufferedImage.TYPE_INT_RGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Graphics2D g2d = bi.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        drawText(g2d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        g.drawImage(bi, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public VolatileImage getVolatileImage(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        VolatileImage image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            image = createVolatileImage(w, h, new ImageCapabilities(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } catch (AWTException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.out.println("Try creating non-accelerated VI instead.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                image = createVolatileImage(w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                            new ImageCapabilities(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } catch (AWTException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                System.out.println("Skipping volatile image test.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void volatileImageText(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        VolatileImage image = getVolatileImage(500, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        boolean painted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while (!painted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            int status = image.validate(getGraphicsConfiguration());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (status == VolatileImage.IMAGE_INCOMPATIBLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                image = getVolatileImage(500, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            drawText(image.createGraphics());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            g.drawImage(image, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            painted = !image.contentsLost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println("painted = " + painted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return new Dimension(500,300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Frame f = new Frame("Composite and Text Test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        f.add(new TextAAHintsTest(), BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        f.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        f.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}