United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6679308 Poor text rendering on translucent image.
JDK-6679308 : Poor text rendering on translucent image.

Details
Type:
Bug
Submit Date:
2008-03-24
Status:
Closed
Updated Date:
2011-03-07
Project Name:
JDK
Resolved Date:
2011-03-07
Component:
client-libs
OS:
generic
Sub-Component:
2d
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.4.0
Fixed Versions:

Related Reports
Backport:

Sub Tasks

Description
The test below from ###@###.### shows that text on a translucent
image renders poorly.


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * @author Chris Campbell
 */
public class TextTest2 extends JPanel {

    private boolean useImage = false;

    public TextTest2() {
        setBackground(new Color(0.3f, 0.3f, 0.3f));
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                useImage = !useImage;
                repaint();
            }
        });
    }

    private void drawText(Graphics2D g) {
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                           RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setColor(Color.WHITE);
        g.setFont(new Font("sansserif", Font.PLAIN, 70));
        g.drawString("Hello!", 20, 200);
        g.setFont(new Font("sansserif", Font.PLAIN, 12));
        g.drawString("Hello!", 20, 230);
        g.setFont(new Font("sansserif", Font.PLAIN, 10));
        g.drawString("Hello!", 20, 250);
    }

    @Override
    public void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getHeight();
        g.setColor(getBackground());
        g.fillRect(0, 0, w, h);
        if (useImage) {
            BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics2D g2 = img.createGraphics();
            drawText(g2);
            g.drawImage(img, 0, 0, null);
            g2.dispose();
        } else {
            drawText((Graphics2D)g);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(280, 380);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                TextTest2 test = new TextTest2();
                frame.add(test);
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

                                    

Comments
EVALUATION

###@###.### identified that problem was in the glyph blending loop
which produced premultiplied results, but then used it in a way that assumed
non-premultiplied results. This bug has been around since at least 1.4.
Also the SPARC VIS loops turn out to have very similar issues.
                                     
2008-03-24



Hardware and Software, Engineered to Work Together