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  * @bug 7157680
  39  * @author Sergey Bylokhov
  40  */
  41 public final class PaintNativeOnUpdate extends Label {
  42 
  43     private boolean fullUpdate = true;
  44 
  45     public static void main(final String[] args) throws AWTException {
  46         final Frame frame = new Frame();
  47         final Component label = new PaintNativeOnUpdate();
  48         frame.setBackground(Color.RED);
  49         frame.add(label);
  50         frame.setSize(300, 300);
  51         frame.setUndecorated(true);
  52         frame.setLocationRelativeTo(null);
  53         frame.setVisible(true);
  54         sleep();
  55         label.repaint();// first paint
  56         sleep();
  57         label.repaint();// incremental paint
  58         sleep();
  59 
  60         Robot robot = new Robot();
  61         robot.setAutoDelay(50);
  62         Point point = label.getLocationOnScreen();
  63         Color color = robot.getPixelColor(point.x + label.getWidth() / 2,
  64                                           point.y + label.getHeight() / 2);
  65         if (!color.equals(Color.GREEN)) {
  66             System.err.println("Expected color = " + Color.GREEN);
  67             System.err.println("Actual color = " + color);
  68             throw new RuntimeException();
  69         }
  70         frame.dispose();
  71     }
  72 
  73     @Override
  74     public void update(final Graphics g) {
  75         if (fullUpdate) {
  76             //full paint
  77             g.setColor(Color.GREEN);
  78             g.fillRect(0, 0, getWidth(), getHeight());
  79             fullUpdate = false;
  80         } else {
  81             // Do nothing
  82             // incremental paint
  83         }
  84     }
  85 
  86     @Override
  87     public void paint(final Graphics g) {
  88         // Do nothing
  89     }
  90 
  91     private static void sleep() {
  92         try {
  93             ((SunToolkit) (Toolkit.getDefaultToolkit())).realSync();
  94             Thread.sleep(1000);
  95         } catch (InterruptedException ignored) {
  96         }
  97     }
  98 }