6837446: Introduce Window.isOpaque() method
authoranthony
Tue, 14 Jul 2009 14:08:47 +0400
changeset 3237 7c6c2d9361d9
parent 3236 e1c4ad748c0f
child 3238 af45a8ed7859
child 3334 8d5a10c419a4
6837446: Introduce Window.isOpaque() method Reviewed-by: art, alexp
jdk/src/share/classes/com/sun/awt/AWTUtilities.java
jdk/src/share/classes/java/awt/Component.java
jdk/src/share/classes/java/awt/GraphicsDevice.java
jdk/src/share/classes/java/awt/Window.java
jdk/src/share/classes/javax/swing/DefaultDesktopManager.java
jdk/src/share/classes/javax/swing/RepaintManager.java
jdk/src/share/classes/sun/awt/AWTAccessor.java
jdk/src/share/classes/sun/awt/SunToolkit.java
jdk/src/windows/classes/sun/awt/windows/WWindowPeer.java
--- a/jdk/src/share/classes/com/sun/awt/AWTUtilities.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/share/classes/com/sun/awt/AWTUtilities.java	Tue Jul 14 14:08:47 2009 +0400
@@ -374,7 +374,7 @@
                     "The window argument should not be null.");
         }
 
-        return AWTAccessor.getWindowAccessor().isOpaque(window);
+        return window.isOpaque();
     }
 
     /**
--- a/jdk/src/share/classes/java/awt/Component.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/share/classes/java/awt/Component.java	Tue Jul 14 14:08:47 2009 +0400
@@ -2370,12 +2370,10 @@
      * rectangular region. A non-opaque component paints only some of
      * its pixels, allowing the pixels underneath it to "show through".
      * A component that does not fully paint its pixels therefore
-     * provides a degree of transparency.  Only lightweight
-     * components can be transparent.
+     * provides a degree of transparency.
      * <p>
      * Subclasses that guarantee to always completely paint their
-     * contents should override this method and return true.  All
-     * of the "heavyweight" AWT components are opaque.
+     * contents should override this method and return true.
      *
      * @return true if this component is completely opaque
      * @see #isLightweight
--- a/jdk/src/share/classes/java/awt/GraphicsDevice.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/share/classes/java/awt/GraphicsDevice.java	Tue Jul 14 14:08:47 2009 +0400
@@ -281,8 +281,8 @@
             if (w.getOpacity() < 1.0f) {
                 w.setOpacity(1.0f);
             }
-            Color bgColor = w.getBackground();
-            if ((bgColor != null) && (bgColor.getAlpha() < 255)) {
+            if (!w.isOpaque()) {
+                Color bgColor = w.getBackground();
                 bgColor = new Color(bgColor.getRed(), bgColor.getGreen(),
                                     bgColor.getBlue(), 255);
                 w.setBackground(bgColor);
--- a/jdk/src/share/classes/java/awt/Window.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/share/classes/java/awt/Window.java	Tue Jul 14 14:08:47 2009 +0400
@@ -3521,6 +3521,7 @@
      * @return this component's background color
      *
      * @see Window#setBackground
+     * @see Window#isOpaque
      * @see GraphicsDevice.WindowTranslucency
      */
     @Override
@@ -3583,6 +3584,7 @@
      *     PERPIXEL_TRANSLUCENT} translucency is not supported
      *
      * @see Window#getBackground
+     * @see Window#isOpaque
      * @see Window#setOpacity()
      * @see Window#setShape()
      * @see GraphicsDevice.WindowTranslucency
@@ -3623,6 +3625,25 @@
         }
     }
 
+    /**
+     * Indicates if the window is currently opaque.
+     * <p>
+     * The method returns {@code false} if the background color of the window
+     * is not {@code null} and the alpha component of the color is less than
+     * 1.0f. The method returns {@code true} otherwise.
+     *
+     * @return {@code true} if the window is opaque, {@code false} otherwise
+     *
+     * @see Window#getBackground
+     * @see Window#setBackground
+     * @since 1.7
+     */
+    @Override
+    public boolean isOpaque() {
+        Color bg = getBackground();
+        return bg != null ? bg.getAlpha() == 255 : true;
+    }
+
     private void updateWindow() {
         synchronized (getTreeLock()) {
             WindowPeer peer = (WindowPeer)getPeer();
@@ -3639,12 +3660,11 @@
      */
     @Override
     public void paint(Graphics g) {
-        Color bgColor = getBackground();
-        if ((bgColor != null) && (bgColor.getAlpha() < 255)) {
+        if (!isOpaque()) {
             Graphics gg = g.create();
             try {
                 if (gg instanceof Graphics2D) {
-                    gg.setColor(bgColor);
+                    gg.setColor(getBackground());
                     ((Graphics2D)gg).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
                     gg.fillRect(0, 0, getWidth(), getHeight());
                 }
@@ -3749,10 +3769,6 @@
             public void setShape(Window window, Shape shape) {
                 window.setShape(shape);
             }
-            public boolean isOpaque(Window window) {
-                Color bg = window.getBackground();
-                return (bg != null) ? bg.getAlpha() == 255 : true;
-            }
             public void setOpaque(Window window, boolean opaque) {
                 Color bg = window.getBackground();
                 if (bg == null) {
--- a/jdk/src/share/classes/javax/swing/DefaultDesktopManager.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/share/classes/javax/swing/DefaultDesktopManager.java	Tue Jul 14 14:08:47 2009 +0400
@@ -708,7 +708,7 @@
       // update window if it's non-opaque
       Window topLevel = SwingUtilities.getWindowAncestor(f);
       Toolkit tk = Toolkit.getDefaultToolkit();
-      if (!AWTAccessor.getWindowAccessor().isOpaque(topLevel) &&
+      if (!topLevel.isOpaque() &&
           (tk instanceof SunToolkit) &&
           ((SunToolkit)tk).needUpdateWindow())
       {
--- a/jdk/src/share/classes/javax/swing/RepaintManager.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/share/classes/javax/swing/RepaintManager.java	Tue Jul 14 14:08:47 2009 +0400
@@ -732,7 +732,7 @@
                 (Window)dirty :
                 SwingUtilities.getWindowAncestor(dirty);
             if (window != null &&
-                !AWTAccessor.getWindowAccessor().isOpaque(window))
+                !window.isOpaque())
             {
                 windows.add(window);
             }
@@ -996,7 +996,7 @@
 
         // If the window is non-opaque, it's double-buffered at peer's level
         Window w = (c instanceof Window) ? (Window)c : SwingUtilities.getWindowAncestor(c);
-        if (!AWTAccessor.getWindowAccessor().isOpaque(w)) {
+        if (!w.isOpaque()) {
             Toolkit tk = Toolkit.getDefaultToolkit();
             if ((tk instanceof SunToolkit) && (((SunToolkit)tk).needUpdateWindow())) {
                 return null;
@@ -1032,7 +1032,7 @@
 
         // If the window is non-opaque, it's double-buffered at peer's level
         Window w = (c instanceof Window) ? (Window)c : SwingUtilities.getWindowAncestor(c);
-        if (!AWTAccessor.getWindowAccessor().isOpaque(w)) {
+        if (!w.isOpaque()) {
             Toolkit tk = Toolkit.getDefaultToolkit();
             if ((tk instanceof SunToolkit) && (((SunToolkit)tk).needUpdateWindow())) {
                 return null;
--- a/jdk/src/share/classes/sun/awt/AWTAccessor.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/share/classes/sun/awt/AWTAccessor.java	Tue Jul 14 14:08:47 2009 +0400
@@ -137,11 +137,6 @@
          */
         void setShape(Window window, Shape shape);
         /*
-         * Identify whether the given window is opaque (true)
-         *  or translucent (false).
-         */
-        boolean isOpaque(Window window);
-        /*
          * Set the opaque preoperty to the given window.
          */
         void setOpaque(Window window, boolean isOpaque);
--- a/jdk/src/share/classes/sun/awt/SunToolkit.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/share/classes/sun/awt/SunToolkit.java	Tue Jul 14 14:08:47 2009 +0400
@@ -1985,8 +1985,7 @@
      */
     public static boolean isContainingTopLevelOpaque(Component c) {
         Window w = getContainingWindow(c);
-        return w != null && ((Window)w).getBackground() != null &&
-            ((Window)w).getBackground().getAlpha() == 255;
+        return w != null && w.isOpaque();
     }
 
     /**
--- a/jdk/src/windows/classes/sun/awt/windows/WWindowPeer.java	Sun Jul 12 23:20:26 2009 -0700
+++ b/jdk/src/windows/classes/sun/awt/windows/WWindowPeer.java	Tue Jul 14 14:08:47 2009 +0400
@@ -194,8 +194,7 @@
             // default value of a boolean field is 'false', so set isOpaque to
             // true here explicitly
             this.isOpaque = true;
-            Color bgColor = ((Window)target).getBackground();
-            setOpaque((bgColor == null) || (bgColor.getAlpha() == 255));
+            setOpaque(((Window)target).isOpaque());
         }
     }