< prev index next >

src/java.desktop/share/classes/java/awt/Component.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 335      * A reference to a {@code GraphicsConfiguration} object
 336      * used to describe the characteristics of a graphics
 337      * destination.
 338      * This value can be {@code null}.
 339      *
 340      * @since 1.3
 341      * @serial
 342      * @see GraphicsConfiguration
 343      * @see #getGraphicsConfiguration
 344      */
 345     private transient volatile GraphicsConfiguration graphicsConfig;
 346 
 347     /**
 348      * A reference to a {@code BufferStrategy} object
 349      * used to manipulate the buffers on this component.
 350      *
 351      * @since 1.4
 352      * @see java.awt.image.BufferStrategy
 353      * @see #getBufferStrategy()
 354      */
 355     private transient BufferStrategy bufferStrategy = null;
 356 
 357     /**
 358      * True when the object should ignore all repaint events.
 359      *
 360      * @since 1.4
 361      * @serial
 362      * @see #setIgnoreRepaint
 363      * @see #getIgnoreRepaint
 364      */
 365     boolean ignoreRepaint = false;
 366 
 367     /**
 368      * True when the object is visible. An object that is not
 369      * visible is not drawn on the screen.
 370      *
 371      * @serial
 372      * @see #isVisible
 373      * @see #setVisible
 374      */
 375     boolean visible = true;


4021          * The drawing buffer as a volatile image
4022          */
4023         protected VolatileImage drawVBuffer; // = null
4024         /**
4025          * Whether or not the drawing buffer has been recently restored from
4026          * a lost state.
4027          */
4028         protected boolean validatedContents; // = false
4029 
4030         /**
4031          * Size of the back buffers.  (Note: these fields were added in 6.0
4032          * but kept package-private to avoid exposing them in the spec.
4033          * None of these fields/methods really should have been marked
4034          * protected when they were introduced in 1.4, but now we just have
4035          * to live with that decision.)
4036          */
4037 
4038          /**
4039           * The width of the back buffers
4040           */
4041         private int width;
4042 
4043         /**
4044          * The height of the back buffers
4045          */
4046         private int height;
4047 
4048         /**
4049          * Creates a new flipping buffer strategy for this component.
4050          * The component must be a {@code Canvas} or {@code Window} or
4051          * {@code Applet}.
4052          * @see Canvas
4053          * @see Window
4054          * @see Applet
4055          * @param numBuffers the number of buffers
4056          * @param caps the capabilities of the buffers
4057          * @exception AWTException if the capabilities supplied could not be
4058          * supported or met
4059          * @exception ClassCastException if the component is not a canvas or
4060          * window.
4061          * @exception IllegalStateException if the component has no peer
4062          * @exception IllegalArgumentException if {@code numBuffers} is less than two,
4063          * or if {@code BufferCapabilities.isPageFlipping} is not
4064          * {@code true}.
4065          * @see #createBuffers(int, BufferCapabilities)
4066          */


4099         protected void createBuffers(int numBuffers, BufferCapabilities caps)
4100             throws AWTException
4101         {
4102             if (numBuffers < 2) {
4103                 throw new IllegalArgumentException(
4104                     "Number of buffers cannot be less than two");
4105             } else if (peer == null) {
4106                 throw new IllegalStateException(
4107                     "Component must have a valid peer");
4108             } else if (caps == null || !caps.isPageFlipping()) {
4109                 throw new IllegalArgumentException(
4110                     "Page flipping capabilities must be specified");
4111             }
4112 
4113             // save the current bounds
4114             width = getWidth();
4115             height = getHeight();
4116 
4117             if (drawBuffer != null) {
4118                 // dispose the existing backbuffers
4119                 invalidate();


4120                 // ... then recreate the backbuffers
4121             }
4122 
4123             if (caps instanceof ExtendedBufferCapabilities) {
4124                 ExtendedBufferCapabilities ebc =
4125                     (ExtendedBufferCapabilities)caps;
4126                 if (ebc.getVSync() == VSYNC_ON) {
4127                     // if this buffer strategy is not allowed to be v-synced,
4128                     // change the caps that we pass to the peer but keep on
4129                     // trying to create v-synced buffers;
4130                     // do not throw IAE here in case it is disallowed, see
4131                     // ExtendedBufferCapabilities for more info
4132                     if (!VSyncedBSManager.vsyncAllowed(this)) {
4133                         caps = ebc.derive(VSYNC_DEFAULT);
4134                     }
4135                 }
4136             }
4137 
4138             peer.createBuffers(numBuffers, caps);
4139             updateInternalBuffers();


4187                               backBuffer.getHeight(null), flipAction);
4188                 }
4189             } else {
4190                 throw new IllegalStateException(
4191                     "Component must have a valid peer");
4192             }
4193         }
4194 
4195         void flipSubRegion(int x1, int y1, int x2, int y2,
4196                       BufferCapabilities.FlipContents flipAction)
4197         {
4198             if (peer != null) {
4199                 peer.flip(x1, y1, x2, y2, flipAction);
4200             } else {
4201                 throw new IllegalStateException(
4202                     "Component must have a valid peer");
4203             }
4204         }
4205 
4206         /**
4207          * Destroys the buffers and invalidates the state of FlipBufferStrategy.
4208          */
4209         private void invalidate() {
4210             drawBuffer = null;
4211             drawVBuffer = null;
4212             destroyBuffers();
4213         }
4214 
4215         /**
4216          * Destroys the buffers created through this object
4217          */
4218         protected void destroyBuffers() {
4219             VSyncedBSManager.releaseVsync(this);
4220             if (peer != null) {
4221                 peer.destroyBuffers();
4222             } else {
4223                 throw new IllegalStateException(
4224                     "Component must have a valid peer");
4225             }
4226         }
4227 
4228         /**
4229          * @return the buffering capabilities of this strategy
4230          */
4231         public BufferCapabilities getCapabilities() {
4232             if (caps instanceof ProxyCapabilities) {
4233                 return ((ProxyCapabilities)caps).orig;
4234             } else {
4235                 return caps;
4236             }
4237         }
4238 
4239         /**
4240          * @return the graphics on the drawing buffer.  This method may not
4241          * be synchronized for performance reasons; use of this method by multiple
4242          * threads should be handled at the application level.  Disposal of the
4243          * graphics object must be handled by the application.
4244          */
4245         public Graphics getDrawGraphics() {
4246             revalidate();
4247             return drawBuffer.getGraphics();
4248         }
4249 
4250         /**
4251          * Restore the drawing buffer if it has been lost
4252          */
4253         protected void revalidate() {




4254             validatedContents = false;
4255             if (getWidth() != width || getHeight() != height
4256                     || drawBuffer == null) {
4257                 // component has been resized or the peer was recreated;
4258                 // recreate the backbuffers
4259                 try {
4260                     createBuffers(numBuffers, caps);
4261                 } catch (AWTException e) {
4262                     // shouldn't be possible
4263                 }
4264                 validatedContents = true;
4265             }
4266 
4267             // get the buffers from the peer every time since they
4268             // might have been replaced in response to a display change event
4269             updateInternalBuffers();
4270 
4271             // now validate the backbuffer
4272             if (drawVBuffer != null) {
4273                 GraphicsConfiguration gc =
4274                         getGraphicsConfiguration_NoClientCode();
4275                 int returnCode = drawVBuffer.validate(gc);
4276                 if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
4277                     try {
4278                         createBuffers(numBuffers, caps);


4316         public void show() {
4317             flip(caps.getFlipContents());
4318         }
4319 
4320         /**
4321          * Makes specified region of the next available buffer visible
4322          * by either blitting or flipping.
4323          */
4324         void showSubRegion(int x1, int y1, int x2, int y2) {
4325             flipSubRegion(x1, y1, x2, y2, caps.getFlipContents());
4326         }
4327 
4328         /**
4329          * {@inheritDoc}
4330          * @since 1.6
4331          */
4332         public void dispose() {
4333             if (Component.this.bufferStrategy == this) {
4334                 Component.this.bufferStrategy = null;
4335                 if (peer != null) {
4336                     invalidate();
4337                 }
4338             }
4339         }
4340 
4341     } // Inner class FlipBufferStrategy
4342 
4343     /**
4344      * Inner class for blitting offscreen surfaces to a component.
4345      *
4346      * @author Michael Martak
4347      * @since 1.4
4348      */
4349     protected class BltBufferStrategy extends BufferStrategy {
4350 
4351         /**
4352          * The buffering capabilities
4353          */
4354         protected BufferCapabilities caps; // = null
4355         /**
4356          * The back buffers


7143             int npopups = (popups != null? popups.size() : 0);
7144             for (int i = 0 ; i < npopups ; i++) {
7145                 PopupMenu popup = popups.elementAt(i);
7146                 popup.removeNotify();
7147             }
7148             // If there is any input context for this component, notify
7149             // that this component is being removed. (This has to be done
7150             // before hiding peer.)
7151             if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) {
7152                 InputContext inputContext = getInputContext();
7153                 if (inputContext != null) {
7154                     inputContext.removeNotify(this);
7155                 }
7156             }
7157 
7158             ComponentPeer p = peer;
7159             if (p != null) {
7160                 boolean isLightweight = isLightweight();
7161 
7162                 if (bufferStrategy instanceof FlipBufferStrategy) {
7163                     ((FlipBufferStrategy)bufferStrategy).invalidate();
7164                 }
7165 
7166                 if (dropTarget != null) dropTarget.removeNotify();
7167 
7168                 // Hide peer first to stop system events such as cursor moves.
7169                 if (visible) {
7170                     p.setVisible(false);
7171                 }
7172 
7173                 peer = null; // Stop peer updates.
7174                 peerFont = null;
7175 
7176                 Toolkit.getEventQueue().removeSourceEvents(this, false);
7177                 KeyboardFocusManager.getCurrentKeyboardFocusManager().
7178                     discardKeyEvents(this);
7179 
7180                 p.dispose();
7181 
7182                 mixOnHiding(isLightweight);
7183 


   1 /*
   2  * Copyright (c) 1995, 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 335      * A reference to a {@code GraphicsConfiguration} object
 336      * used to describe the characteristics of a graphics
 337      * destination.
 338      * This value can be {@code null}.
 339      *
 340      * @since 1.3
 341      * @serial
 342      * @see GraphicsConfiguration
 343      * @see #getGraphicsConfiguration
 344      */
 345     private transient volatile GraphicsConfiguration graphicsConfig;
 346 
 347     /**
 348      * A reference to a {@code BufferStrategy} object
 349      * used to manipulate the buffers on this component.
 350      *
 351      * @since 1.4
 352      * @see java.awt.image.BufferStrategy
 353      * @see #getBufferStrategy()
 354      */
 355     transient BufferStrategy bufferStrategy = null;
 356 
 357     /**
 358      * True when the object should ignore all repaint events.
 359      *
 360      * @since 1.4
 361      * @serial
 362      * @see #setIgnoreRepaint
 363      * @see #getIgnoreRepaint
 364      */
 365     boolean ignoreRepaint = false;
 366 
 367     /**
 368      * True when the object is visible. An object that is not
 369      * visible is not drawn on the screen.
 370      *
 371      * @serial
 372      * @see #isVisible
 373      * @see #setVisible
 374      */
 375     boolean visible = true;


4021          * The drawing buffer as a volatile image
4022          */
4023         protected VolatileImage drawVBuffer; // = null
4024         /**
4025          * Whether or not the drawing buffer has been recently restored from
4026          * a lost state.
4027          */
4028         protected boolean validatedContents; // = false
4029 
4030         /**
4031          * Size of the back buffers.  (Note: these fields were added in 6.0
4032          * but kept package-private to avoid exposing them in the spec.
4033          * None of these fields/methods really should have been marked
4034          * protected when they were introduced in 1.4, but now we just have
4035          * to live with that decision.)
4036          */
4037 
4038          /**
4039           * The width of the back buffers
4040           */
4041         int width;
4042 
4043         /**
4044          * The height of the back buffers
4045          */
4046         int height;
4047 
4048         /**
4049          * Creates a new flipping buffer strategy for this component.
4050          * The component must be a {@code Canvas} or {@code Window} or
4051          * {@code Applet}.
4052          * @see Canvas
4053          * @see Window
4054          * @see Applet
4055          * @param numBuffers the number of buffers
4056          * @param caps the capabilities of the buffers
4057          * @exception AWTException if the capabilities supplied could not be
4058          * supported or met
4059          * @exception ClassCastException if the component is not a canvas or
4060          * window.
4061          * @exception IllegalStateException if the component has no peer
4062          * @exception IllegalArgumentException if {@code numBuffers} is less than two,
4063          * or if {@code BufferCapabilities.isPageFlipping} is not
4064          * {@code true}.
4065          * @see #createBuffers(int, BufferCapabilities)
4066          */


4099         protected void createBuffers(int numBuffers, BufferCapabilities caps)
4100             throws AWTException
4101         {
4102             if (numBuffers < 2) {
4103                 throw new IllegalArgumentException(
4104                     "Number of buffers cannot be less than two");
4105             } else if (peer == null) {
4106                 throw new IllegalStateException(
4107                     "Component must have a valid peer");
4108             } else if (caps == null || !caps.isPageFlipping()) {
4109                 throw new IllegalArgumentException(
4110                     "Page flipping capabilities must be specified");
4111             }
4112 
4113             // save the current bounds
4114             width = getWidth();
4115             height = getHeight();
4116 
4117             if (drawBuffer != null) {
4118                 // dispose the existing backbuffers
4119                 drawBuffer = null;
4120                 drawVBuffer = null;
4121                 destroyBuffers();
4122                 // ... then recreate the backbuffers
4123             }
4124 
4125             if (caps instanceof ExtendedBufferCapabilities) {
4126                 ExtendedBufferCapabilities ebc =
4127                     (ExtendedBufferCapabilities)caps;
4128                 if (ebc.getVSync() == VSYNC_ON) {
4129                     // if this buffer strategy is not allowed to be v-synced,
4130                     // change the caps that we pass to the peer but keep on
4131                     // trying to create v-synced buffers;
4132                     // do not throw IAE here in case it is disallowed, see
4133                     // ExtendedBufferCapabilities for more info
4134                     if (!VSyncedBSManager.vsyncAllowed(this)) {
4135                         caps = ebc.derive(VSYNC_DEFAULT);
4136                     }
4137                 }
4138             }
4139 
4140             peer.createBuffers(numBuffers, caps);
4141             updateInternalBuffers();


4189                               backBuffer.getHeight(null), flipAction);
4190                 }
4191             } else {
4192                 throw new IllegalStateException(
4193                     "Component must have a valid peer");
4194             }
4195         }
4196 
4197         void flipSubRegion(int x1, int y1, int x2, int y2,
4198                       BufferCapabilities.FlipContents flipAction)
4199         {
4200             if (peer != null) {
4201                 peer.flip(x1, y1, x2, y2, flipAction);
4202             } else {
4203                 throw new IllegalStateException(
4204                     "Component must have a valid peer");
4205             }
4206         }
4207 
4208         /**









4209          * Destroys the buffers created through this object
4210          */
4211         protected void destroyBuffers() {
4212             VSyncedBSManager.releaseVsync(this);
4213             if (peer != null) {
4214                 peer.destroyBuffers();
4215             } else {
4216                 throw new IllegalStateException(
4217                     "Component must have a valid peer");
4218             }
4219         }
4220 
4221         /**
4222          * @return the buffering capabilities of this strategy
4223          */
4224         public BufferCapabilities getCapabilities() {
4225             if (caps instanceof ProxyCapabilities) {
4226                 return ((ProxyCapabilities)caps).orig;
4227             } else {
4228                 return caps;
4229             }
4230         }
4231 
4232         /**
4233          * @return the graphics on the drawing buffer.  This method may not
4234          * be synchronized for performance reasons; use of this method by multiple
4235          * threads should be handled at the application level.  Disposal of the
4236          * graphics object must be handled by the application.
4237          */
4238         public Graphics getDrawGraphics() {
4239             revalidate();
4240             return drawBuffer.getGraphics();
4241         }
4242 
4243         /**
4244          * Restore the drawing buffer if it has been lost
4245          */
4246         protected void revalidate() {
4247             revalidate(true);
4248         }
4249 
4250         void revalidate(boolean checkSize) {
4251             validatedContents = false;
4252 
4253             if (checkSize && (getWidth() != width || getHeight() != height)) {
4254                 // component has been resized; recreate the backbuffers

4255                 try {
4256                     createBuffers(numBuffers, caps);
4257                 } catch (AWTException e) {
4258                     // shouldn't be possible
4259                 }
4260                 validatedContents = true;
4261             }
4262 
4263             // get the buffers from the peer every time since they
4264             // might have been replaced in response to a display change event
4265             updateInternalBuffers();
4266 
4267             // now validate the backbuffer
4268             if (drawVBuffer != null) {
4269                 GraphicsConfiguration gc =
4270                         getGraphicsConfiguration_NoClientCode();
4271                 int returnCode = drawVBuffer.validate(gc);
4272                 if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
4273                     try {
4274                         createBuffers(numBuffers, caps);


4312         public void show() {
4313             flip(caps.getFlipContents());
4314         }
4315 
4316         /**
4317          * Makes specified region of the next available buffer visible
4318          * by either blitting or flipping.
4319          */
4320         void showSubRegion(int x1, int y1, int x2, int y2) {
4321             flipSubRegion(x1, y1, x2, y2, caps.getFlipContents());
4322         }
4323 
4324         /**
4325          * {@inheritDoc}
4326          * @since 1.6
4327          */
4328         public void dispose() {
4329             if (Component.this.bufferStrategy == this) {
4330                 Component.this.bufferStrategy = null;
4331                 if (peer != null) {
4332                     destroyBuffers();
4333                 }
4334             }
4335         }
4336 
4337     } // Inner class FlipBufferStrategy
4338 
4339     /**
4340      * Inner class for blitting offscreen surfaces to a component.
4341      *
4342      * @author Michael Martak
4343      * @since 1.4
4344      */
4345     protected class BltBufferStrategy extends BufferStrategy {
4346 
4347         /**
4348          * The buffering capabilities
4349          */
4350         protected BufferCapabilities caps; // = null
4351         /**
4352          * The back buffers


7139             int npopups = (popups != null? popups.size() : 0);
7140             for (int i = 0 ; i < npopups ; i++) {
7141                 PopupMenu popup = popups.elementAt(i);
7142                 popup.removeNotify();
7143             }
7144             // If there is any input context for this component, notify
7145             // that this component is being removed. (This has to be done
7146             // before hiding peer.)
7147             if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) {
7148                 InputContext inputContext = getInputContext();
7149                 if (inputContext != null) {
7150                     inputContext.removeNotify(this);
7151                 }
7152             }
7153 
7154             ComponentPeer p = peer;
7155             if (p != null) {
7156                 boolean isLightweight = isLightweight();
7157 
7158                 if (bufferStrategy instanceof FlipBufferStrategy) {
7159                     ((FlipBufferStrategy)bufferStrategy).destroyBuffers();
7160                 }
7161 
7162                 if (dropTarget != null) dropTarget.removeNotify();
7163 
7164                 // Hide peer first to stop system events such as cursor moves.
7165                 if (visible) {
7166                     p.setVisible(false);
7167                 }
7168 
7169                 peer = null; // Stop peer updates.
7170                 peerFont = null;
7171 
7172                 Toolkit.getEventQueue().removeSourceEvents(this, false);
7173                 KeyboardFocusManager.getCurrentKeyboardFocusManager().
7174                     discardKeyEvents(this);
7175 
7176                 p.dispose();
7177 
7178                 mixOnHiding(isLightweight);
7179 


< prev index next >