1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.awt.Dimension;
25import java.awt.Graphics2D;
26import java.awt.image.BufferedImage;
27import javax.swing.JSlider;
28import javax.swing.SwingUtilities;
29import javax.swing.UIManager;
30import javax.swing.plaf.metal.MetalLookAndFeel;
31
32/*
33 * @test
34 * @bug 8162856
35 * @summary Bad rendering of Swing UI controls with Metal L&F on HiDPI display
36 * @run main MetalHiDPISliderThumbTest
37 */
38public class MetalHiDPISliderThumbTest {
39
40    public static void main(String[] args) throws Exception {
41
42        SwingUtilities.invokeAndWait(() -> {
43
44            try {
45                UIManager.setLookAndFeel(new MetalLookAndFeel());
46            } catch (Exception e) {
47                throw new RuntimeException(e);
48            }
49
50            if (!testSliderThumb(true)) {
51                throw new RuntimeException("Horizontal Slider Thumb is not scaled!");
52            }
53
54            if (!testSliderThumb(false)) {
55                throw new RuntimeException("Vertical Slider Thumb is not scaled!");
56            }
57        });
58    }
59
60    private static boolean testSliderThumb(boolean horizontal) {
61        int scale = 3;
62
63        int w = horizontal ? 100 : 20;
64        int h = horizontal ? 20 : 100;
65
66        JSlider testSlider = new JSlider();
67        testSlider.setSize(w, h);
68        Dimension size = new Dimension(w, h);
69        testSlider.setPreferredSize(size);
70        testSlider.setMinimumSize(size);
71        testSlider.setMaximumSize(size);
72        testSlider.setOrientation(horizontal ? JSlider.HORIZONTAL : JSlider.VERTICAL);
73
74        int sw = scale * w;
75        int sh = scale * h;
76
77        final BufferedImage img = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
78
79        Graphics2D g = img.createGraphics();
80        g.scale(scale, scale);
81        testSlider.paint(g);
82        g.dispose();
83
84        if (horizontal) {
85            int y = sh / 2;
86
87            int xMin = 0;
88            int rgb = img.getRGB(xMin, y);
89            for (int i = 0; i < sw; i++) {
90                if (img.getRGB(i, y) != rgb) {
91                    xMin = i;
92                    break;
93                }
94            }
95
96            int xMax = sw - 1;
97            rgb = img.getRGB(xMax, y);
98            for (int i = sw - 1; i > 0; i--) {
99                if (img.getRGB(i, y) != rgb) {
100                    xMax = i;
101                    break;
102                }
103            }
104
105            int d = 3 * scale;
106            int xc = (xMin + xMax) / 2 - d;
107            rgb = img.getRGB(xc, y);
108
109            for (int x = xMin + d; x < xc; x++) {
110                if (img.getRGB(x, y) != rgb) {
111                    return true;
112                }
113            }
114        } else {
115            int x = sw / 2;
116
117            int yMin = 0;
118            int rgb = img.getRGB(x, yMin);
119            for (int i = 0; i < sh; i++) {
120                if (img.getRGB(x, i) != rgb) {
121                    yMin = i;
122                    break;
123                }
124            }
125
126            int yMax = sh - 1;
127            rgb = img.getRGB(x, yMax);
128            for (int i = sh - 1; i > 0; i--) {
129                if (img.getRGB(x, i) != rgb) {
130                    yMax = i;
131                    break;
132                }
133            }
134
135            int d = 3 * scale;
136            int yc = (yMin + yMax) / 2 - d;
137            rgb = img.getRGB(x, yc);
138
139            for (int y = yMin + d; y < yc; y++) {
140                if (img.getRGB(x, y) != rgb) {
141                    return true;
142                }
143            }
144        }
145        return false;
146    }
147}
148