PaintNativeOnUpdate.java revision 11127:418d2e751094
1/*
2 * Copyright (c) 2014, 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
24import java.awt.AWTException;
25import java.awt.Color;
26import java.awt.Component;
27import java.awt.Frame;
28import java.awt.Graphics;
29import java.awt.Label;
30import java.awt.Point;
31
32/**
33 * @test
34 * @bug 7157680
35 * @library ../../../lib/testlibrary
36 * @build ExtendedRobot
37 * @author Sergey Bylokhov
38 @ @run main PaintNativeOnUpdate
39 */
40public final class PaintNativeOnUpdate extends Label {
41
42    private boolean fullUpdate = true;
43
44    public static void main(final String[] args) throws AWTException {
45        ExtendedRobot robot = new ExtendedRobot();
46        robot.setAutoDelay(50);
47        final Frame frame = new Frame();
48        final Component label = new PaintNativeOnUpdate();
49        frame.setBackground(Color.RED);
50        frame.add(label);
51        frame.setSize(300, 300);
52        frame.setUndecorated(true);
53        frame.setLocationRelativeTo(null);
54        frame.setVisible(true);
55        robot.waitForIdle(1000);
56        label.repaint();// first paint
57        robot.waitForIdle(1000);
58        label.repaint();// incremental paint
59        robot.waitForIdle(1000);
60
61        Point point = label.getLocationOnScreen();
62        Color color = robot.getPixelColor(point.x + label.getWidth() / 2,
63                                          point.y + label.getHeight() / 2);
64        if (!color.equals(Color.GREEN)) {
65            System.err.println("Expected color = " + Color.GREEN);
66            System.err.println("Actual color = " + color);
67            throw new RuntimeException();
68        }
69        frame.dispose();
70    }
71
72    @Override
73    public void update(final Graphics g) {
74        if (fullUpdate) {
75            //full paint
76            g.setColor(Color.GREEN);
77            g.fillRect(0, 0, getWidth(), getHeight());
78            fullUpdate = false;
79        } else {
80            // Do nothing
81            // incremental paint
82        }
83    }
84
85    @Override
86    public void paint(final Graphics g) {
87        // Do nothing
88    }
89}
90