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 
  24 import java.awt.AWTException;
  25 import java.awt.Color;
  26 import java.awt.Component;
  27 import java.awt.Frame;
  28 import java.awt.Graphics;
  29 import java.awt.Label;
  30 import java.awt.Point;
  31 import java.awt.Robot;
  32 import java.awt.Toolkit;
  33 
  34 import sun.awt.SunToolkit;
  35 
  36 /**
  37  * @test
  38  * @key headful
  39  * @bug 7157680
  40  * @author Sergey Bylokhov
  41  */
  42 public final class PaintNativeOnUpdate extends Label {
  43 
  44     private boolean fullUpdate = true;
  45 
  46     public static void main(final String[] args) throws AWTException {
  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         sleep();
  56         label.repaint();// first paint
  57         sleep();
  58         label.repaint();// incremental paint
  59         sleep();
  60 
  61         Robot robot = new Robot();
  62         robot.setAutoDelay(50);
  63         Point point = label.getLocationOnScreen();
  64         Color color = robot.getPixelColor(point.x + label.getWidth() / 2,
  65                                           point.y + label.getHeight() / 2);
  66         if (!color.equals(Color.GREEN)) {
  67             System.err.println("Expected color = " + Color.GREEN);
  68             System.err.println("Actual color = " + color);
  69             throw new RuntimeException();
  70         }
  71         frame.dispose();
  72     }
  73 
  74     @Override
  75     public void update(final Graphics g) {
  76         if (fullUpdate) {
  77             //full paint
  78             g.setColor(Color.GREEN);
  79             g.fillRect(0, 0, getWidth(), getHeight());
  80             fullUpdate = false;
  81         } else {
  82             // Do nothing
  83             // incremental paint
  84         }
  85     }
  86 
  87     @Override
  88     public void paint(final Graphics g) {
  89         // Do nothing
  90     }
  91 
  92     private static void sleep() {
  93         try {
  94             ((SunToolkit) (Toolkit.getDefaultToolkit())).realSync();
  95             Thread.sleep(1000);
  96         } catch (InterruptedException ignored) {
  97         }
  98     }
  99 }