Path: blob/master/test/jdk/javax/swing/JSlider/Thumb/PaintThumbSize.java
41153 views
/*1* Copyright (c) 2020, 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.Color;24import java.awt.EventQueue;25import java.awt.Graphics2D;26import java.awt.image.BufferedImage;27import java.io.File;28import java.io.IOException;2930import javax.imageio.ImageIO;31import javax.swing.JSlider;32import javax.swing.UIManager;33import javax.swing.UnsupportedLookAndFeelException;34import javax.swing.plaf.SliderUI;35import javax.swing.plaf.basic.BasicSliderUI;36import javax.swing.plaf.metal.DefaultMetalTheme;37import javax.swing.plaf.metal.MetalLookAndFeel;3839import static java.awt.image.BufferedImage.TYPE_INT_ARGB;40import static javax.swing.UIManager.getInstalledLookAndFeels;4142/**43* @test44* @bug 825671345* @key headful46* @summary The thumb should not touch pixels outside its location.47*/48public final class PaintThumbSize {4950private static final int SCALE = 2;51private static final int SHIFT = 100;5253public static void main(String[] args) throws Exception {54for (UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {55EventQueue.invokeAndWait(() -> setLookAndFeel(laf));56EventQueue.invokeAndWait(PaintThumbSize::test);57if (laf.getClassName().contains("Metal")) {58EventQueue.invokeAndWait(() -> {59System.err.println("\tAdditional theme: DefaultMetalTheme");60MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());61test();62});63}64}65}6667private static void test() {68BufferedImage bi = new BufferedImage(500, 500, TYPE_INT_ARGB);69Graphics2D g = bi.createGraphics();70g.setColor(Color.CYAN);71g.fillRect(0, 0, bi.getWidth(), bi.getHeight());72g.setColor(Color.BLACK);7374g.scale(SCALE, SCALE);75g.translate(SHIFT, SHIFT);7677JSlider slider = new JSlider();78SliderUI ui = slider.getUI();79if (ui instanceof BasicSliderUI) {80BasicSliderUI bui = (BasicSliderUI) ui;81bui.setThumbLocation(0, 0);82bui.paintThumb(g);8384for (int y = 0; y < bi.getHeight(); ++y) {85for (int x = 0; x < bi.getWidth(); ++x) {86if (x >= SHIFT * SCALE && y >= SHIFT * SCALE) {87continue;88}89if (bi.getRGB(x, y) != Color.CYAN.getRGB()) {90System.err.println("x = " + x);91System.err.println("y = " + y);92try {93ImageIO.write(bi,"png", new File("image.png"));94} catch (IOException e) {95e.printStackTrace();96}97throw new RuntimeException("Wrong color");98}99}100}101}102g.dispose();103}104105106private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {107try {108System.err.println("LookAndFeel: " + laf.getClassName());109UIManager.setLookAndFeel(laf.getClassName());110} catch (UnsupportedLookAndFeelException ignored) {111System.err.println("Unsupported LookAndFeel: " + laf.getClassName());112} catch (ClassNotFoundException | InstantiationException |113IllegalAccessException e) {114throw new RuntimeException(e);115}116}117}118119120