Path: blob/master/test/jdk/javax/swing/JCheckBox/ImageCheckboxFocus/ImageCheckboxTest.java
41154 views
/*1* Copyright (c) 2021, 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 821635837* @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);5354CustomCheckBox checkbox = new CustomCheckBox("Test", new MyIcon(Color.GREEN));55checkbox.setSize(100, 50);56checkbox.setFocused(false);57checkbox.paint(imageNoFocus.createGraphics());58checkbox.setFocused(true);59checkbox.paint(imageFocus.createGraphics());6061if (Util.compareBufferedImages(imageFocus, imageNoFocus)) {62ImageIO.write(imageFocus, "png", new File("imageFocus.png"));63ImageIO.write(imageNoFocus, "png", new File("imageNoFocus.png"));64throw new Exception("Changing focus is not visualized");65}66}6768class MyIcon implements Icon {69Color color;70public MyIcon(Color color) {71this.color = color;72}7374@Override75public void paintIcon(Component c, Graphics g, int x, int y) {76Color old = g.getColor();77g.setColor(color);78g.fillArc(x+2, y+2, 12, 12, 0, 360);79g.setColor(old);80}8182@Override83public int getIconWidth() {84return 18;85}8687@Override88public int getIconHeight() {89return 18;90}91}9293class CustomCheckBox extends JCheckBox {94public CustomCheckBox(String label, Icon icon) {95super(label, icon);96}9798private boolean focused = false;99public void setFocused(boolean focused) {100this.focused = focused;101}102103@Override104public boolean hasFocus() {105return focused;106}107}108}109110111