Path: blob/master/test/jdk/javax/swing/JSlider/8162856/MetalHiDPISliderThumbTest.java
41153 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Dimension;24import java.awt.Graphics2D;25import java.awt.image.BufferedImage;26import javax.swing.JSlider;27import javax.swing.SwingUtilities;28import javax.swing.UIManager;29import javax.swing.plaf.metal.MetalLookAndFeel;3031/*32* @test33* @bug 816285634* @summary Bad rendering of Swing UI controls with Metal L&F on HiDPI display35* @run main MetalHiDPISliderThumbTest36*/37public class MetalHiDPISliderThumbTest {3839public static void main(String[] args) throws Exception {4041SwingUtilities.invokeAndWait(() -> {4243try {44UIManager.setLookAndFeel(new MetalLookAndFeel());45} catch (Exception e) {46throw new RuntimeException(e);47}4849if (!testSliderThumb(true)) {50throw new RuntimeException("Horizontal Slider Thumb is not scaled!");51}5253if (!testSliderThumb(false)) {54throw new RuntimeException("Vertical Slider Thumb is not scaled!");55}56});57}5859private static boolean testSliderThumb(boolean horizontal) {60int scale = 3;6162int w = horizontal ? 100 : 20;63int h = horizontal ? 20 : 100;6465JSlider testSlider = new JSlider();66testSlider.setSize(w, h);67Dimension size = new Dimension(w, h);68testSlider.setPreferredSize(size);69testSlider.setMinimumSize(size);70testSlider.setMaximumSize(size);71testSlider.setOrientation(horizontal ? JSlider.HORIZONTAL : JSlider.VERTICAL);7273int sw = scale * w;74int sh = scale * h;7576final BufferedImage img = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);7778Graphics2D g = img.createGraphics();79g.scale(scale, scale);80testSlider.paint(g);81g.dispose();8283if (horizontal) {84int y = sh / 2;8586int xMin = 0;87int rgb = img.getRGB(xMin, y);88for (int i = 0; i < sw; i++) {89if (img.getRGB(i, y) != rgb) {90xMin = i;91break;92}93}9495int xMax = sw - 1;96rgb = img.getRGB(xMax, y);97for (int i = sw - 1; i > 0; i--) {98if (img.getRGB(i, y) != rgb) {99xMax = i;100break;101}102}103104int d = 3 * scale;105int xc = (xMin + xMax) / 2 - d;106rgb = img.getRGB(xc, y);107108for (int x = xMin + d; x < xc; x++) {109if (img.getRGB(x, y) != rgb) {110return true;111}112}113} else {114int x = sw / 2;115116int yMin = 0;117int rgb = img.getRGB(x, yMin);118for (int i = 0; i < sh; i++) {119if (img.getRGB(x, i) != rgb) {120yMin = i;121break;122}123}124125int yMax = sh - 1;126rgb = img.getRGB(x, yMax);127for (int i = sh - 1; i > 0; i--) {128if (img.getRGB(x, i) != rgb) {129yMax = i;130break;131}132}133134int d = 3 * scale;135int yc = (yMin + yMax) / 2 - d;136rgb = img.getRGB(x, yc);137138for (int y = yMin + d; y < yc; y++) {139if (img.getRGB(x, y) != rgb) {140return true;141}142}143}144return false;145}146}147148149