MetalHiDPISliderThumbTest.java revision 15395:1457d59f0853
1238104Sdes/*
2238104Sdes * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3238104Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4238104Sdes *
5238104Sdes * This code is free software; you can redistribute it and/or modify it
6238104Sdes * under the terms of the GNU General Public License version 2 only, as
7238104Sdes * published by the Free Software Foundation.
8238104Sdes *
9238104Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
10238104Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11238104Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12238104Sdes * version 2 for more details (a copy is included in the LICENSE file that
13238104Sdes * accompanied this code).
14238104Sdes *
15238104Sdes * You should have received a copy of the GNU General Public License version
16238104Sdes * 2 along with this work; if not, write to the Free Software Foundation,
17238104Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18238104Sdes *
19238104Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20238104Sdes * or visit www.oracle.com if you need additional information or have any
21238104Sdes * questions.
22238104Sdes */
23238104Sdes
24238104Sdesimport java.awt.Dimension;
25238104Sdesimport java.awt.Graphics2D;
26238104Sdesimport java.awt.image.BufferedImage;
27238104Sdesimport javax.swing.JSlider;
28238104Sdesimport javax.swing.SwingUtilities;
29238104Sdesimport javax.swing.UIManager;
30238104Sdesimport javax.swing.plaf.metal.MetalLookAndFeel;
31238104Sdes
32238104Sdes/*
33238104Sdes * @test
34238104Sdes * @bug 8162856
35238104Sdes * @summary Bad rendering of Swing UI controls with Metal L&F on HiDPI display
36238104Sdes * @run main MetalHiDPISliderThumbTest
37238104Sdes */
38238104Sdespublic class MetalHiDPISliderThumbTest {
39238104Sdes
40238104Sdes    public static void main(String[] args) throws Exception {
41238104Sdes
42238104Sdes        SwingUtilities.invokeAndWait(() -> {
43238104Sdes
44238104Sdes            try {
45238104Sdes                UIManager.setLookAndFeel(new MetalLookAndFeel());
46238104Sdes            } catch (Exception e) {
47238104Sdes                throw new RuntimeException(e);
48238104Sdes            }
49238104Sdes
50238104Sdes            if (!testSliderThumb(true)) {
51238104Sdes                throw new RuntimeException("Horizontal Slider Thumb is not scaled!");
52238104Sdes            }
53238104Sdes
54238104Sdes            if (!testSliderThumb(false)) {
55238104Sdes                throw new RuntimeException("Vertical Slider Thumb is not scaled!");
56238104Sdes            }
57238104Sdes        });
58238104Sdes    }
59238104Sdes
60238104Sdes    private static boolean testSliderThumb(boolean horizontal) {
61238104Sdes        int scale = 3;
62238104Sdes
63238104Sdes        int w = horizontal ? 100 : 20;
64238104Sdes        int h = horizontal ? 20 : 100;
65238104Sdes
66238104Sdes        JSlider testSlider = new JSlider();
67238104Sdes        testSlider.setSize(w, h);
68238104Sdes        Dimension size = new Dimension(w, h);
69238104Sdes        testSlider.setPreferredSize(size);
70238104Sdes        testSlider.setMinimumSize(size);
71238104Sdes        testSlider.setMaximumSize(size);
72238104Sdes        testSlider.setOrientation(horizontal ? JSlider.HORIZONTAL : JSlider.VERTICAL);
73238104Sdes
74238104Sdes        int sw = scale * w;
75238104Sdes        int sh = scale * h;
76238104Sdes
77238104Sdes        final BufferedImage img = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
78238104Sdes
79238104Sdes        Graphics2D g = img.createGraphics();
80238104Sdes        g.scale(scale, scale);
81238104Sdes        testSlider.paint(g);
82238104Sdes        g.dispose();
83238104Sdes
84238104Sdes        if (horizontal) {
85238104Sdes            int y = sh / 2;
86238104Sdes
87238104Sdes            int xMin = 0;
88238104Sdes            int rgb = img.getRGB(xMin, y);
89238104Sdes            for (int i = 0; i < sw; i++) {
90238104Sdes                if (img.getRGB(i, y) != rgb) {
91238104Sdes                    xMin = i;
92238104Sdes                    break;
93238104Sdes                }
94238104Sdes            }
95238104Sdes
96238104Sdes            int xMax = sw - 1;
97238104Sdes            rgb = img.getRGB(xMax, y);
98238104Sdes            for (int i = sw - 1; i > 0; i--) {
99238104Sdes                if (img.getRGB(i, y) != rgb) {
100238104Sdes                    xMax = i;
101238104Sdes                    break;
102238104Sdes                }
103238104Sdes            }
104238104Sdes
105238104Sdes            int d = 3 * scale;
106238104Sdes            int xc = (xMin + xMax) / 2 - d;
107238104Sdes            rgb = img.getRGB(xc, y);
108238104Sdes
109238104Sdes            for (int x = xMin + d; x < xc; x++) {
110238104Sdes                if (img.getRGB(x, y) != rgb) {
111238104Sdes                    return true;
112238104Sdes                }
113238104Sdes            }
114238104Sdes        } else {
115238104Sdes            int x = sw / 2;
116238104Sdes
117238104Sdes            int yMin = 0;
118238104Sdes            int rgb = img.getRGB(x, yMin);
119238104Sdes            for (int i = 0; i < sh; i++) {
120238104Sdes                if (img.getRGB(x, i) != rgb) {
121238104Sdes                    yMin = i;
122238104Sdes                    break;
123238104Sdes                }
124238104Sdes            }
125238104Sdes
126238104Sdes            int yMax = sh - 1;
127238104Sdes            rgb = img.getRGB(x, yMax);
128238104Sdes            for (int i = sh - 1; i > 0; i--) {
129238104Sdes                if (img.getRGB(x, i) != rgb) {
130238104Sdes                    yMax = i;
131238104Sdes                    break;
132238104Sdes                }
133238104Sdes            }
134238104Sdes
135238104Sdes            int d = 3 * scale;
136238104Sdes            int yc = (yMin + yMax) / 2 - d;
137238104Sdes            rgb = img.getRGB(x, yc);
138238104Sdes
139238104Sdes            for (int y = yMin + d; y < yc; y++) {
140238104Sdes                if (img.getRGB(x, y) != rgb) {
141238104Sdes                    return true;
142238104Sdes                }
143238104Sdes            }
144238104Sdes        }
145238104Sdes        return false;
146238104Sdes    }
147238104Sdes}
148238104Sdes