File: TranslateTest.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 (170 lines) | stat: -rw-r--r-- 6,355 bytes parent folder | download | duplicates (13)
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
/*
 * Copyright (c) 1999, 2023, 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 4207383
 * @summary This tests, in a round about manner, that SwingGraphics does
 *          not wrongly translate the original graphics when disposed. While
 *          this test seems rather ugly, it was possible to get this to happen
 *          in real world apps. This test is really only valid for 1.1.x.
 * @key headful
 * @run main TranslateTest
 */

import java.io.File;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ComponentUI;
import javax.imageio.ImageIO;

public class TranslateTest {
    static JFrame frame;
    static volatile Point pt;
    static volatile Dimension dim;
    static final int WIDTH = 200;
    static final int HEIGHT = 200;

    public static void main(String[] args) throws Exception {
        Robot robot = new Robot();

        try {
            SwingUtilities.invokeAndWait(() -> {
                frame = new JFrame("TranslateTest");

                // paintComponent() triggers create swing graphics which will
                // be invoked on child.
                MyPanel panel = new MyPanel();
                panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                panel.test();
                frame.setVisible(true);
            });
            robot.waitForIdle();
            robot.delay(1000);
            SwingUtilities.invokeAndWait(() -> {
                pt = frame.getLocationOnScreen();
                dim = frame.getSize();
            });
            BufferedImage img = robot.createScreenCapture(
                                    new Rectangle(pt.x + dim.width / 2,
                                                  pt.y + dim.height / 2,
                                                  WIDTH / 2, HEIGHT / 2));
            robot.waitForIdle();
            robot.delay(500);
            Color c = new Color(img.getRGB(img.getWidth() / 2, img.getHeight() / 2));
            if (c.getRed() < 250) {
                ImageIO.write(img, "png", new File("image.png"));
                System.out.println("Color " + c);
                throw new RuntimeException("Translated Color is not red");
            }
        } finally {
            SwingUtilities.invokeAndWait(() -> {
                if (frame != null) {
                    frame.dispose();
                }
            });
        }
    }


    static class MyPanel extends JPanel {
        int            state;
        Graphics       realG;
        Image          image;

        public void test() {
            image = createImage(TranslateTest.WIDTH, TranslateTest.HEIGHT);
            Graphics g = image.getGraphics();
            g.setClip(0, 0, TranslateTest.WIDTH, TranslateTest.HEIGHT);
            realG = g;
            state = 1;
            paintComponent(g);
            state = 3;
            paintComponent(g);
            state = 4;
        }


        public void paint(Graphics g) {
            if (state == 0) {
                test();
            }
            super.paint(g);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
        }

        public void updateUI() {
            setUI(new ComponentUI() {
                public void paint(Graphics g, JComponent c) {
                    if (state == 1) {
                        // g is the first SwingGraphics, when it is disposed
                        // translateX/translateY will be wrong
                        //System.out.println("FIRST:" + g);
                        g.translate(100, 100);
                        state = 2;
                        paintComponent(realG);
                    }
                    else if (state == 2) {
                        // g is the first SwingGraphics, when it is disposed
                        // translateX/translateY will be wrong
                        g.translate(100, 100);
                        //System.out.println("Second:" + g);
                    }
                    else if (state == 3) {
                        // g should be the same as the first, with the wrong
                        // translate.
                        // otherG should be the second graphics, again with
                        // the wrong translation, disposing the second will
                        // cause g to be translated to -100, -100, which
                        // should not happen.
                        Graphics otherG = g.create(0, 0, 100, 100);
                        //System.out.println("THIRD:" + g);
                        otherG.dispose();
                        g.setColor(Color.red);
                        //System.out.println("LAST: " + g);
                        g.fillRect(100, 100, 100, 100);
                    }
                    else if (state == 4) {
                        g.drawImage(image, 0, 0, null);
                    }
                }
            });
        }
    }
}