Path: blob/master/test/jdk/javax/swing/JCheckBox/ImageCheckboxFocus/ImageCheckboxTest.java
66646 views
/*1* Copyright (c) 2021, 2022, 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.Component;25import java.awt.Dimension;26import java.awt.Graphics;27import java.awt.image.BufferedImage;28import java.io.File;29import javax.imageio.ImageIO;30import javax.swing.Icon;31import javax.swing.JCheckBox;3233/*34* @test35* @key headful36* @bug 8216358 827958637* @summary [macos] The focus is invisible when tab to "Image Radio Buttons" and "Image CheckBoxes"38* @library ../../regtesthelpers/39* @build Util40* @run main ImageCheckboxTest41*/4243public class ImageCheckboxTest {44public static void main(String[] args) throws Exception {45new ImageCheckboxTest().performTest();46}4748public void performTest() throws Exception {49BufferedImage imageNoFocus = new BufferedImage(100, 50,50BufferedImage.TYPE_INT_ARGB);51BufferedImage imageFocus = new BufferedImage(100, 50,52BufferedImage.TYPE_INT_ARGB);53BufferedImage imageFocusNotPainted = new BufferedImage(100, 50,54BufferedImage.TYPE_INT_ARGB);555657CustomCheckBox checkbox = new CustomCheckBox("Test", new MyIcon(Color.GREEN));58checkbox.setFocusPainted(true);59checkbox.setSize(100, 50);60checkbox.setFocused(false);61checkbox.paint(imageNoFocus.createGraphics());62checkbox.setFocused(true);63checkbox.paint(imageFocus.createGraphics());6465if (Util.compareBufferedImages(imageFocus, imageNoFocus)) {66ImageIO.write(imageFocus, "png", new File("imageFocus.png"));67ImageIO.write(imageNoFocus, "png", new File("imageNoFocus.png"));68throw new Exception("Changing focus is not visualized");69}7071checkbox.setFocusPainted(false);72checkbox.paint(imageFocusNotPainted.createGraphics());7374if (!Util.compareBufferedImages(imageFocusNotPainted, imageNoFocus)) {75ImageIO.write(imageFocusNotPainted, "png",76new File("imageFocusNotPainted.png"));77ImageIO.write(imageFocus, "png", new File("imageFocus.png"));78ImageIO.write(imageNoFocus, "png", new File("imageNoFocus.png"));79throw new Exception("setFocusPainted(false) is ignored");80}81}8283class MyIcon implements Icon {84Color color;85public MyIcon(Color color) {86this.color = color;87}8889@Override90public void paintIcon(Component c, Graphics g, int x, int y) {91Color old = g.getColor();92g.setColor(color);93g.fillArc(x+2, y+2, 12, 12, 0, 360);94g.setColor(old);95}9697@Override98public int getIconWidth() {99return 18;100}101102@Override103public int getIconHeight() {104return 18;105}106}107108class CustomCheckBox extends JCheckBox {109public CustomCheckBox(String label, Icon icon) {110super(label, icon);111}112113private boolean focused = false;114public void setFocused(boolean focused) {115this.focused = focused;116}117118@Override119public boolean hasFocus() {120return focused;121}122}123}124125126