8176883: Enable antialiasing for Metal L&F icons on HiDPI display
authoralexsch
Thu, 16 Mar 2017 23:29:52 +0300
changeset 44353 eb56196e2a3a
parent 44352 fd086da7c916
child 44354 a99429b721a9
8176883: Enable antialiasing for Metal L&F icons on HiDPI display Reviewed-by: prr
jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalIconFactory.java
jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java
jdk/test/javax/swing/plaf/metal/MetalIcons/MetalHiDPIIconsTest.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalIconFactory.java	Thu Mar 16 09:51:15 2017 -0700
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalIconFactory.java	Thu Mar 16 23:29:52 2017 +0300
@@ -33,6 +33,8 @@
 import java.util.Enumeration;
 import java.util.Vector;
 import sun.swing.CachedPainter;
+import static sun.swing.SwingUtilities2.setAntialiasingHintForScaledGraphics;
+import static sun.swing.SwingUtilities2.getAndSetAntialisingHintForScaledGraphics;
 
 /**
  * Factory object that vends <code>Icon</code>s for
@@ -1329,8 +1331,12 @@
         }
 
         public void paintIcon(Component c, Graphics g, int x, int y) {
+
+            Object aaHint = getAndSetAntialisingHintForScaledGraphics(g);
+
             if (MetalLookAndFeel.usingOcean()) {
                 paintOceanIcon(c, g, x, y);
+                setAntialiasingHintForScaledGraphics(g, aaHint);
                 return;
             }
             JRadioButton rb = (JRadioButton)c;
@@ -1382,6 +1388,7 @@
             }
 
             g.translate(-x, -y);
+            setAntialiasingHintForScaledGraphics(g, aaHint);
         }
 
         public int getIconWidth() {
@@ -2271,8 +2278,12 @@
 
         public void paintIcon( Component c, Graphics g, int x, int y )
         {
+
+            Object aaHint = getAndSetAntialisingHintForScaledGraphics(g);
+
             if (MetalLookAndFeel.usingOcean()) {
                 paintOceanIcon(c, g, x, y);
+                setAntialiasingHintForScaledGraphics(g, aaHint);
                 return;
             }
             JMenuItem b = (JMenuItem) c;
@@ -2333,6 +2344,7 @@
             }
 
             g.translate( -x, -y );
+            setAntialiasingHintForScaledGraphics(g, aaHint);
         }
 
         public int getIconWidth() { return menuCheckIconSize.width; }
--- a/jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java	Thu Mar 16 09:51:15 2017 -0700
+++ b/jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java	Thu Mar 16 23:29:52 2017 +0300
@@ -2164,6 +2164,41 @@
         return false;
     }
 
+    /**
+     * Enables the antialiasing rendering hint for the scaled graphics and
+     * returns the previous hint value.
+     * The returned null value indicates that the passed graphics is not
+     * instance of Graphics2D.
+     *
+     * @param g the graphics
+     * @return the previous antialiasing rendering hint value if the passed
+     * graphics is instance of Graphics2D, null otherwise.
+     */
+    public static Object getAndSetAntialisingHintForScaledGraphics(Graphics g) {
+        if (isScaledGraphics(g) && isLocalDisplay()) {
+            Graphics2D g2d = (Graphics2D) g;
+            Object hint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
+            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+                    RenderingHints.VALUE_ANTIALIAS_ON);
+            return hint;
+        }
+        return null;
+    }
+
+    /**
+     * Sets the antialiasing rendering hint if its value is not null.
+     * Null hint value indicates that the passed graphics is not instance of
+     * Graphics2D.
+     *
+     * @param g the graphics
+     * @param hint the antialiasing rendering hint
+     */
+    public static void setAntialiasingHintForScaledGraphics(Graphics g, Object hint) {
+        if (hint != null) {
+            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, hint);
+        }
+    }
+
     public static boolean isFloatingPointScale(AffineTransform tx) {
         int type = tx.getType() & ~(TYPE_FLIP | TYPE_TRANSLATION);
         if (type == 0) {
--- a/jdk/test/javax/swing/plaf/metal/MetalIcons/MetalHiDPIIconsTest.java	Thu Mar 16 09:51:15 2017 -0700
+++ b/jdk/test/javax/swing/plaf/metal/MetalIcons/MetalHiDPIIconsTest.java	Thu Mar 16 23:29:52 2017 +0300
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8160986 8174845
+ * @bug 8160986 8174845 8176883
  * @summary Bad rendering of Swing UI controls with Metal L&F on HiDPI display
  * @run main/manual MetalHiDPIIconsTest
  */