File: PaletteTester.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (204 lines) | stat: -rw-r--r-- 6,943 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4366799
 * @key headful
 * @requires (os.family == "windows")
 * @library /java/awt/regtesthelpers
 * @build PassFailJFrame
 * @summary verifies that Windows applications react to palette
 * changes in 8-bit mode correctly.
 * @run main/manual PaletteTester
*/

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.VolatileImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.io.File;

public class PaletteTester {

    static VImageColors demo;

    private static final String INSTRUCTIONS = """
        This test should be run on any Windows platform in 8-bit
        (256 color) display mode only. To check for errors, run a browser
        application (Firefox or Internet Explorer) at the same time
        and switch between this test and the browser (by clicking on the
        title bars).

        The three panels in this test should look roughly the same (there
        may be some dithering differences if you switch display modes
        during the test, but the overall look should be the same.  If
        completely different colors are being used (either for the orange
        background fill, the text, the image, or the rectangles), then the
        test has failed.
        """;

    private static void init() {

        int width = 300, height = 300;

        demo = new VImageColors();
        Frame f = new Frame("PaletteTester");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {}
            public void windowDeiconified(WindowEvent e) { demo.start(); }
            public void windowIconified(WindowEvent e) { demo.stop(); }
        });
        f.add(demo);
        f.setSize(new Dimension(width, height));
        f.setLocationRelativeTo(null);

        PassFailJFrame.addTestWindow(f);
        PassFailJFrame.positionTestWindow(f, PassFailJFrame.Position.HORIZONTAL);
        f.setVisible(true);

        demo.start();

    }//End  init()

    public static void main( String args[] ) throws Exception {

        PassFailJFrame passFailJFrame = PassFailJFrame.builder()
                                        .title("PaletteTester Instructions")
                                        .instructions(INSTRUCTIONS)
                                        .testTimeOut(5)
                                        .rows(15)
                                        .columns(40)
                                        .screenCapture()
                                        .build();

        EventQueue.invokeAndWait(PaletteTester::init);


        try {
            passFailJFrame.awaitAndCheck();
        } finally {
            demo.stop();
        }
    }//main
}

//************ Begin classes defined for the test ****************

class VImageColors extends JPanel implements Runnable {

    VolatileImage vImage;
    Image bImage;
    private static int width = 300, height = 300;
    private Thread thread;
    Color fillColor = new Color(240, 188, 136);
    Color textColor = new Color(40, 18, 97);
    Color rectColor = new Color(0, 150, 0);
    File f = new File(System.getProperty("test.src", "."), "duke.gif");
    Image duke = new ImageIcon(f.toString()).getImage();

    public void initOffscreen() {
        vImage = this.createVolatileImage(getWidth()/3, getHeight());
        bImage = this.createImage(getWidth()/3, getHeight());
    }

    public void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        if (vImage == null) {
            initOffscreen();
        }

        // Render the left panel via VolatileImage
        do {
            if (
                vImage.validate(getGraphicsConfiguration()) ==
                VolatileImage.IMAGE_INCOMPATIBLE)
            {
                vImage = createVolatileImage(width/3, height);
            }
            Graphics vg = vImage.createGraphics();
            vg.setColor(fillColor);
            vg.fillRect(0, 0, width/3, height);
            vg.drawImage(duke, 0, 0, null);
            vg.setColor(textColor);
            vg.drawString("Vol Image", 5, height-1);
            vg.setColor(rectColor);
            vg.drawRect(0, 0, width/3-1, height-1);
            vg.dispose();
            g.drawImage(vImage, 0, 0, width/3, height, null);
        } while (vImage.contentsLost());

        // Render the middle panel via BufferedImage
        Graphics bg = bImage.getGraphics();
        bg.setColor(fillColor);
        bg.fillRect(0, 0, width/3, height);
        bg.drawImage(duke, 0, 0, null);
        bg.setColor(textColor);
        bg.drawString("Buff Image", 5, height-1);
        bg.setColor(rectColor);
        bg.drawRect(0, 0, width/3-1, height-1);
        bg.dispose();
        g.drawImage(bImage, width/3, 0, width/3, height, null);

        // Render the right panel directly to the screen
        g.setColor(fillColor);
        g.fillRect(2*(width/3), 0, width/3, height);
        g.drawImage(duke, 2*(width/3), 0, null);
        g.setColor(textColor);
        g.drawString("Screen", 2*(width/3) + 5, height-1);
        g.setColor(rectColor);
        g.drawRect(2*(width/3), 0, width/3-1, height-1);

    }

    public void start() {
        thread = new Thread(this);
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.start();
    }

    public synchronized void stop() {
        thread = null;
    }

    public void run() {
        Thread me = Thread.currentThread();
        while (thread == me) {
            try {
                thread.sleep(100);
            } catch (InterruptedException e) { break; }
        }
        thread = null;
    }
}

//************** End classes defined for the test *******************