|
| 1 | +/* |
| 2 | + * Copyright (c) 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 distributed in the hope that it will be useful, but WITHOUT |
| 6 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 7 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 8 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 9 | + * accompanied this code). |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public License version |
| 12 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 13 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 14 | + * |
| 15 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 16 | + * or visit www.oracle.com if you need additional information or have any |
| 17 | + * questions. |
| 18 | + */ |
| 19 | + |
| 20 | +import java.awt.BorderLayout; |
| 21 | +import java.awt.Color; |
| 22 | +import java.awt.Component; |
| 23 | +import java.awt.Dimension; |
| 24 | +import java.awt.Graphics2D; |
| 25 | +import java.awt.Point; |
| 26 | +import java.awt.image.BufferedImage; |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import javax.imageio.ImageIO; |
| 33 | +import javax.swing.BorderFactory; |
| 34 | +import javax.swing.Box; |
| 35 | +import javax.swing.BoxLayout; |
| 36 | +import javax.swing.JFrame; |
| 37 | +import javax.swing.JPanel; |
| 38 | +import javax.swing.SwingUtilities; |
| 39 | + |
| 40 | +/* |
| 41 | + * @test |
| 42 | + * @bug 8279614 |
| 43 | + * @summary The left line of the TitledBorder is not painted on 150 scale factor |
| 44 | + * @requires (os.family == "windows") |
| 45 | + * @run main ScaledEtchedBorderTest |
| 46 | + */ |
| 47 | + |
| 48 | +public class ScaledEtchedBorderTest { |
| 49 | + |
| 50 | + public static final Dimension SIZE = new Dimension(120, 20); |
| 51 | + |
| 52 | + public static Color highlight = Color.RED; |
| 53 | + public static Color shadow = Color.BLUE; |
| 54 | + |
| 55 | + private static final double[] scales = |
| 56 | + {1.00, 1.25, 1.50, 1.75, 2.00, 2.50, 3.00}; |
| 57 | + |
| 58 | + private static final List<BufferedImage> images = |
| 59 | + new ArrayList<>(scales.length); |
| 60 | + |
| 61 | + private static final List<Point> panelLocations = |
| 62 | + new ArrayList<>(4); |
| 63 | + |
| 64 | + public static void main(String[] args) throws Exception { |
| 65 | + boolean showFrame = args.length > 0 && "-show".equals(args[0]); |
| 66 | + SwingUtilities.invokeAndWait(() -> testScaling(showFrame)); |
| 67 | + } |
| 68 | + |
| 69 | + private static void testScaling(boolean show) { |
| 70 | + createGUI(show); |
| 71 | + |
| 72 | + for (int i = 0; i < scales.length; i++) { |
| 73 | + BufferedImage img = images.get(i); |
| 74 | + double scaling = scales[i]; |
| 75 | + System.out.println("Testing scaling: " + scaling); |
| 76 | + |
| 77 | + |
| 78 | + // checking vertical border |
| 79 | + int x = SIZE.width / 2; |
| 80 | + checkVerticalBorder(x, img, scaling); |
| 81 | + |
| 82 | + for (Point p : panelLocations) { |
| 83 | + int y = (int) (p.y * scaling) + SIZE.height / 2; |
| 84 | + checkHorizontalBorder(y, img, scaling); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static void checkHorizontalBorder(int y, BufferedImage img, double scaling) { |
| 90 | + int thickness = 0; |
| 91 | + boolean checkShadow = false; |
| 92 | + boolean checkHighlight = false; |
| 93 | + for (int x = 0; x < img.getWidth(); x++) { |
| 94 | + int color = img.getRGB(x, y); |
| 95 | + if (!checkHighlight && !checkShadow) { |
| 96 | + if (color == shadow.getRGB()) { |
| 97 | + checkHighlight = true; |
| 98 | + thickness++; |
| 99 | + } else if (color == highlight.getRGB()) { |
| 100 | + throw new RuntimeException("Horizontal Border was clipped or overdrawn."); |
| 101 | + } |
| 102 | + } else if (checkHighlight) { |
| 103 | + if (color == shadow.getRGB()) { |
| 104 | + thickness++; |
| 105 | + } else if (color == highlight.getRGB()) { |
| 106 | + verifyThickness(x, y, thickness, scaling, "Horizontal"); |
| 107 | + checkHighlight = false; |
| 108 | + checkShadow = true; |
| 109 | + thickness = 1; |
| 110 | + } else { |
| 111 | + throw new RuntimeException("Horizontal Border has empty space between highlight and shadow."); |
| 112 | + } |
| 113 | + } else { |
| 114 | + if (color == shadow.getRGB()) { |
| 115 | + throw new RuntimeException("Border colors reversed."); |
| 116 | + } else if (color == highlight.getRGB()) { |
| 117 | + thickness++; |
| 118 | + } else { |
| 119 | + verifyThickness(x, y, thickness, scaling, "Horizontal"); |
| 120 | + checkShadow = false; |
| 121 | + thickness = 0; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private static void verifyThickness(int x, int y, int thickness, double scaling, String orientation) { |
| 128 | + int expected = (int) Math.floor(scaling); |
| 129 | + if (thickness != expected) { |
| 130 | + throw new RuntimeException("Unexpected " + orientation + " Border thickness at x:" |
| 131 | + + x + " y: " + y + ". Expected: " + expected + " Actual: " + thickness); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static void checkVerticalBorder(int x, BufferedImage img, double scaling) { |
| 136 | + int thickness = 0; |
| 137 | + boolean checkShadow = false; |
| 138 | + boolean checkHighlight = false; |
| 139 | + for (int y = 0; y < img.getHeight(); y++) { |
| 140 | + int color = img.getRGB(x, y); |
| 141 | + if (!checkHighlight && !checkShadow) { |
| 142 | + if (color == shadow.getRGB()) { |
| 143 | + checkHighlight = true; |
| 144 | + thickness++; |
| 145 | + } else if (color == highlight.getRGB()) { |
| 146 | + throw new RuntimeException("Vertical Border was clipped or overdrawn."); |
| 147 | + } |
| 148 | + } else if (checkHighlight) { |
| 149 | + if (color == shadow.getRGB()) { |
| 150 | + thickness++; |
| 151 | + } else if (color == highlight.getRGB()) { |
| 152 | + verifyThickness(x, y, thickness, scaling, "Vertical"); |
| 153 | + checkHighlight = false; |
| 154 | + checkShadow = true; |
| 155 | + thickness = 1; |
| 156 | + } else { |
| 157 | + throw new RuntimeException("Vertical Border has empty space between highlight and shadow."); |
| 158 | + } |
| 159 | + } else { |
| 160 | + if (color == shadow.getRGB()) { |
| 161 | + throw new RuntimeException("Border colors reversed."); |
| 162 | + } else if (color == highlight.getRGB()) { |
| 163 | + thickness++; |
| 164 | + } else { |
| 165 | + verifyThickness(x, y, thickness, scaling, "Vertical"); |
| 166 | + checkShadow = false; |
| 167 | + thickness = 0; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private static void createGUI(boolean show) { |
| 174 | + // Render content panel |
| 175 | + JPanel contentPanel = new JPanel(); |
| 176 | + contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS)); |
| 177 | + |
| 178 | + Dimension childSize = null; |
| 179 | + for (int i = 0; i < 4; i++) { |
| 180 | + JPanel childPanel = new JPanel(new BorderLayout()); |
| 181 | + childPanel.setBorder(BorderFactory.createCompoundBorder( |
| 182 | + BorderFactory.createEmptyBorder(0, i, 4, 4), |
| 183 | + BorderFactory.createEtchedBorder(highlight, shadow))); |
| 184 | + childPanel.add(Box.createRigidArea(SIZE), BorderLayout.CENTER); |
| 185 | + |
| 186 | + contentPanel.add(childPanel); |
| 187 | + if (childSize == null) { |
| 188 | + childSize = childPanel.getPreferredSize(); |
| 189 | + } |
| 190 | + childPanel.setBounds(0, childSize.height * i, childSize.width, childSize.height); |
| 191 | + } |
| 192 | + |
| 193 | + contentPanel.setSize(childSize.width, childSize.height * 4); |
| 194 | + |
| 195 | + for (double scaling : scales) { |
| 196 | + // Create BufferedImage |
| 197 | + BufferedImage buff = new BufferedImage((int) Math.ceil(contentPanel.getWidth() * scaling), |
| 198 | + (int) Math.ceil(contentPanel.getHeight() * scaling), |
| 199 | + BufferedImage.TYPE_INT_ARGB); |
| 200 | + Graphics2D graph = buff.createGraphics(); |
| 201 | + graph.scale(scaling, scaling); |
| 202 | + // Painting panel onto BufferedImage |
| 203 | + contentPanel.paint(graph); |
| 204 | + graph.dispose(); |
| 205 | + // Save each image ? -- Here it's useful for debugging |
| 206 | + saveImage(buff, String.format("test%.2f.png", scaling)); |
| 207 | + images.add(buff); |
| 208 | + } |
| 209 | + // Save coordinates of the panels |
| 210 | + for (Component comp : contentPanel.getComponents()) { |
| 211 | + panelLocations.add(comp.getLocation()); |
| 212 | + } |
| 213 | + |
| 214 | + if (show) { |
| 215 | + JFrame frame = new JFrame("Swing Test"); |
| 216 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 217 | + frame.getContentPane().add(contentPanel, BorderLayout.CENTER); |
| 218 | + frame.pack(); |
| 219 | + frame.setLocationRelativeTo(null); |
| 220 | + frame.setVisible(true); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + private static void saveImage(BufferedImage image, String filename) { |
| 225 | + try { |
| 226 | + ImageIO.write(image, "png", new File(filename)); |
| 227 | + } catch (IOException e) { |
| 228 | + // Don't propagate the exception |
| 229 | + e.printStackTrace(); |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments