Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6679308

Poor text rendering on translucent image.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 1.4.0
    • Fix Version/s: 7
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
      2d
    • Resolved In Build:
      b28
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Backports

        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);
                    }
                });
            }
        }

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                prr Philip Race
                Reporter:
                prr Philip Race
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: