Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/swing/JCheckBox/ImageCheckboxFocus/ImageCheckboxTest.java
66646 views
1
/*
2
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Dimension;
27
import java.awt.Graphics;
28
import java.awt.image.BufferedImage;
29
import java.io.File;
30
import javax.imageio.ImageIO;
31
import javax.swing.Icon;
32
import javax.swing.JCheckBox;
33
34
/*
35
* @test
36
* @key headful
37
* @bug 8216358 8279586
38
* @summary [macos] The focus is invisible when tab to "Image Radio Buttons" and "Image CheckBoxes"
39
* @library ../../regtesthelpers/
40
* @build Util
41
* @run main ImageCheckboxTest
42
*/
43
44
public class ImageCheckboxTest {
45
public static void main(String[] args) throws Exception {
46
new ImageCheckboxTest().performTest();
47
}
48
49
public void performTest() throws Exception {
50
BufferedImage imageNoFocus = new BufferedImage(100, 50,
51
BufferedImage.TYPE_INT_ARGB);
52
BufferedImage imageFocus = new BufferedImage(100, 50,
53
BufferedImage.TYPE_INT_ARGB);
54
BufferedImage imageFocusNotPainted = new BufferedImage(100, 50,
55
BufferedImage.TYPE_INT_ARGB);
56
57
58
CustomCheckBox checkbox = new CustomCheckBox("Test", new MyIcon(Color.GREEN));
59
checkbox.setFocusPainted(true);
60
checkbox.setSize(100, 50);
61
checkbox.setFocused(false);
62
checkbox.paint(imageNoFocus.createGraphics());
63
checkbox.setFocused(true);
64
checkbox.paint(imageFocus.createGraphics());
65
66
if (Util.compareBufferedImages(imageFocus, imageNoFocus)) {
67
ImageIO.write(imageFocus, "png", new File("imageFocus.png"));
68
ImageIO.write(imageNoFocus, "png", new File("imageNoFocus.png"));
69
throw new Exception("Changing focus is not visualized");
70
}
71
72
checkbox.setFocusPainted(false);
73
checkbox.paint(imageFocusNotPainted.createGraphics());
74
75
if (!Util.compareBufferedImages(imageFocusNotPainted, imageNoFocus)) {
76
ImageIO.write(imageFocusNotPainted, "png",
77
new File("imageFocusNotPainted.png"));
78
ImageIO.write(imageFocus, "png", new File("imageFocus.png"));
79
ImageIO.write(imageNoFocus, "png", new File("imageNoFocus.png"));
80
throw new Exception("setFocusPainted(false) is ignored");
81
}
82
}
83
84
class MyIcon implements Icon {
85
Color color;
86
public MyIcon(Color color) {
87
this.color = color;
88
}
89
90
@Override
91
public void paintIcon(Component c, Graphics g, int x, int y) {
92
Color old = g.getColor();
93
g.setColor(color);
94
g.fillArc(x+2, y+2, 12, 12, 0, 360);
95
g.setColor(old);
96
}
97
98
@Override
99
public int getIconWidth() {
100
return 18;
101
}
102
103
@Override
104
public int getIconHeight() {
105
return 18;
106
}
107
}
108
109
class CustomCheckBox extends JCheckBox {
110
public CustomCheckBox(String label, Icon icon) {
111
super(label, icon);
112
}
113
114
private boolean focused = false;
115
public void setFocused(boolean focused) {
116
this.focused = focused;
117
}
118
119
@Override
120
public boolean hasFocus() {
121
return focused;
122
}
123
}
124
}
125
126