# HG changeset patch # User anthony # Date 1240299352 -14400 # Node ID aa45a227fce379d590797f13caf70c3a5f9f1578 # Parent ea80a312972e6845d97aaa903fb34e62bf56ceb1 6802853: API: shaped & translucent windows Summary: A public API for the feature forward-ported from 6u10. Reviewed-by: yan diff -r ea80a312972e -r aa45a227fce3 jdk/src/share/classes/java/awt/GraphicsConfiguration.java --- a/jdk/src/share/classes/java/awt/GraphicsConfiguration.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/share/classes/java/awt/GraphicsConfiguration.java Tue Apr 21 11:35:52 2009 +0400 @@ -440,13 +440,14 @@ * the {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT * PERPIXEL_TRANSLUCENT} kind of translucency. * - * @param gc GraphicsConfiguration - * @throws NullPointerException if the gc argument is null * @return whether the given GraphicsConfiguration supports * the translucency effects. + * * @see Window#setBackground(Color) + * + * @since 1.7 */ - /*public */boolean isTranslucencyCapable() { + public boolean isTranslucencyCapable() { // Overridden in subclasses return false; } diff -r ea80a312972e -r aa45a227fce3 jdk/src/share/classes/java/awt/GraphicsDevice.java --- a/jdk/src/share/classes/java/awt/GraphicsDevice.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/share/classes/java/awt/GraphicsDevice.java Tue Apr 21 11:35:52 2009 +0400 @@ -112,10 +112,14 @@ */ public final static int TYPE_IMAGE_BUFFER = 2; - /** Kinds of translucency supported by the underlying system. - * @see #isTranslucencySupported + /** + * Kinds of translucency supported by the underlying system. + * + * @see #isWindowTranslucencySupported + * + * @since 1.7 */ - /*public */static enum WindowTranslucency { + public static enum WindowTranslucency { /** * Represents support in the underlying system for windows each pixel * of which is guaranteed to be either completely opaque, with @@ -246,38 +250,44 @@ * full-screen window is not visible, this method will make it visible. * It will remain visible when returning to windowed mode. *

- * When returning to windowed mode from an exclusive full-screen window, any - * display changes made by calling setDisplayMode are + * When entering full-screen mode, all the translucency effects are reset for + * the window. Its shape is set to {@code null}, the opacity value is set to + * 1.0f, and the background color alpha is set to 255 (completely opaque). + * These values are not restored when returning to windowed mode. + *

+ * When returning to windowed mode from an exclusive full-screen window, + * any display changes made by calling {@code setDisplayMode} are * automatically restored to their original state. * - * @param w a window to use as the full-screen window; null + * @param w a window to use as the full-screen window; {@code null} * if returning to windowed mode. Some platforms expect the * fullscreen window to be a top-level component (i.e., a Frame); * therefore it is preferable to use a Frame here rather than a * Window. + * * @see #isFullScreenSupported * @see #getFullScreenWindow * @see #setDisplayMode * @see Component#enableInputMethods * @see Component#setVisible + * * @since 1.4 */ public void setFullScreenWindow(Window w) { if (w != null) { - //XXX: The actions should be documented in some non-update release. - /* if (w.getShape() != null) { - w.setShape(w, null); - } - if (!w.isOpaque()) { - w.setOpaque(false); + w.setShape(null); } if (w.getOpacity() < 1.0f) { w.setOpacity(1.0f); } - */ + Color bgColor = w.getBackground(); + if (bgColor.getAlpha() < 255) { + bgColor = new Color(bgColor.getRed(), bgColor.getGreen(), + bgColor.getBlue(), 255); + w.setBackground(bgColor); + } } - if (fullScreenWindow != null && windowedModeBounds != null) { // if the window went into fs mode before it was realized it may // have (0,0) dimensions @@ -469,13 +479,15 @@ } /** - * Returns whether the given level of translucency is supported + * Returns whether the given level of translucency is supported by * this graphics device. * * @param translucencyKind a kind of translucency support * @return whether the given translucency kind is supported + * + * @since 1.7 */ - /*public */boolean isWindowTranslucencySupported(WindowTranslucency translucencyKind) { + public boolean isWindowTranslucencySupported(WindowTranslucency translucencyKind) { switch (translucencyKind) { case PERPIXEL_TRANSPARENT: return isWindowShapingSupported(); diff -r ea80a312972e -r aa45a227fce3 jdk/src/share/classes/java/awt/Window.java --- a/jdk/src/share/classes/java/awt/Window.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/share/classes/java/awt/Window.java Tue Apr 21 11:35:52 2009 +0400 @@ -25,6 +25,7 @@ package java.awt; import java.awt.event.*; +import java.awt.geom.Path2D; import java.awt.geom.Point2D; import java.awt.im.InputContext; import java.awt.image.BufferStrategy; @@ -297,6 +298,7 @@ /* * Opacity level of the window * + * @serial * @see #setOpacity(float) * @see #getOpacity() * @since 1.7 @@ -307,6 +309,7 @@ * The shape assigned to this window. This field is set to null if * no shape is set (rectangular window). * + * @serial * @see #getShape() * @see #setShape(Shape) * @since 1.7 @@ -3340,32 +3343,78 @@ // ******************** SHAPES & TRANSPARENCY CODE ******************** /** - * JavaDoc + * Returns the opacity of the window. + * + * @return the opacity of the window + * + * @see Window#setOpacity + * @see GraphicsDevice.WindowTranslucency + * + * @since 1.7 */ - /*public */float getOpacity() { + public float getOpacity() { synchronized (getTreeLock()) { return opacity; } } /** - * JavaDoc + * Sets the opacity of the window. + *

+ * The opacity value is in the range [0..1]. Note that setting the opacity + * level of 0 may or may not disable the mouse event handling on this + * window. This is a platform-dependent behavior. + *

+ * In order for this method to enable the translucency effect, the {@link + * GraphicsDevice#isWindowTranslucencySupported()} method must indicate that + * the {@link GraphicsDevice.WindowTranslucency#TRANSLUCENT TRANSLUCENT} + * translucency is supported. + *

+ * Also note that the window must not be in the full-screen mode when + * setting the opacity value < 1.0f. Otherwise the {@code + * IllegalComponentStateException} is thrown. + *

+ * The translucency levels of individual pixels may also be effected by the + * alpha component of their color (see {@link setBackground()}) and the + * current shape of this window (see {@link setShape()}). + * + * @param opacity the opacity level to set to the window + * + * @throws IllegalArgumentException if the opacity is out of the range + * [0..1] + * @throws IllegalComponentStateException if the window is in full screen + * mode, and the opacity is less than 1.0f + * @throws UnsupportedOperationException if the {@code + * GraphicsDevice.WindowTranslucency#TRANSLUCENT TRANSLUCENT} + * translucency kind is not supported and the opacity is less than 1.0f + * + * @see Window#getOpacity + * @see Window#setBackground() + * @see Window#setShape() + * @see GraphicsDevice.WindowTranslucency + * @see GraphicsDevice#isWindowTranslucencySupported() + * + * @since 1.7 */ - /*public */void setOpacity(float opacity) { + public void setOpacity(float opacity) { synchronized (getTreeLock()) { if (opacity < 0.0f || opacity > 1.0f) { throw new IllegalArgumentException( "The value of opacity should be in the range [0.0f .. 1.0f]."); } - GraphicsConfiguration gc = getGraphicsConfiguration(); - GraphicsDevice gd = gc.getDevice(); - if (!gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT)) { - throw new UnsupportedOperationException( + if (opacity < 1.0f) { + GraphicsConfiguration gc = getGraphicsConfiguration(); + GraphicsDevice gd = gc.getDevice(); + if (gc.getDevice().getFullScreenWindow() == this) { + throw new IllegalComponentStateException( + "Setting opacity for full-screen window is not supported."); + } + if (!gd.isWindowTranslucencySupported( + GraphicsDevice.WindowTranslucency.TRANSLUCENT)) + { + throw new UnsupportedOperationException( "TRANSLUCENT translucency is not supported."); - } - if ((gc.getDevice().getFullScreenWindow() == this) && (opacity < 1.0f)) { - throw new IllegalArgumentException( - "Setting opacity for full-screen window is not supported."); + } } this.opacity = opacity; WindowPeer peer = (WindowPeer)getPeer(); @@ -3376,37 +3425,86 @@ } /** - * JavaDoc + * Returns the shape of the window. + * + * The value returned by this method may not be the same as + * previously set with {@code setShape(shape)}, but it is guaranteed + * to represent the same shape. + * + * @return the shape of the window or {@code null} if no + * shape is specified for the window + * + * @see Window#setShape + * @see GraphicsDevice.WindowTranslucency + * + * @since 1.7 */ - /*public */Shape getShape() { + public Shape getShape() { synchronized (getTreeLock()) { - return shape; + return shape == null ? null : new Path2D.Float(shape); } } /** - * JavaDoc + * Sets the shape of the window. + *

+ * Setting a shape enables cutting off some parts of the window, leaving + * visible and clickable only those parts belonging to the given shape + * (see {@link Shape}). If the shape argument is null, this methods + * restores the default shape (making the window rectangular on most + * platforms.) + *

+ * The following conditions must be met in order to set a non-null shape: + *

+ * If a certain condition is not met, either the {@code + * UnsupportedOperationException} or {@code IllegalComponentStateException} + * is thrown. + *

+ * The tranlucency levels of individual pixels may also be effected by the + * alpha component of their color (see {@link setBackground()}) and the + * opacity value (see {@link setOpacity()}). See {@link + * GraphicsDevice#WindowTranslucency} for more details. * - * @param window the window to set the shape to * @param shape the shape to set to the window - * @throws IllegalArgumentException if the window is in full screen mode, - * and the shape is not null + * + * @throws IllegalComponentStateException if the shape is not {@code + * null} and the window is in full-screen mode + * @throws UnsupportedOperationException if the shape is not {@code + * null} and {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSPARENT + * PERPIXEL_TRANSPARENT} translucency is not supported + * + * @see Window#getShape() + * @see Window#setBackgound() + * @see Window#setOpacity() + * @see GraphicsDevice.WindowTranslucency + * @see GraphicsDevice#isWindowTranslucencySupported() + * + * @since 1.7 */ - /*public */void setShape(Shape shape) { + public void setShape(Shape shape) { synchronized (getTreeLock()) { - GraphicsConfiguration gc = getGraphicsConfiguration(); - GraphicsDevice gd = gc.getDevice(); - if (!gd.isWindowTranslucencySupported( - GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) - { - throw new UnsupportedOperationException( + if (shape != null) { + GraphicsConfiguration gc = getGraphicsConfiguration(); + GraphicsDevice gd = gc.getDevice(); + if (gc.getDevice().getFullScreenWindow() == this) { + throw new IllegalComponentStateException( + "Setting shape for full-screen window is not supported."); + } + if (!gd.isWindowTranslucencySupported( + GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) + { + throw new UnsupportedOperationException( "PERPIXEL_TRANSPARENT translucency is not supported."); + } } - if ((gc.getDevice().getFullScreenWindow() == this) && (shape != null)) { - throw new IllegalArgumentException( - "Setting shape for full-screen window is not supported."); - } - this.shape = shape; + this.shape = (shape == null) ? null : new Path2D.Float(shape); WindowPeer peer = (WindowPeer)getPeer(); if (peer != null) { peer.applyShape(shape == null ? null : Region.getInstance(shape, null)); @@ -3415,66 +3513,115 @@ } /** - * JavaDoc + * Gets the background color of this window. + *

+ * Note that the alpha component of the returned color indicates whether + * the window is in the non-opaque (per-pixel translucent) mode. + * + * @return this component's background color + * + * @see Window#setBackground + * @see GraphicsDevice.WindowTranslucency */ -/* + @Override + public Color getBackground() { + return super.getBackground(); + } + + /** + * Sets the background color of this window. + *

+ * If the windowing system supports the {@link + * GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT PERPIXEL_TRANSLUCENT} + * tranclucency, the alpha component of the given background color + * may effect the mode of operation for this window: it indicates whether + * this window must be opaque (alpha == 1.0f) or per-pixel translucent + * (alpha < 1.0f). All the following conditions must be met in order + * to be able to enable the per-pixel transparency mode for this window: + *

+ * If a certain condition is not met at the time of calling this method, + * the alpha component of the given background color will not effect the + * mode of operation for this window. + *

+ * When the window is per-pixel translucent, the drawing sub-system + * respects the alpha value of each individual pixel. If a pixel gets + * painted with the alpha color component equal to zero, it becomes + * visually transparent, if the alpha of the pixel is equal to 1.0f, the + * pixel is fully opaque. Interim values of the alpha color component make + * the pixel semi-transparent. In this mode the background of the window + * gets painted with the alpha value of the given background color (meaning + * that it is not painted at all if the alpha value of the argument of this + * method is equal to zero.) + *

+ * The actual level of translucency of a given pixel also depends on window + * opacity (see {@link setOpacity()}), as well as the current shape of + * this window (see {@link setShape()}). + *

+ * Note that painting a pixel with the alpha value of 0 may or may not + * disable the mouse event handling on this pixel. This is a + * platform-dependent behavior. To make sure the mouse clicks do not get + * dispatched to a particular pixel, the pixel must be excluded from the + * shape of the window. + *

+ * Enabling the per-pixel translucency mode may change the graphics + * configuration of this window due to the native platform requirements. + * + * @param bgColor the color to become this window's background color. + * + * @throws IllegalComponentStateException if the alpha value of the given + * background color is less than 1.0f and the window is in + * full-screen mode + * @throws UnsupportedOperationException if the alpha value of the given + * background color is less than 1.0f and + * {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT + * PERPIXEL_TRANSLUCENT} translucency is not supported + * + * @see Window#getBackground + * @see Window#setOpacity() + * @see Window#setShape() + * @see GraphicsDevice.WindowTranslucency + * @see GraphicsDevice#isWindowTranslucencySupported() + * @see GraphicsConfiguration#isTranslucencyCapable() + */ @Override public void setBackground(Color bgColor) { + Color oldBg = getBackground(); + if (oldBg != null && oldBg.equals(bgColor)) { + return; + } + super.setBackground(bgColor); + int oldAlpha = oldBg != null ? oldBg.getAlpha() : 255; int alpha = bgColor.getAlpha(); - if (alpha < 255) { // non-opaque window + if ((oldAlpha == 255) && (alpha < 255)) { // non-opaque window GraphicsConfiguration gc = getGraphicsConfiguration(); GraphicsDevice gd = gc.getDevice(); if (gc.getDevice().getFullScreenWindow() == this) { - throw new IllegalArgumentException( + throw new IllegalComponentStateException( "Making full-screen window non opaque is not supported."); } if (!gc.isTranslucencyCapable()) { GraphicsConfiguration capableGC = gd.getTranslucencyCapableGC(); if (capableGC == null) { - throw new IllegalArgumentException( + throw new UnsupportedOperationException( "PERPIXEL_TRANSLUCENT translucency is not supported"); } - // TODO: change GC + setGraphicsConfiguration(capableGC); } setLayersOpaque(this, false); + } else if ((oldAlpha < 255) && (alpha == 255)) { + setLayersOpaque(this, true); } - - super.setBackground(bgColor); - WindowPeer peer = (WindowPeer)getPeer(); if (peer != null) { peer.setOpaque(alpha == 255); } } -*/ - - private transient boolean opaque = true; - - void setOpaque(boolean opaque) { - synchronized (getTreeLock()) { - GraphicsConfiguration gc = getGraphicsConfiguration(); - if (!opaque && !com.sun.awt.AWTUtilities.isTranslucencyCapable(gc)) { - throw new IllegalArgumentException( - "The window must use a translucency-compatible graphics configuration"); - } - if (!com.sun.awt.AWTUtilities.isTranslucencySupported( - com.sun.awt.AWTUtilities.Translucency.PERPIXEL_TRANSLUCENT)) - { - throw new UnsupportedOperationException( - "PERPIXEL_TRANSLUCENT translucency is not supported."); - } - if ((gc.getDevice().getFullScreenWindow() == this) && !opaque) { - throw new IllegalArgumentException( - "Making full-screen window non opaque is not supported."); - } - setLayersOpaque(this, opaque); - this.opaque = opaque; - WindowPeer peer = (WindowPeer)getPeer(); - if (peer != null) { - peer.setOpaque(opaque); - } - } - } private void updateWindow(BufferedImage backBuffer) { synchronized (getTreeLock()) { @@ -3505,10 +3652,10 @@ } lp.setOpaque(isOpaque); root.setOpaque(isOpaque); - root.setDoubleBuffered(isOpaque); //XXX: the "white rect" workaround + root.setDoubleBuffered(isOpaque); if (content != null) { content.setOpaque(isOpaque); - content.setDoubleBuffered(isOpaque); //XXX: the "white rect" workaround + content.setDoubleBuffered(isOpaque); // Iterate down one level to see whether we have a JApplet // (which is also a RootPaneContainer) which requires processing @@ -3523,36 +3670,6 @@ } } } - - Color bg = component.getBackground(); - boolean hasTransparentBg = TRANSPARENT_BACKGROUND_COLOR.equals(bg); - - Container container = null; - if (component instanceof Container) { - container = (Container) component; - } - - if (isOpaque) { - if (hasTransparentBg) { - // Note: we use the SystemColor.window color as the default. - // This color is used in the WindowPeer implementations to - // initialize the background color of the window if it is null. - // (This might not be the right thing to do for other - // RootPaneContainers we might be invoked with) - Color newColor = null; - if (container != null && container.preserveBackgroundColor != null) { - newColor = container.preserveBackgroundColor; - } else { - newColor = SystemColor.window; - } - component.setBackground(newColor); - } - } else { - if (!hasTransparentBg && container != null) { - container.preserveBackgroundColor = bg; - } - component.setBackground(TRANSPARENT_BACKGROUND_COLOR); - } } @@ -3620,20 +3737,16 @@ window.setShape(shape); } public boolean isOpaque(Window window) { - /* - return window.getBackground().getAlpha() < 255; - */ - synchronized (window.getTreeLock()) { - return window.opaque; - } + 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) { + bg = new Color(0, 0, 0, 0); + } window.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue(), opaque ? 255 : 0)); - */ - window.setOpaque(opaque); } public void updateWindow(Window window, BufferedImage backBuffer) { window.updateWindow(backBuffer); diff -r ea80a312972e -r aa45a227fce3 jdk/src/share/classes/sun/awt/EmbeddedFrame.java --- a/jdk/src/share/classes/sun/awt/EmbeddedFrame.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/share/classes/sun/awt/EmbeddedFrame.java Tue Apr 21 11:35:52 2009 +0400 @@ -588,9 +588,11 @@ public void setOpacity(float opacity) { } + public void setOpaque(boolean isOpaque) { } - public void updateWindow(BufferedImage backBuffer) { + + public void updateWindow(BufferedImage bi) { } public void repositionSecurityWarning() { } diff -r ea80a312972e -r aa45a227fce3 jdk/src/share/classes/sun/awt/SunToolkit.java --- a/jdk/src/share/classes/sun/awt/SunToolkit.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/share/classes/sun/awt/SunToolkit.java Tue Apr 21 11:35:52 2009 +0400 @@ -2038,37 +2038,34 @@ /** * Returns whether or not a containing top level window for the passed * component is - * {@link com.sun.awt.AWTUtilities.Translucency#PERPIXEL_TRANSLUCENT PERPIXEL_TRANSLUCENT}. + * {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT PERPIXEL_TRANSLUCENT}. * * @param c a Component which toplevel's to check * @return {@code true} if the passed component is not null and has a * containing toplevel window which is opaque (so per-pixel translucency * is not enabled), {@code false} otherwise - * @see com.sun.awt.AWTUtilities.Translucency#PERPIXEL_TRANSLUCENT - * @see com.sun.awt.AWTUtilities#isWindowOpaque(Window) + * @see GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT */ public static boolean isContainingTopLevelOpaque(Component c) { Window w = getContainingWindow(c); - // return w != null && (w).isOpaque(); - return w != null && com.sun.awt.AWTUtilities.isWindowOpaque(w); + return w != null && ((Window)w).getBackground() != null && + ((Window)w).getBackground().getAlpha() == 255; } /** * Returns whether or not a containing top level window for the passed * component is - * {@link com.sun.awt.AWTUtilities.Translucency#TRANSLUCENT TRANSLUCENT}. + * {@link GraphicsDevice.WindowTranslucency#TRANSLUCENT TRANSLUCENT}. * * @param c a Component which toplevel's to check * @return {@code true} if the passed component is not null and has a * containing toplevel window which has opacity less than * 1.0f (which means that it is translucent), {@code false} otherwise - * @see com.sun.awt.AWTUtilities.Translucency#TRANSLUCENT - * @see com.sun.awt.AWTUtilities#getWindowOpacity(Window) + * @see GraphicsDevice.WindowTranslucency#TRANSLUCENT */ public static boolean isContainingTopLevelTranslucent(Component c) { Window w = getContainingWindow(c); - // return w != null && (w).getOpacity() < 1.0f; - return w != null && com.sun.awt.AWTUtilities.getWindowOpacity((Window)w) < 1.0f; + return w != null && ((Window)w).getOpacity() < 1.0f; } /** diff -r ea80a312972e -r aa45a227fce3 jdk/src/solaris/classes/sun/awt/X11GraphicsConfig.java --- a/jdk/src/solaris/classes/sun/awt/X11GraphicsConfig.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/solaris/classes/sun/awt/X11GraphicsConfig.java Tue Apr 21 11:35:52 2009 +0400 @@ -451,9 +451,7 @@ } } - /* @Override - */ public boolean isTranslucencyCapable() { return isTranslucencyCapable(getAData()); } diff -r ea80a312972e -r aa45a227fce3 jdk/src/windows/classes/sun/awt/Win32GraphicsConfig.java --- a/jdk/src/windows/classes/sun/awt/Win32GraphicsConfig.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/windows/classes/sun/awt/Win32GraphicsConfig.java Tue Apr 21 11:35:52 2009 +0400 @@ -332,9 +332,7 @@ // the rest of the flip actions are not supported } - /* @Override - */ public boolean isTranslucencyCapable() { //XXX: worth checking if 8-bit? Anyway, it doesn't hurt. return true; diff -r ea80a312972e -r aa45a227fce3 jdk/src/windows/classes/sun/awt/windows/WComponentPeer.java --- a/jdk/src/windows/classes/sun/awt/windows/WComponentPeer.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/windows/classes/sun/awt/windows/WComponentPeer.java Tue Apr 21 11:35:52 2009 +0400 @@ -970,11 +970,12 @@ * * Conditions which could prevent hw acceleration include the toplevel * window containing this component being - * {@link com.sun.awt.AWTUtilities.Translucency#TRANSLUCENT TRANSLUCENT}. + * {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT + * PERPIXEL_TRANSLUCENT}. * * @return {@code true} if this component is capable of being hw * accelerated, {@code false} otherwise - * @see com.sun.awt.AWTUtilities.Translucency#TRANSLUCENT + * @see GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT */ public boolean isAccelCapable() { boolean isTranslucent = diff -r ea80a312972e -r aa45a227fce3 jdk/src/windows/classes/sun/awt/windows/WWindowPeer.java --- a/jdk/src/windows/classes/sun/awt/windows/WWindowPeer.java Mon Apr 20 19:18:41 2009 +0400 +++ b/jdk/src/windows/classes/sun/awt/windows/WWindowPeer.java Tue Apr 21 11:35:52 2009 +0400 @@ -335,16 +335,14 @@ } private void updateShape() { - // Shape shape = ((Window)target).getShape(); - Shape shape = AWTAccessor.getWindowAccessor().getShape((Window)target); + Shape shape = ((Window)target).getShape(); if (shape != null) { applyShape(Region.getInstance(shape, null)); } } private void updateOpacity() { - // float opacity = ((Window)target).getOpacity(); - float opacity = AWTAccessor.getWindowAccessor().getOpacity((Window)target); + float opacity = ((Window)target).getOpacity(); if (opacity < 1.0f) { setOpacity(opacity); } @@ -610,11 +608,13 @@ public void setOpaque(boolean isOpaque) { Window target = (Window)getTarget(); - SunToolkit sunToolkit = (SunToolkit)target.getToolkit(); - if (!sunToolkit.isWindowTranslucencySupported() || - !sunToolkit.isTranslucencyCapable(target.getGraphicsConfiguration())) - { - return; + if (!isOpaque) { + SunToolkit sunToolkit = (SunToolkit)target.getToolkit(); + if (!sunToolkit.isWindowTranslucencySupported() || + !sunToolkit.isTranslucencyCapable(target.getGraphicsConfiguration())) + { + return; + } } boolean opaqueChanged = this.isOpaque != isOpaque; @@ -648,9 +648,9 @@ // its shape only. To restore the correct visual appearance // of the window (i.e. w/ the correct shape) we have to reset // the shape. - Shape shape = AWTAccessor.getWindowAccessor().getShape(target); + Shape shape = ((Window)target).getShape(); if (shape != null) { - AWTAccessor.getWindowAccessor().setShape(target, shape); + ((Window)target).setShape(shape); } } @@ -664,6 +664,11 @@ return; } + Component target = (Component)this.target; + if (target.getWidth() <= 0 || target.getHeight() <= 0) { + return; + } + TranslucentWindowPainter currentPainter = painter; if (currentPainter != null) { currentPainter.updateWindow(backBuffer); diff -r ea80a312972e -r aa45a227fce3 jdk/test/com/sun/awt/Translucency/TranslucentJAppletTest/TranslucentJAppletTest.java --- a/jdk/test/com/sun/awt/Translucency/TranslucentJAppletTest/TranslucentJAppletTest.java Mon Apr 20 19:18:41 2009 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,103 +0,0 @@ -/* - * Copyright 2008-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, - * CA 95054 USA or visit www.sun.com if you need additional information or - * have any questions. - */ - -/* - * @test %I% %E% - * @bug 6683728 - * @summary Tests that a JApplet in a translucent JFrame works properly - * @author Kenneth.Russell@sun.com: area=Graphics - * @compile -XDignore.symbol.file=true TranslucentJAppletTest.java - * @run main/manual/othervm TranslucentJAppletTest - */ - -import java.awt.*; -import java.awt.image.*; - -import javax.swing.*; - -public class TranslucentJAppletTest { - - private static JFrame frame; - private static volatile boolean paintComponentCalled = false; - - private static void initAndShowGUI() { - frame = new JFrame(); - JApplet applet = new JApplet(); - JPanel panel = new JPanel() { - protected void paintComponent(Graphics g) { - paintComponentCalled = true; - g.setColor(Color.RED); - g.fillOval(0, 0, getWidth(), getHeight()); - } - }; - panel.setDoubleBuffered(false); - panel.setOpaque(false); - applet.add(panel); - frame.add(applet); - frame.setBounds(100, 100, 200, 200); - frame.setUndecorated(true); - com.sun.awt.AWTUtilities.setWindowOpaque(frame, false); - frame.setVisible(true); - } - - public static void main(String[] args) - throws Exception - { - sun.awt.SunToolkit tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit(); - - Robot r = new Robot(); - Color color1 = r.getPixelColor(100, 100); // (0, 0) in frame coordinates - - SwingUtilities.invokeAndWait(new Runnable() { - public void run() { - initAndShowGUI(); - } - }); - tk.realSync(); - - if (!paintComponentCalled) { - throw new RuntimeException("Test FAILED: panel's paintComponent() method is not called"); - } - - Color newColor1 = r.getPixelColor(100, 100); - // unfortunately, robot.getPixelColor() doesn't work for some unknown reason - // Color newColor2 = r.getPixelColor(200, 200); - BufferedImage bim = r.createScreenCapture(new Rectangle(200, 200, 1, 1)); - Color newColor2 = new Color(bim.getRGB(0, 0)); - - // Frame must be transparent at (100, 100) in screen coords - if (!color1.equals(newColor1)) { - System.err.println("color1 = " + color1); - System.err.println("newColor1 = " + newColor1); - throw new RuntimeException("Test FAILED: frame pixel at (0, 0) is not transparent"); - } - - // Frame must be RED at (200, 200) in screen coords - if (!newColor2.equals(Color.RED)) { - System.err.println("newColor2 = " + newColor2); - throw new RuntimeException("Test FAILED: frame pixel at (100, 100) is not red (transparent?)"); - } - - System.out.println("Test PASSED"); - } -} diff -r ea80a312972e -r aa45a227fce3 jdk/test/com/sun/awt/Translucency/TranslucentShapedFrameTest/TSFrame.java --- a/jdk/test/com/sun/awt/Translucency/TranslucentShapedFrameTest/TSFrame.java Mon Apr 20 19:18:41 2009 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,306 +0,0 @@ -/* - * Copyright 2008-2009 Sun Microsystems, Inc. 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. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the LICENSE file that accompanied this code. - * - * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, - * CA 95054 USA or visit www.sun.com if you need additional information or - * have any questions. - */ - -import com.sun.awt.AWTUtilities; -import static com.sun.awt.AWTUtilities.Translucency.*; -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Frame; -import java.awt.Graphics; -import java.awt.GraphicsConfiguration; -import java.awt.GraphicsEnvironment; -import java.awt.RenderingHints; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.awt.Canvas; -import java.awt.Component; -import java.awt.GradientPaint; -import java.awt.Graphics2D; -import java.awt.Paint; -import java.util.Random; -import java.awt.geom.Ellipse2D; -import javax.swing.JApplet; -import javax.swing.JButton; -import javax.swing.JComponent; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.SwingUtilities; - -public class TSFrame { - - static volatile boolean done = false; - - static final boolean useSwing = System.getProperty("useswing") != null; - static final boolean useShape = System.getProperty("useshape") != null; - static final boolean useTransl = System.getProperty("usetransl") != null; - static final boolean useNonOpaque = System.getProperty("usenonop") != null; - - static final Random rnd = new Random(); - private static void render(Graphics g, int w, int h, boolean useNonOpaque) { - if (useNonOpaque) { - Graphics2D g2d = (Graphics2D)g; - GradientPaint p = - new GradientPaint(0.0f, 0.0f, - new Color(rnd.nextInt(0xffffff)), - w, h, - new Color(rnd.nextInt(0xff), - rnd.nextInt(0xff), - rnd.nextInt(0xff), 0), - true); - g2d.setPaint(p); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - g2d.fillOval(0, 0, w, h); - } else { - g.setColor(new Color(rnd.nextInt(0xffffff))); - g.fillRect(0, 0, w, h); - } - } - - private static class MyCanvas extends Canvas { - @Override - public void paint(Graphics g) { - render(g, getWidth(), getHeight(), false); - } - @Override - public Dimension getPreferredSize() { - return new Dimension(200, 100); - } - } - private static class NonOpaqueJFrame extends JFrame { - NonOpaqueJFrame(GraphicsConfiguration gc) { - super("NonOpaque Swing JFrame", gc); - JPanel p = new JPanel() { - public void paintComponent(Graphics g) { - super.paintComponent(g); - render(g, getWidth(), getHeight(), true); - g.setColor(Color.red); - g.drawString("Non-Opaque Swing JFrame", 10, 15); - } - }; - p.setDoubleBuffered(false); - p.setOpaque(false); - add(p); - setUndecorated(true); - } - } - private static class NonOpaqueJAppletFrame extends JFrame { - JPanel p; - NonOpaqueJAppletFrame(GraphicsConfiguration gc) { - super("NonOpaque Swing JAppletFrame", gc); - JApplet ja = new JApplet() { - public void paint(Graphics g) { - super.paint(g); - System.err.println("JAppletFrame paint called"); - } - }; - p = new JPanel() { - public void paintComponent(Graphics g) { - super.paintComponent(g); - render(g, getWidth(), getHeight(), true); - g.setColor(Color.red); - g.drawString("Non-Opaque Swing JFrame", 10, 15); - } - }; - p.setDoubleBuffered(false); - p.setOpaque(false); - ja.add(p); - add(ja); - setUndecorated(true); - } - } - private static class NonOpaqueFrame extends Frame { - NonOpaqueFrame(GraphicsConfiguration gc) { - super("NonOpaque AWT Frame", gc); - // uncomment to test with hw child -// setLayout(null); -// Component c = new Panel() { -// public void paint(Graphics g) { -// g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f)); -// g.fillRect(0, 0, getWidth(), getHeight()); -// } -// }; -// c.setSize(100, 100); -// c.setBackground(Color.red); -// c.setForeground(Color.red); -// add(c); -// c.setLocation(130, 130); - } - @Override - public void paint(Graphics g) { - render(g, getWidth(), getHeight(), true); - g.setColor(Color.red); - g.drawString("Non-Opaque AWT Frame", 10, 15); - } - } - - private static class MyJPanel extends JPanel { - @Override - public void paintComponent(Graphics g) { - render(g, getWidth(), getHeight(), false); - } - } - - public static Frame createGui(GraphicsConfiguration gc, - final boolean useSwing, - final boolean useShape, - final boolean useTransl, - final boolean useNonOpaque, - final float factor) - { - Frame frame; - done = false; - - if (gc == null) { - gc = GraphicsEnvironment.getLocalGraphicsEnvironment(). - getDefaultScreenDevice().getDefaultConfiguration(); - } - - if (useNonOpaque) { - if (useSwing) { - frame = new NonOpaqueJFrame(gc); -// frame = new NonOpaqueJAppletFrame(gc); - } else { - frame = new NonOpaqueFrame(gc); - } - animateComponent(frame); - } else if (useSwing) { - frame = new JFrame("Swing Frame", gc); - JComponent p = new JButton("Swing!"); - p.setPreferredSize(new Dimension(200, 100)); - frame.add("North", p); - p = new MyJPanel(); - animateComponent(p); - frame.add("Center", p); - } else { - frame = new Frame("AWT Frame", gc) { - public void paint(Graphics g) { - g.setColor(Color.red); - g.fillRect(0, 0, 100, 100); - } - }; - frame.setLayout(new BorderLayout()); - Canvas c = new MyCanvas(); - frame.add("North", c); - animateComponent(c); - c = new MyCanvas(); - frame.add("Center", c); - animateComponent(c); - c = new MyCanvas(); - frame.add("South", c); - animateComponent(c); - } - final Frame finalFrame = frame; - frame.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - finalFrame.dispose(); - done = true; - } - }); - frame.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - finalFrame.dispose(); - done = true; - } - }); - frame.setPreferredSize(new Dimension(800, 600)); - - if (useShape) { - frame.setUndecorated(true); - } - - frame.setLocation(450, 10); - frame.pack(); - - if (useShape) { - if (AWTUtilities.isTranslucencySupported(PERPIXEL_TRANSPARENT)) { - System.out.println("applying PERPIXEL_TRANSPARENT"); - AWTUtilities.setWindowShape(frame, - new Ellipse2D.Double(0, 0, frame.getWidth(), - frame.getHeight()/3)); - frame.setTitle("PERPIXEL_TRANSPARENT"); - } else { - System.out.println("Passed: PERPIXEL_TRANSPARENT unsupported"); - } - } - if (useTransl) { - if (AWTUtilities.isTranslucencySupported(TRANSLUCENT)) { - System.out.println("applying TRANSLUCENT"); - AWTUtilities.setWindowOpacity(frame, factor); - frame.setTitle("TRANSLUCENT"); - } else { - System.out.println("Passed: TRANSLUCENT unsupported"); - } - } - if (useNonOpaque) { - if (AWTUtilities.isTranslucencySupported(PERPIXEL_TRANSLUCENT) && - AWTUtilities.isTranslucencyCapable(gc)) - { - System.out.println("applying PERPIXEL_TRANSLUCENT"); - AWTUtilities.setWindowOpaque(frame, false); - frame.setTitle("PERPIXEL_TRANSLUCENT"); - } else { - System.out.println("Passed: PERPIXEL_TRANSLUCENT unsupported"); - } - } - frame.setVisible(true); - return frame; - } - - public static void stopThreads() { - done = true; - } - - private static void animateComponent(final Component comp) { - Thread t = new Thread(new Runnable() { - public void run() { - do { - try { - Thread.sleep(50); - } catch (InterruptedException ex) {} - comp.repaint(); - } while (!done); - } - }); - t.start(); - } - - public static void main(String[] args) throws Exception { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - TSFrame.createGui(null, useSwing, - useShape, - useTransl, - useNonOpaque, - 0.7f); - } - }); - } -} diff -r ea80a312972e -r aa45a227fce3 jdk/test/com/sun/awt/Translucency/TranslucentShapedFrameTest/TranslucentShapedFrameTest.form --- a/jdk/test/com/sun/awt/Translucency/TranslucentShapedFrameTest/TranslucentShapedFrameTest.form Mon Apr 20 19:18:41 2009 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,230 +0,0 @@ - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff -r ea80a312972e -r aa45a227fce3 jdk/test/com/sun/awt/Translucency/TranslucentShapedFrameTest/TranslucentShapedFrameTest.java --- a/jdk/test/com/sun/awt/Translucency/TranslucentShapedFrameTest/TranslucentShapedFrameTest.java Mon Apr 20 19:18:41 2009 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,359 +0,0 @@ -/* - * Copyright 2008-2009 Sun Microsystems, Inc. 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. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the LICENSE file that accompanied this code. - * - * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, - * CA 95054 USA or visit www.sun.com if you need additional information or - * have any questions. - */ - -/* - * @test %I% %E% - * @bug 6655001 6670649 6687141 - * @summary Tests that hw acceleration doesn't affect translucent/shaped windows - * @author Dmitri.Trembovetski@sun.com: area=Graphics - * @compile -XDignore.symbol.file=true TranslucentShapedFrameTest.java - * @compile -XDignore.symbol.file=true TSFrame.java - * @run main/manual/othervm TranslucentShapedFrameTest - * @run main/manual/othervm -Dsun.java2d.noddraw=true TranslucentShapedFrameTest - * @run main/manual/othervm -Dsun.java2d.opengl=True TranslucentShapedFrameTest - */ -import com.sun.awt.AWTUtilities; -import static com.sun.awt.AWTUtilities.Translucency.*; -import java.awt.Frame; -import java.awt.GraphicsConfiguration; -import java.awt.GraphicsDevice; -import java.awt.GraphicsEnvironment; -import java.awt.Shape; -import java.awt.geom.Ellipse2D; -import java.util.concurrent.CountDownLatch; -import javax.swing.JSlider; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; - -public class TranslucentShapedFrameTest extends javax.swing.JFrame { - Frame testFrame; - static CountDownLatch done; - static volatile boolean failed = false; - GraphicsConfiguration gcToUse = null; - - /** - * Creates new form TranslucentShapedFrameTest - */ - public TranslucentShapedFrameTest() { - // not necessary, but we just look nicer - try { - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch (Exception ex) {} - - initComponents(); - checkEffects(); - - SwingUtilities.updateComponentTreeUI(this); - } - - /** This method is called from within the constructor to - * initialize the form. - * WARNING: Do NOT modify this code. The content of this method is - * always regenerated by the Form Editor. - */ - // //GEN-BEGIN:initComponents - private void initComponents() { - createDisposeGrp = new javax.swing.ButtonGroup(); - jLabel1 = new javax.swing.JLabel(); - transparencySld = new javax.swing.JSlider(); - shapedCb = new javax.swing.JCheckBox(); - nonOpaqueChb = new javax.swing.JCheckBox(); - jScrollPane1 = new javax.swing.JScrollPane(); - jTextArea1 = new javax.swing.JTextArea(); - jLabel2 = new javax.swing.JLabel(); - passedBtn = new javax.swing.JButton(); - failedBtn = new javax.swing.JButton(); - createFrameBtn = new javax.swing.JToggleButton(); - disposeFrameBtn = new javax.swing.JToggleButton(); - useSwingCb = new javax.swing.JCheckBox(); - - setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - setTitle("TranslucentShapedFrameTest"); - jLabel1.setText("Frame Opacity:"); - - transparencySld.setMajorTickSpacing(10); - transparencySld.setMinorTickSpacing(5); - transparencySld.setPaintLabels(true); - transparencySld.setPaintTicks(true); - transparencySld.setValue(100); - transparencySld.addChangeListener(new javax.swing.event.ChangeListener() { - public void stateChanged(javax.swing.event.ChangeEvent evt) { - transparencySldStateChanged(evt); - } - }); - - shapedCb.setText("Shaped Frame"); - shapedCb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); - shapedCb.setMargin(new java.awt.Insets(0, 0, 0, 0)); - shapedCb.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - shapedCbActionPerformed(evt); - } - }); - - nonOpaqueChb.setText("Non Opaque Frame"); - nonOpaqueChb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); - nonOpaqueChb.setMargin(new java.awt.Insets(0, 0, 0, 0)); - nonOpaqueChb.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - nonOpaqueChbActionPerformed(evt); - } - }); - - jTextArea1.setColumns(20); - jTextArea1.setRows(5); - jTextArea1.setText("Create translucent and/or shaped, or\nnon-opaque frame. Make sure it behaves\ncorrectly (no artifacts left on the screen\nwhen dragging - if dragging is possible).\nClick \"Passed\" if the test behaves correctly,\n\"Falied\" otherwise."); - jScrollPane1.setViewportView(jTextArea1); - - jLabel2.setText("Instructions:"); - - passedBtn.setBackground(new java.awt.Color(129, 255, 100)); - passedBtn.setText("Passed"); - passedBtn.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - passedBtnActionPerformed(evt); - } - }); - - failedBtn.setBackground(java.awt.Color.red); - failedBtn.setText("Failed"); - failedBtn.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - failedBtnActionPerformed(evt); - } - }); - - createDisposeGrp.add(createFrameBtn); - createFrameBtn.setText("Create Frame"); - createFrameBtn.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - createFrameBtnActionPerformed(evt); - } - }); - - createDisposeGrp.add(disposeFrameBtn); - disposeFrameBtn.setSelected(true); - disposeFrameBtn.setText("Dispose Frame"); - disposeFrameBtn.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - disposeFrameBtnActionPerformed(evt); - } - }); - - useSwingCb.setText("Use JFrame"); - useSwingCb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); - useSwingCb.setMargin(new java.awt.Insets(0, 0, 0, 0)); - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); - getContentPane().setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addComponent(transparencySld, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE) - .addContainerGap()) - .addComponent(jLabel1) - .addGroup(layout.createSequentialGroup() - .addComponent(shapedCb) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(nonOpaqueChb) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(useSwingCb) - .addContainerGap(102, Short.MAX_VALUE)) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addComponent(jLabel2) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 314, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGroup(layout.createSequentialGroup() - .addComponent(passedBtn) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(failedBtn) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 241, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE) - .addGroup(layout.createSequentialGroup() - .addComponent(createFrameBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(disposeFrameBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))) - .addContainerGap()))) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(transparencySld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(shapedCb) - .addComponent(nonOpaqueChb) - .addComponent(useSwingCb)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(disposeFrameBtn) - .addComponent(createFrameBtn)) - .addGap(17, 17, 17) - .addComponent(jLabel2) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(passedBtn) - .addComponent(failedBtn)) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); - pack(); - }// //GEN-END:initComponents - - private void nonOpaqueChbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nonOpaqueChbActionPerformed - if (testFrame != null) { - // REMIND: this path in the test doesn't work well (test bug) -// AWTUtilities.setWindowOpaque(testFrame, !nonOpaqueChb.isSelected()); - } - }//GEN-LAST:event_nonOpaqueChbActionPerformed - - private void shapedCbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shapedCbActionPerformed - if (testFrame != null) { - Shape s = null; - if (shapedCb.isSelected()) { - s = new Ellipse2D.Double(0, 0, - testFrame.getWidth(), - testFrame.getHeight()); - } - AWTUtilities.setWindowShape(testFrame, s); - } - }//GEN-LAST:event_shapedCbActionPerformed - - private void transparencySldStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_transparencySldStateChanged - JSlider source = (JSlider)evt.getSource(); - int transl = transparencySld.getValue(); - if (testFrame != null) { - AWTUtilities.setWindowOpacity(testFrame, (float)transl/100f); - } - }//GEN-LAST:event_transparencySldStateChanged - - private void failedBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_failedBtnActionPerformed - disposeFrameBtnActionPerformed(evt); - dispose(); - failed = true; - done.countDown(); - }//GEN-LAST:event_failedBtnActionPerformed - - private void disposeFrameBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disposeFrameBtnActionPerformed - TSFrame.stopThreads(); - if (testFrame != null) { - testFrame.dispose(); - testFrame = null; - } - }//GEN-LAST:event_disposeFrameBtnActionPerformed - - private void createFrameBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createFrameBtnActionPerformed - disposeFrameBtnActionPerformed(evt); - int transl = transparencySld.getValue(); - testFrame = TSFrame.createGui(gcToUse, - useSwingCb.isSelected(), shapedCb.isSelected(), - (transl < 100), nonOpaqueChb.isSelected(), - (float)transl/100f); - }//GEN-LAST:event_createFrameBtnActionPerformed - - private void passedBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passedBtnActionPerformed - disposeFrameBtnActionPerformed(evt); - dispose(); - done.countDown(); - }//GEN-LAST:event_passedBtnActionPerformed - - /** - * @param args the command line arguments - */ - public static void main(String args[]) { - done = new CountDownLatch(1); - java.awt.EventQueue.invokeLater(new Runnable() { - public void run() { - new TranslucentShapedFrameTest().setVisible(true); - } - }); - try { - done.await(); - } catch (InterruptedException ex) {} - if (failed) { - throw new RuntimeException("Test FAILED"); - } - System.out.println("Test PASSED"); - } - - private void checkEffects() { - if (!AWTUtilities.isTranslucencySupported(PERPIXEL_TRANSPARENT)) { - shapedCb.setEnabled(false); - } - - if (!AWTUtilities.isTranslucencySupported(TRANSLUCENT)) { - transparencySld.setEnabled(false); - } - - GraphicsConfiguration gc = null; - if (AWTUtilities.isTranslucencySupported(PERPIXEL_TRANSLUCENT)) { - gc = findGraphicsConfig(); - if (gc == null) { - nonOpaqueChb.setEnabled(false); - } - } - - gcToUse = gc; - } - - private GraphicsConfiguration findGraphicsConfig() { - GraphicsDevice gd = - GraphicsEnvironment.getLocalGraphicsEnvironment(). - getDefaultScreenDevice(); - GraphicsConfiguration gcs[] = gd.getConfigurations(); - for (GraphicsConfiguration gc : gcs) { - if (AWTUtilities.isTranslucencyCapable(gc)) { - return gc; - } - } - return null; - } - - // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.ButtonGroup createDisposeGrp; - private javax.swing.JToggleButton createFrameBtn; - private javax.swing.JToggleButton disposeFrameBtn; - private javax.swing.JButton failedBtn; - private javax.swing.JLabel jLabel1; - private javax.swing.JLabel jLabel2; - private javax.swing.JScrollPane jScrollPane1; - private javax.swing.JTextArea jTextArea1; - private javax.swing.JCheckBox nonOpaqueChb; - private javax.swing.JButton passedBtn; - private javax.swing.JCheckBox shapedCb; - private javax.swing.JSlider transparencySld; - private javax.swing.JCheckBox useSwingCb; - // End of variables declaration//GEN-END:variables - -} diff -r ea80a312972e -r aa45a227fce3 jdk/test/java/awt/Window/TranslucentJAppletTest/TranslucentJAppletTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/awt/Window/TranslucentJAppletTest/TranslucentJAppletTest.java Tue Apr 21 11:35:52 2009 +0400 @@ -0,0 +1,103 @@ +/* + * Copyright 2008-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test %I% %E% + * @bug 6683728 + * @summary Tests that a JApplet in a translucent JFrame works properly + * @author Kenneth.Russell@sun.com: area=Graphics + * @compile -XDignore.symbol.file=true TranslucentJAppletTest.java + * @run main/manual/othervm TranslucentJAppletTest + */ + +import java.awt.*; +import java.awt.image.*; + +import javax.swing.*; + +public class TranslucentJAppletTest { + + private static JFrame frame; + private static volatile boolean paintComponentCalled = false; + + private static void initAndShowGUI() { + frame = new JFrame(); + JApplet applet = new JApplet(); + JPanel panel = new JPanel() { + protected void paintComponent(Graphics g) { + paintComponentCalled = true; + g.setColor(Color.RED); + g.fillOval(0, 0, getWidth(), getHeight()); + } + }; + panel.setDoubleBuffered(false); + panel.setOpaque(false); + applet.add(panel); + frame.add(applet); + frame.setBounds(100, 100, 200, 200); + frame.setUndecorated(true); + frame.setBackground(new Color(0, 0, 0, 0)); + frame.setVisible(true); + } + + public static void main(String[] args) + throws Exception + { + sun.awt.SunToolkit tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit(); + + Robot r = new Robot(); + Color color1 = r.getPixelColor(100, 100); // (0, 0) in frame coordinates + + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + initAndShowGUI(); + } + }); + tk.realSync(); + + if (!paintComponentCalled) { + throw new RuntimeException("Test FAILED: panel's paintComponent() method is not called"); + } + + Color newColor1 = r.getPixelColor(100, 100); + // unfortunately, robot.getPixelColor() doesn't work for some unknown reason + // Color newColor2 = r.getPixelColor(200, 200); + BufferedImage bim = r.createScreenCapture(new Rectangle(200, 200, 1, 1)); + Color newColor2 = new Color(bim.getRGB(0, 0)); + + // Frame must be transparent at (100, 100) in screen coords + if (!color1.equals(newColor1)) { + System.err.println("color1 = " + color1); + System.err.println("newColor1 = " + newColor1); + throw new RuntimeException("Test FAILED: frame pixel at (0, 0) is not transparent"); + } + + // Frame must be RED at (200, 200) in screen coords + if (!newColor2.equals(Color.RED)) { + System.err.println("newColor2 = " + newColor2); + throw new RuntimeException("Test FAILED: frame pixel at (100, 100) is not red (transparent?)"); + } + + System.out.println("Test PASSED"); + } +} diff -r ea80a312972e -r aa45a227fce3 jdk/test/java/awt/Window/TranslucentShapedFrameTest/TSFrame.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/awt/Window/TranslucentShapedFrameTest/TSFrame.java Tue Apr 21 11:35:52 2009 +0400 @@ -0,0 +1,299 @@ +/* + * Copyright 2008-2009 Sun Microsystems, Inc. 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.GraphicsDevice.WindowTranslucency; +import java.awt.GraphicsEnvironment; +import java.awt.RenderingHints; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.Canvas; +import java.awt.Component; +import java.awt.GradientPaint; +import java.awt.Graphics2D; +import java.awt.Paint; +import java.util.Random; +import java.awt.geom.Ellipse2D; +import javax.swing.JApplet; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; + +public class TSFrame { + + static volatile boolean done = false; + + static final boolean useSwing = System.getProperty("useswing") != null; + static final boolean useShape = System.getProperty("useshape") != null; + static final boolean useTransl = System.getProperty("usetransl") != null; + static final boolean useNonOpaque = System.getProperty("usenonop") != null; + + static final Random rnd = new Random(); + private static void render(Graphics g, int w, int h, boolean useNonOpaque) { + if (useNonOpaque) { + Graphics2D g2d = (Graphics2D)g; + GradientPaint p = + new GradientPaint(0.0f, 0.0f, + new Color(rnd.nextInt(0xffffff)), + w, h, + new Color(rnd.nextInt(0xff), + rnd.nextInt(0xff), + rnd.nextInt(0xff), 0), + true); + g2d.setPaint(p); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + g2d.fillOval(0, 0, w, h); + } else { + g.setColor(new Color(rnd.nextInt(0xffffff))); + g.fillRect(0, 0, w, h); + } + } + + private static class MyCanvas extends Canvas { + @Override + public void paint(Graphics g) { + render(g, getWidth(), getHeight(), false); + } + @Override + public Dimension getPreferredSize() { + return new Dimension(200, 100); + } + } + private static class NonOpaqueJFrame extends JFrame { + NonOpaqueJFrame() { + super("NonOpaque Swing JFrame"); + JPanel p = new JPanel() { + public void paintComponent(Graphics g) { + super.paintComponent(g); + render(g, getWidth(), getHeight(), true); + g.setColor(Color.red); + g.drawString("Non-Opaque Swing JFrame", 10, 15); + } + }; + p.setDoubleBuffered(false); + p.setOpaque(false); + add(p); + setUndecorated(true); + } + } + private static class NonOpaqueJAppletFrame extends JFrame { + JPanel p; + NonOpaqueJAppletFrame() { + super("NonOpaque Swing JAppletFrame"); + JApplet ja = new JApplet() { + public void paint(Graphics g) { + super.paint(g); + System.err.println("JAppletFrame paint called"); + } + }; + p = new JPanel() { + public void paintComponent(Graphics g) { + super.paintComponent(g); + render(g, getWidth(), getHeight(), true); + g.setColor(Color.red); + g.drawString("Non-Opaque Swing JFrame", 10, 15); + } + }; + p.setDoubleBuffered(false); + p.setOpaque(false); + ja.add(p); + add(ja); + setUndecorated(true); + } + } + private static class NonOpaqueFrame extends Frame { + NonOpaqueFrame() { + super("NonOpaque AWT Frame"); + // uncomment to test with hw child +// setLayout(null); +// Component c = new Panel() { +// public void paint(Graphics g) { +// g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f)); +// g.fillRect(0, 0, getWidth(), getHeight()); +// } +// }; +// c.setSize(100, 100); +// c.setBackground(Color.red); +// c.setForeground(Color.red); +// add(c); +// c.setLocation(130, 130); + } + @Override + public void paint(Graphics g) { + render(g, getWidth(), getHeight(), true); + g.setColor(Color.red); + g.drawString("Non-Opaque AWT Frame", 10, 15); + } + } + + private static class MyJPanel extends JPanel { + @Override + public void paintComponent(Graphics g) { + render(g, getWidth(), getHeight(), false); + } + } + + public static Frame createGui( + final boolean useSwing, + final boolean useShape, + final boolean useTransl, + final boolean useNonOpaque, + final float factor) + { + Frame frame; + done = false; + + if (useNonOpaque) { + if (useSwing) { + frame = new NonOpaqueJFrame(); +// frame = new NonOpaqueJAppletFrame(gc); + } else { + frame = new NonOpaqueFrame(); + } + animateComponent(frame); + } else if (useSwing) { + frame = new JFrame("Swing Frame"); + JComponent p = new JButton("Swing!"); + p.setPreferredSize(new Dimension(200, 100)); + frame.add("North", p); + p = new MyJPanel(); + animateComponent(p); + frame.add("Center", p); + } else { + frame = new Frame("AWT Frame") { + public void paint(Graphics g) { + g.setColor(Color.red); + g.fillRect(0, 0, 100, 100); + } + }; + frame.setLayout(new BorderLayout()); + Canvas c = new MyCanvas(); + frame.add("North", c); + animateComponent(c); + c = new MyCanvas(); + frame.add("Center", c); + animateComponent(c); + c = new MyCanvas(); + frame.add("South", c); + animateComponent(c); + } + final Frame finalFrame = frame; + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + finalFrame.dispose(); + done = true; + } + }); + frame.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + finalFrame.dispose(); + done = true; + } + }); + frame.setPreferredSize(new Dimension(800, 600)); + + if (useShape) { + frame.setUndecorated(true); + } + + frame.setLocation(450, 10); + frame.pack(); + + GraphicsDevice gd = frame.getGraphicsConfiguration().getDevice(); + if (useShape) { + if (gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) { + System.out.println("applying PERPIXEL_TRANSPARENT"); + frame.setShape(new Ellipse2D.Double(0, 0, frame.getWidth(), + frame.getHeight()/3)); + frame.setTitle("PERPIXEL_TRANSPARENT"); + } else { + System.out.println("Passed: PERPIXEL_TRANSPARENT unsupported"); + } + } + if (useTransl) { + if (gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) { + System.out.println("applying TRANSLUCENT"); + frame.setOpacity(factor); + frame.setTitle("TRANSLUCENT"); + } else { + System.out.println("Passed: TRANSLUCENT unsupported"); + } + } + if (useNonOpaque) { + if (gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) { + System.out.println("applying PERPIXEL_TRANSLUCENT"); + frame.setBackground(new Color(0, 0, 0, 0)); + frame.setTitle("PERPIXEL_TRANSLUCENT"); + } else { + System.out.println("Passed: PERPIXEL_TRANSLUCENT unsupported"); + } + } + frame.setVisible(true); + return frame; + } + + public static void stopThreads() { + done = true; + } + + private static void animateComponent(final Component comp) { + Thread t = new Thread(new Runnable() { + public void run() { + do { + try { + Thread.sleep(50); + } catch (InterruptedException ex) {} + comp.repaint(); + } while (!done); + } + }); + t.start(); + } + + public static void main(String[] args) throws Exception { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + TSFrame.createGui(useSwing, + useShape, + useTransl, + useNonOpaque, + 0.7f); + } + }); + } +} diff -r ea80a312972e -r aa45a227fce3 jdk/test/java/awt/Window/TranslucentShapedFrameTest/TranslucentShapedFrameTest.form --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/awt/Window/TranslucentShapedFrameTest/TranslucentShapedFrameTest.form Tue Apr 21 11:35:52 2009 +0400 @@ -0,0 +1,230 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff -r ea80a312972e -r aa45a227fce3 jdk/test/java/awt/Window/TranslucentShapedFrameTest/TranslucentShapedFrameTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/awt/Window/TranslucentShapedFrameTest/TranslucentShapedFrameTest.java Tue Apr 21 11:35:52 2009 +0400 @@ -0,0 +1,339 @@ +/* + * Copyright 2008-2009 Sun Microsystems, Inc. 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test %I% %E% + * @bug 6655001 6670649 6687141 + * @summary Tests that hw acceleration doesn't affect translucent/shaped windows + * @author Dmitri.Trembovetski@sun.com: area=Graphics + * @compile -XDignore.symbol.file=true TranslucentShapedFrameTest.java + * @compile -XDignore.symbol.file=true TSFrame.java + * @run main/manual/othervm TranslucentShapedFrameTest + * @run main/manual/othervm -Dsun.java2d.noddraw=true TranslucentShapedFrameTest + * @run main/manual/othervm -Dsun.java2d.opengl=True TranslucentShapedFrameTest + */ +import java.awt.Color; +import java.awt.Frame; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.GraphicsDevice.WindowTranslucency; +import java.awt.GraphicsEnvironment; +import java.awt.Shape; +import java.awt.geom.Ellipse2D; +import java.util.concurrent.CountDownLatch; +import javax.swing.JSlider; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; + +public class TranslucentShapedFrameTest extends javax.swing.JFrame { + Frame testFrame; + static CountDownLatch done; + static volatile boolean failed = false; + GraphicsConfiguration gcToUse = null; + + /** + * Creates new form TranslucentShapedFrameTest + */ + public TranslucentShapedFrameTest() { + // not necessary, but we just look nicer + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception ex) {} + + initComponents(); + checkEffects(); + + SwingUtilities.updateComponentTreeUI(this); + } + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // //GEN-BEGIN:initComponents + private void initComponents() { + createDisposeGrp = new javax.swing.ButtonGroup(); + jLabel1 = new javax.swing.JLabel(); + transparencySld = new javax.swing.JSlider(); + shapedCb = new javax.swing.JCheckBox(); + nonOpaqueChb = new javax.swing.JCheckBox(); + jScrollPane1 = new javax.swing.JScrollPane(); + jTextArea1 = new javax.swing.JTextArea(); + jLabel2 = new javax.swing.JLabel(); + passedBtn = new javax.swing.JButton(); + failedBtn = new javax.swing.JButton(); + createFrameBtn = new javax.swing.JToggleButton(); + disposeFrameBtn = new javax.swing.JToggleButton(); + useSwingCb = new javax.swing.JCheckBox(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle("TranslucentShapedFrameTest"); + jLabel1.setText("Frame Opacity:"); + + transparencySld.setMajorTickSpacing(10); + transparencySld.setMinorTickSpacing(5); + transparencySld.setPaintLabels(true); + transparencySld.setPaintTicks(true); + transparencySld.setValue(100); + transparencySld.addChangeListener(new javax.swing.event.ChangeListener() { + public void stateChanged(javax.swing.event.ChangeEvent evt) { + transparencySldStateChanged(evt); + } + }); + + shapedCb.setText("Shaped Frame"); + shapedCb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); + shapedCb.setMargin(new java.awt.Insets(0, 0, 0, 0)); + shapedCb.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + shapedCbActionPerformed(evt); + } + }); + + nonOpaqueChb.setText("Non Opaque Frame"); + nonOpaqueChb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); + nonOpaqueChb.setMargin(new java.awt.Insets(0, 0, 0, 0)); + nonOpaqueChb.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + nonOpaqueChbActionPerformed(evt); + } + }); + + jTextArea1.setColumns(20); + jTextArea1.setRows(5); + jTextArea1.setText("Create translucent and/or shaped, or\nnon-opaque frame. Make sure it behaves\ncorrectly (no artifacts left on the screen\nwhen dragging - if dragging is possible).\nClick \"Passed\" if the test behaves correctly,\n\"Falied\" otherwise."); + jScrollPane1.setViewportView(jTextArea1); + + jLabel2.setText("Instructions:"); + + passedBtn.setBackground(new Color(129, 255, 100)); + passedBtn.setText("Passed"); + passedBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + passedBtnActionPerformed(evt); + } + }); + + failedBtn.setBackground(Color.red); + failedBtn.setText("Failed"); + failedBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + failedBtnActionPerformed(evt); + } + }); + + createDisposeGrp.add(createFrameBtn); + createFrameBtn.setText("Create Frame"); + createFrameBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + createFrameBtnActionPerformed(evt); + } + }); + + createDisposeGrp.add(disposeFrameBtn); + disposeFrameBtn.setSelected(true); + disposeFrameBtn.setText("Dispose Frame"); + disposeFrameBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + disposeFrameBtnActionPerformed(evt); + } + }); + + useSwingCb.setText("Use JFrame"); + useSwingCb.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); + useSwingCb.setMargin(new java.awt.Insets(0, 0, 0, 0)); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(transparencySld, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE) + .addContainerGap()) + .addComponent(jLabel1) + .addGroup(layout.createSequentialGroup() + .addComponent(shapedCb) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(nonOpaqueChb) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(useSwingCb) + .addContainerGap(102, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(jLabel2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 314, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(passedBtn) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(failedBtn) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 241, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addComponent(createFrameBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(disposeFrameBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addContainerGap()))) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(transparencySld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(shapedCb) + .addComponent(nonOpaqueChb) + .addComponent(useSwingCb)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(disposeFrameBtn) + .addComponent(createFrameBtn)) + .addGap(17, 17, 17) + .addComponent(jLabel2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(passedBtn) + .addComponent(failedBtn)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + pack(); + }// //GEN-END:initComponents + + private void nonOpaqueChbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nonOpaqueChbActionPerformed + if (testFrame != null) { + // REMIND: this path in the test doesn't work well (test bug) + testFrame.setBackground(new Color(0, 0, 0, nonOpaqueChb.isSelected() ? 0 : 255)); + } + }//GEN-LAST:event_nonOpaqueChbActionPerformed + + private void shapedCbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shapedCbActionPerformed + if (testFrame != null) { + Shape s = null; + if (shapedCb.isSelected()) { + s = new Ellipse2D.Double(0, 0, + testFrame.getWidth(), + testFrame.getHeight()); + } + testFrame.setShape(s); + } + }//GEN-LAST:event_shapedCbActionPerformed + + private void transparencySldStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_transparencySldStateChanged + JSlider source = (JSlider)evt.getSource(); + int transl = transparencySld.getValue(); + if (testFrame != null) { + testFrame.setOpacity((float)transl/100f); + } + }//GEN-LAST:event_transparencySldStateChanged + + private void failedBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_failedBtnActionPerformed + disposeFrameBtnActionPerformed(evt); + dispose(); + failed = true; + done.countDown(); + }//GEN-LAST:event_failedBtnActionPerformed + + private void disposeFrameBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disposeFrameBtnActionPerformed + TSFrame.stopThreads(); + if (testFrame != null) { + testFrame.dispose(); + testFrame = null; + } + }//GEN-LAST:event_disposeFrameBtnActionPerformed + + private void createFrameBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createFrameBtnActionPerformed + disposeFrameBtnActionPerformed(evt); + int transl = transparencySld.getValue(); + testFrame = TSFrame.createGui( + useSwingCb.isSelected(), shapedCb.isSelected(), + (transl < 100), nonOpaqueChb.isSelected(), + (float)transl/100f); + }//GEN-LAST:event_createFrameBtnActionPerformed + + private void passedBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passedBtnActionPerformed + disposeFrameBtnActionPerformed(evt); + dispose(); + done.countDown(); + }//GEN-LAST:event_passedBtnActionPerformed + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + done = new CountDownLatch(1); + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new TranslucentShapedFrameTest().setVisible(true); + } + }); + try { + done.await(); + } catch (InterruptedException ex) {} + if (failed) { + throw new RuntimeException("Test FAILED"); + } + System.out.println("Test PASSED"); + } + + private void checkEffects() { + GraphicsDevice gd = getGraphicsConfiguration().getDevice(); + if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) { + shapedCb.setEnabled(false); + } + if (!gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) { + transparencySld.setEnabled(false); + } + if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) { + nonOpaqueChb.setEnabled(false); + } + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.ButtonGroup createDisposeGrp; + private javax.swing.JToggleButton createFrameBtn; + private javax.swing.JToggleButton disposeFrameBtn; + private javax.swing.JButton failedBtn; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTextArea jTextArea1; + private javax.swing.JCheckBox nonOpaqueChb; + private javax.swing.JButton passedBtn; + private javax.swing.JCheckBox shapedCb; + private javax.swing.JSlider transparencySld; + private javax.swing.JCheckBox useSwingCb; + // End of variables declaration//GEN-END:variables + +}