|
1 | 1 | /* |
2 | | - * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2002, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
46 | 46 | public class FrameVisualTest { |
47 | 47 | private static GraphicsConfiguration[] gcs; |
48 | 48 | private static volatile Frame[] frames; |
49 | | - private static volatile int index; |
50 | 49 |
|
51 | | - private static Frame f; |
52 | 50 | private static Robot robot; |
| 51 | + private static volatile int frameNum; |
53 | 52 | private static volatile Point p; |
54 | 53 | private static volatile Dimension d; |
55 | 54 | private static final int TOLERANCE = 5; |
| 55 | + private static final int MAX_FRAME_COUNT = 30; |
56 | 56 |
|
57 | 57 | public static void main(String[] args) throws Exception { |
58 | | - gcs = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getConfigurations(); |
| 58 | + gcs = GraphicsEnvironment.getLocalGraphicsEnvironment() |
| 59 | + .getDefaultScreenDevice().getConfigurations(); |
59 | 60 | robot = new Robot(); |
60 | 61 | robot.setAutoDelay(100); |
61 | | - try { |
62 | | - EventQueue.invokeAndWait(() -> { |
63 | | - createAndShowUI(); |
64 | | - }); |
65 | | - robot.delay(1000); |
66 | | - System.out.println("frames.length: "+frames.length); |
67 | | - for (index = 0; index < frames.length; index++) { |
| 62 | + |
| 63 | + // Limit the number of frames tested if needed |
| 64 | + if (gcs.length > MAX_FRAME_COUNT) { |
| 65 | + frames = new Frame[MAX_FRAME_COUNT]; |
| 66 | + } else { |
| 67 | + frames = new Frame[gcs.length]; |
| 68 | + } |
| 69 | + System.out.println(gcs.length + " gcs found. Testing " |
| 70 | + + frames.length + " frame(s)."); |
| 71 | + |
| 72 | + for (frameNum = 0; frameNum < frames.length; frameNum++) { |
| 73 | + try { |
| 74 | + EventQueue.invokeAndWait(() -> { |
| 75 | + frames[frameNum] = new Frame("Frame w/ gc " |
| 76 | + + frameNum, gcs[frameNum]); |
| 77 | + frames[frameNum].setSize(100, 100); |
| 78 | + frames[frameNum].setUndecorated(true); |
| 79 | + frames[frameNum].setBackground(Color.WHITE); |
| 80 | + frames[frameNum].setVisible(true); |
| 81 | + System.out.println("Frame " + frameNum + " created"); |
| 82 | + }); |
| 83 | + |
| 84 | + robot.delay(1000); |
| 85 | + |
68 | 86 | EventQueue.invokeAndWait(() -> { |
69 | | - p = frames[index].getLocation(); |
70 | | - d = frames[index].getSize(); |
| 87 | + p = frames[frameNum].getLocation(); |
| 88 | + d = frames[frameNum].getSize(); |
71 | 89 | }); |
| 90 | + |
72 | 91 | Rectangle rect = new Rectangle(p, d); |
73 | 92 | BufferedImage img = robot.createScreenCapture(rect); |
74 | 93 | if (chkImgBackgroundColor(img)) { |
75 | 94 | try { |
76 | | - ImageIO.write(img, "png", new File("Frame_" + index + ".png")); |
| 95 | + ImageIO.write(img, "png", |
| 96 | + new File("Frame_" |
| 97 | + + frameNum + ".png")); |
77 | 98 | } catch (IOException ignored) {} |
78 | | - throw new RuntimeException("Frame visual test failed with non-white background color"); |
| 99 | + throw new RuntimeException("Frame visual test " + |
| 100 | + "failed with non-white background color"); |
79 | 101 | } |
80 | | - } |
81 | | - } finally { |
82 | | - for (index = 0; index < frames.length; index++) { |
| 102 | + } finally { |
83 | 103 | EventQueue.invokeAndWait(() -> { |
84 | | - if (frames[index] != null) { |
85 | | - frames[index].dispose(); |
| 104 | + if (frames[frameNum] != null) { |
| 105 | + frames[frameNum].dispose(); |
| 106 | + System.out.println("Frame " + frameNum + " disposed"); |
86 | 107 | } |
87 | 108 | }); |
88 | 109 | } |
89 | 110 | } |
90 | 111 | } |
91 | 112 |
|
92 | | - private static void createAndShowUI() { |
93 | | - frames = new Frame[gcs.length]; |
94 | | - for (int i = 0; i < frames.length; i++) { |
95 | | - frames[i] = new Frame("Frame w/ gc " + i, gcs[i]); |
96 | | - frames[i].setSize(100, 100); |
97 | | - frames[i].setUndecorated(true); |
98 | | - frames[i].setBackground(Color.WHITE); |
99 | | - frames[i].setVisible(true); |
100 | | - } |
101 | | - } |
102 | | - |
103 | 113 | private static boolean chkImgBackgroundColor(BufferedImage img) { |
104 | | - |
105 | 114 | // scan for mid-line and if it is non-white color then return true. |
106 | 115 | for (int x = 1; x < img.getWidth() - 1; ++x) { |
107 | 116 | Color c = new Color(img.getRGB(x, img.getHeight() / 2)); |
|
0 commit comments