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

[macos] AlphaComposite.Clear is ignored when RenderingHints.KEY_TEXT_ANTIALIASING is ON

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P3
    • Resolution: Unresolved
    • Affects Version/s: 9, 11, 13, 14, 15
    • Fix Version/s: tbd
    • Component/s: client-libs
    • Labels:
    • Environment:

      macOS 10.14 (18A391)

      openjdk version "11" 2018-09-25

      OpenJDK Runtime Environment 18.9 (build 11+28)

      OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

    • Subcomponent:
      2d
    • OS:
      os_x

      Description

      When trying to draw a string with alpha composite mode "clear", nothing is drawn (or rather removed), when RenderingHints.KEY_TEXT_ANTIALIASING, is set to RenderingHints.VALUE_TEXT_ANTIALIAS_ON.

      Demo code
      =========

      import javax.swing.*;
      import java.awt.*;
      import java.awt.image.BufferedImage;

      public class CompositeDemo {

          public static void main(final String[] args) {
              final ImageIcon imageIconWithAntiAliasing = createIcon(true);
              final ImageIcon imageIconWithoutAntiAliasing = createIcon(false);
              final JFrame frame = new JFrame();
              frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              frame.getContentPane().setLayout(new FlowLayout());
              frame.getContentPane().add(new JLabel(imageIconWithAntiAliasing));
              frame.getContentPane().add(new JLabel(imageIconWithoutAntiAliasing));
              SwingUtilities.invokeLater(() -> {
                  frame.setSize(250, 100);
                  frame.setVisible(true);
              });
          }

          private static ImageIcon createIcon(final boolean textAntiAlias) {
              final BufferedImage image = new BufferedImage(100, 50, BufferedImage.TYPE_INT_ARGB);
              final Graphics2D g = (Graphics2D)image.getGraphics();
              if (textAntiAlias) {
                  g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
              }

              g.setColor(Color.BLACK);
              g.drawRect(0, 0, 99, 49);
              g.setColor(Color.RED);
              g.fillRect(0, 0, 50, 50);
              g.setComposite(AlphaComposite.Clear);
              g.setColor(Color.BLUE);
              g.drawString("sdfegergerwgrtwgtrw", 0, 25);

              return new ImageIcon(image);
          }
      }

      A screenshot is attached. The text should be visible in both labels, but is only visible in the one without text anti-aliasing.

      Workaround:
      Change the AlphaComposite rule to AlphaComposite.DstOut.

        Attachments

        1. 11_09.png
          11_09.png
          4 kB
        2. 11.png
          11.png
          4 kB
        3. 8u261.png
          8u261.png
          5 kB
        4. 9.png
          9.png
          3 kB
        5. Screen Shot 2018-10-17 at 09.55.14.png
          Screen Shot 2018-10-17 at 09.55.14.png
          94 kB

          Activity

            People

            Assignee:
            jdv Jayathirth D V
            Reporter:
            hschreiber Hendrik Schreiber
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: