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

Setting desktop hints and mixing composites result in garbled text

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 7
    • 6
    • client-libs
    • 2d
    • b17
    • generic
    • generic
    • Verified

    Description

      I'm running the following code under JDK 6.0 on Vista. The second line of text is painted all garbled. The interesting thing here is that all three lines marked with the comments must be there:

      1. Desktop hints must be installed
      2. Another text must be painted under the default composite
      3. A custom translucent SRC_OVER composite must be installed afterwards
      ----------------

      import java.awt.*;
      import javax.swing.*;
      import java.util.*;

      public class TextRenderingBug extends JPanel {
              @Override
              protected void paintComponent(Graphics g) {
                      Graphics2D graphics = (Graphics2D) g.create();

                      // get the desktop hints and install them
                      Toolkit toolkit = Toolkit.getDefaultToolkit();
                      Map desktopHints = (Map) toolkit
                                      .getDesktopProperty("awt.font.desktophints");
                      if (desktopHints != null) {
                              // if this line is removed, everything is OK
                              graphics.addRenderingHints(desktopHints);
                      }

                      graphics.setColor(Color.black);

                      // if this line is removed, everything is OK
                      graphics.drawString("test", 10, 50);

                      // if this line is removed, everything is OK
                      graphics.setComposite(AlphaComposite.getInstance(
                                      AlphaComposite.SRC_OVER, 0.6f));
                      graphics.drawString("test", 10, 80);

                      graphics.dispose();
              }

              public static void main(String[] args) throws Exception {
                      SwingUtilities.invokeLater(new Runnable() {
                              public void run() {
                                      JFrame fr = new JFrame("cool");
                                      fr.setLayout(new BorderLayout());
                                      fr.setSize(400, 300);
                                      fr.add(new TextRenderingBug(), BorderLayout.CENTER);
                                      fr.setLocationRelativeTo(null);
                                      fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                      fr.setVisible(true);
                              }
                      });
              }
      }

      Attachments

        Issue Links

          Activity

            People

              prr Philip Race
              igor Igor Nekrestyanov (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: