8160986: Bad rendering of Swing UI controls with Metal L&F on HiDPI display
Reviewed-by: prr
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalComboBoxIcon.java Tue Aug 09 13:55:27 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalComboBoxIcon.java Tue Aug 09 12:08:51 2016 +0300
@@ -52,13 +52,11 @@
g.translate( x, y );
- g.setColor( component.isEnabled() ? MetalLookAndFeel.getControlInfo() : MetalLookAndFeel.getControlShadow() );
- g.drawLine( 0, 0, iconWidth - 1, 0 );
- g.drawLine( 1, 1, 1 + (iconWidth - 3), 1 );
- g.drawLine( 2, 2, 2 + (iconWidth - 5), 2 );
- g.drawLine( 3, 3, 3 + (iconWidth - 7), 3 );
- g.drawLine( 4, 4, 4 + (iconWidth - 9), 4 );
-
+ g.setColor(component.isEnabled()
+ ? MetalLookAndFeel.getControlInfo()
+ : MetalLookAndFeel.getControlShadow());
+ g.fillPolygon(new int[]{0, 5, iconWidth - 5, iconWidth},
+ new int[]{0, 5, 5, 0}, 4);
g.translate( -x, -y );
}
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalIconFactory.java Tue Aug 09 13:55:27 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalIconFactory.java Tue Aug 09 12:08:51 2016 +0300
@@ -1289,11 +1289,7 @@
else {
g.setColor(MetalLookAndFeel.getControl());
}
- g.fillRect(2, 2, 8, 8);
- g.fillRect(4, 1, 4, 1);
- g.fillRect(4, 10, 4, 1);
- g.fillRect(1, 4, 1, 4);
- g.fillRect(10, 4, 1, 4);
+ g.fillOval(1, 1, 9, 9);
}
// draw Dark Circle (start at top, go clockwise)
@@ -1303,35 +1299,14 @@
else {
g.setColor(MetalLookAndFeel.getControlDarkShadow());
}
- g.drawLine( 4, 0, 7, 0);
- g.drawLine( 8, 1, 9, 1);
- g.drawLine(10, 2, 10, 3);
- g.drawLine(11, 4, 11, 7);
- g.drawLine(10, 8, 10, 9);
- g.drawLine( 9,10, 8,10);
- g.drawLine( 7,11, 4,11);
- g.drawLine( 3,10, 2,10);
- g.drawLine( 1, 9, 1, 8);
- g.drawLine( 0, 7, 0, 4);
- g.drawLine( 1, 3, 1, 2);
- g.drawLine( 2, 1, 3, 1);
+ g.drawOval(0, 0, 11, 11);
if (pressed) {
- g.fillRect(1, 4, 1, 4);
- g.fillRect(2, 2, 1, 2);
- g.fillRect(3, 2, 1, 1);
- g.fillRect(4, 1, 4, 1);
+ g.drawArc(1, 1, 10, 10, 60, 160);
}
else if (rollover) {
g.setColor(MetalLookAndFeel.getPrimaryControl());
- g.fillRect(4, 1, 4, 2);
- g.fillRect(8, 2, 2, 2);
- g.fillRect(9, 4, 2, 4);
- g.fillRect(8, 8, 2, 2);
- g.fillRect(4, 9, 4, 2);
- g.fillRect(2, 8, 2, 2);
- g.fillRect(1, 4, 2, 4);
- g.fillRect(2, 2, 2, 2);
+ g.drawOval(2, 2, 7, 7);
}
// selected dot
@@ -1341,11 +1316,7 @@
} else {
g.setColor(MetalLookAndFeel.getControlDarkShadow());
}
- g.fillRect( 4, 4, 4, 4);
- g.drawLine( 4, 3, 7, 3);
- g.drawLine( 8, 4, 8, 7);
- g.drawLine( 7, 8, 4, 8);
- g.drawLine( 3, 7, 3, 4);
+ g.fillOval(2, 2, 7, 7);
}
g.translate(-x, -y);
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalScrollButton.java Tue Aug 09 13:55:27 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalScrollButton.java Tue Aug 09 12:08:51 2016 +0300
@@ -128,9 +128,10 @@
int startY = ((h+1) - arrowHeight) / 2;
int startX = (w / 2);
- for (int line = 0; line < arrowHeight; line++) {
- g.drawLine( startX-line, startY+line, startX +line+1, startY+line);
- }
+ g.translate(startX, startY);
+ g.fillPolygon(new int[]{0, 1, arrowHeight + 1, -arrowHeight},
+ new int[]{0, 0, arrowHeight, arrowHeight}, 4);
+ g.translate(-startX, -startY);
if (isEnabled) {
g.setColor( highlightColor );
@@ -174,10 +175,10 @@
int startY = (((h+1) - arrowHeight) / 2)+ arrowHeight-1;
int startX = (w / 2);
-
- for (int line = 0; line < arrowHeight; line++) {
- g.drawLine( startX-line, startY-line, startX +line+1, startY-line);
- }
+ g.translate(startX, startY);
+ g.fillPolygon(new int[]{0, 1, arrowHeight + 1, -arrowHeight},
+ new int[]{0, 0, -arrowHeight, -arrowHeight}, 4);
+ g.translate(-startX, -startY);
if (isEnabled) {
g.setColor( highlightColor );
@@ -220,9 +221,10 @@
int startX = (((w+1) - arrowHeight) / 2) + arrowHeight-1;
int startY = (h / 2);
- for (int line = 0; line < arrowHeight; line++) {
- g.drawLine( startX-line, startY-line, startX -line, startY+line+1);
- }
+ g.translate(startX, startY);
+ g.fillPolygon(new int[]{0, 0, -arrowHeight, -arrowHeight},
+ new int[]{0, 1, arrowHeight + 1, -arrowHeight}, 4);
+ g.translate(-startX, -startY);
if (isEnabled) {
g.setColor( highlightColor );
@@ -262,10 +264,10 @@
int startX = (((w+1) - arrowHeight) / 2);
int startY = (h / 2);
-
- for (int line = 0; line < arrowHeight; line++) {
- g.drawLine( startX+line, startY-line, startX +line, startY+line+1);
- }
+ g.translate(startX, startY);
+ g.fillPolygon(new int[]{0, 0, arrowHeight, arrowHeight},
+ new int[]{0, 1, arrowHeight + 1, -arrowHeight}, 4);
+ g.translate(-startX, -startY);
if (isEnabled) {
g.setColor( highlightColor );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/plaf/metal/MetalIcons/MetalHiDPIIconsTest.java Tue Aug 09 12:08:51 2016 +0300
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8160986
+ * @summary Bad rendering of Swing UI controls with Metal L&F on HiDPI display
+ * @run main/manual MetalHiDPIIconsTest
+ */
+import java.awt.Color;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JTextArea;
+import javax.swing.SwingUtilities;
+
+public class MetalHiDPIIconsTest {
+
+ private static volatile boolean testResult = false;
+ private static volatile CountDownLatch countDownLatch;
+ private static final String INSTRUCTIONS = "INSTRUCTIONS:\n"
+ + "Verify that icons are painted smoothly for standard Swing UI controls.\n\n"
+ + "If the display does not support HiDPI mode press PASS.\n\n"
+ + "1. Run the SwingSet2 demo on HiDPI Display.\n"
+ + "2. Select Metal Look and Feel\n"
+ + "3. Check that the icons are painted smoothly on Swing UI controls like:\n"
+ + " - JRadioButton\n"
+ + " - JCheckBox\n"
+ + " - JComboBox\n"
+ + " - JScrollPane (vertical and horizontal scroll bars)\n"
+ + "and others...\n\n"
+ + "If so, press PASS, else press FAIL.\n";
+
+ public static void main(String args[]) throws Exception {
+ countDownLatch = new CountDownLatch(1);
+
+ SwingUtilities.invokeLater(MetalHiDPIIconsTest::createUI);
+ countDownLatch.await(15, TimeUnit.MINUTES);
+
+ if (!testResult) {
+ throw new RuntimeException("Test fails!");
+ }
+ }
+
+ private static void createUI() {
+
+ final JFrame mainFrame = new JFrame("Metal L&F icons test");
+ GridBagLayout layout = new GridBagLayout();
+ JPanel mainControlPanel = new JPanel(layout);
+ JPanel resultButtonPanel = new JPanel(layout);
+
+ GridBagConstraints gbc = new GridBagConstraints();
+
+ JTextArea instructionTextArea = new JTextArea();
+ instructionTextArea.setText(INSTRUCTIONS);
+ instructionTextArea.setEditable(false);
+ instructionTextArea.setBackground(Color.white);
+
+ gbc.gridx = 0;
+ gbc.gridy = 0;
+ gbc.fill = GridBagConstraints.HORIZONTAL;
+ mainControlPanel.add(instructionTextArea, gbc);
+
+ JButton passButton = new JButton("Pass");
+ passButton.setActionCommand("Pass");
+ passButton.addActionListener((ActionEvent e) -> {
+ testResult = true;
+ mainFrame.dispose();
+ countDownLatch.countDown();
+
+ });
+
+ JButton failButton = new JButton("Fail");
+ failButton.setActionCommand("Fail");
+ failButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ mainFrame.dispose();
+ countDownLatch.countDown();
+ }
+ });
+
+ gbc.gridx = 0;
+ gbc.gridy = 0;
+ resultButtonPanel.add(passButton, gbc);
+
+ gbc.gridx = 1;
+ gbc.gridy = 0;
+ resultButtonPanel.add(failButton, gbc);
+
+ gbc.gridx = 0;
+ gbc.gridy = 1;
+ mainControlPanel.add(resultButtonPanel, gbc);
+
+ mainFrame.add(mainControlPanel);
+ mainFrame.pack();
+
+ mainFrame.addWindowListener(new WindowAdapter() {
+
+ @Override
+ public void windowClosing(WindowEvent e) {
+ mainFrame.dispose();
+ countDownLatch.countDown();
+ }
+ });
+ mainFrame.setVisible(true);
+ }
+}