Path: blob/master/src/java.desktop/share/classes/java/awt/Canvas.java
41152 views
/*1* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.awt.image.BufferStrategy;28import java.awt.peer.CanvasPeer;29import java.io.Serial;3031import javax.accessibility.Accessible;32import javax.accessibility.AccessibleContext;33import javax.accessibility.AccessibleRole;3435/**36* A {@code Canvas} component represents a blank rectangular37* area of the screen onto which the application can draw or from38* which the application can trap input events from the user.39* <p>40* An application must subclass the {@code Canvas} class in41* order to get useful functionality such as creating a custom42* component. The {@code paint} method must be overridden43* in order to perform custom graphics on the canvas.44*45* @author Sami Shaio46* @since 1.047*/48public class Canvas extends Component implements Accessible {4950private static final String base = "canvas";51private static int nameCounter = 0;5253/**54* Use serialVersionUID from JDK 1.1 for interoperability.55*/56@Serial57private static final long serialVersionUID = -2284879212465893870L;5859/**60* Constructs a new Canvas.61*/62public Canvas() {63}6465/**66* Constructs a new Canvas given a GraphicsConfiguration object. If null is67* passed, then the default GraphicsConfiguration will be used.68*69* @param config a reference to a GraphicsConfiguration object or null70*71* @see GraphicsConfiguration72* @see Component#getGraphicsConfiguration()73*/74public Canvas(GraphicsConfiguration config) {75this();76setGraphicsConfiguration(config);77}7879@Override80void setGraphicsConfiguration(GraphicsConfiguration gc) {81synchronized(getTreeLock()) {82CanvasPeer peer = (CanvasPeer) this.peer;83if (peer != null) {84gc = peer.getAppropriateGraphicsConfiguration(gc);85}86super.setGraphicsConfiguration(gc);87}88}8990/**91* Construct a name for this component. Called by getName() when the92* name is null.93*/94String constructComponentName() {95synchronized (Canvas.class) {96return base + nameCounter++;97}98}99100/**101* Creates the peer of the canvas. This peer allows you to change the102* user interface of the canvas without changing its functionality.103* @see java.awt.Component#getToolkit()104*/105public void addNotify() {106synchronized (getTreeLock()) {107if (peer == null)108peer = getComponentFactory().createCanvas(this);109super.addNotify();110}111}112113/**114* Paints this canvas.115* <p>116* Most applications that subclass {@code Canvas} should117* override this method in order to perform some useful operation118* (typically, custom painting of the canvas).119* The default operation is simply to clear the canvas.120* Applications that override this method need not call121* super.paint(g).122*123* @param g the specified Graphics context124* @see #update(Graphics)125* @see Component#paint(Graphics)126*/127public void paint(Graphics g) {128g.clearRect(0, 0, width, height);129}130131/**132* Updates this canvas.133* <p>134* This method is called in response to a call to {@code repaint}.135* The canvas is first cleared by filling it with the background136* color, and then completely redrawn by calling this canvas's137* {@code paint} method.138* Note: applications that override this method should either call139* super.update(g) or incorporate the functionality described140* above into their own code.141*142* @param g the specified Graphics context143* @see #paint(Graphics)144* @see Component#update(Graphics)145*/146public void update(Graphics g) {147g.clearRect(0, 0, width, height);148paint(g);149}150151boolean postsOldMouseEvents() {152return true;153}154155/**156* Creates a new strategy for multi-buffering on this component.157* Multi-buffering is useful for rendering performance. This method158* attempts to create the best strategy available with the number of159* buffers supplied. It will always create a {@code BufferStrategy}160* with that number of buffers.161* A page-flipping strategy is attempted first, then a blitting strategy162* using accelerated buffers. Finally, an unaccelerated blitting163* strategy is used.164* <p>165* Each time this method is called,166* the existing buffer strategy for this component is discarded.167* @param numBuffers number of buffers to create, including the front buffer168* @exception IllegalArgumentException if numBuffers is less than 1.169* @exception IllegalStateException if the component is not displayable170* @see #isDisplayable171* @see #getBufferStrategy172* @since 1.4173*/174public void createBufferStrategy(int numBuffers) {175super.createBufferStrategy(numBuffers);176}177178/**179* Creates a new strategy for multi-buffering on this component with the180* required buffer capabilities. This is useful, for example, if only181* accelerated memory or page flipping is desired (as specified by the182* buffer capabilities).183* <p>184* Each time this method185* is called, the existing buffer strategy for this component is discarded.186* @param numBuffers number of buffers to create187* @param caps the required capabilities for creating the buffer strategy;188* cannot be {@code null}189* @exception AWTException if the capabilities supplied could not be190* supported or met; this may happen, for example, if there is not enough191* accelerated memory currently available, or if page flipping is specified192* but not possible.193* @exception IllegalArgumentException if numBuffers is less than 1, or if194* caps is {@code null}195* @see #getBufferStrategy196* @since 1.4197*/198public void createBufferStrategy(int numBuffers,199BufferCapabilities caps) throws AWTException {200super.createBufferStrategy(numBuffers, caps);201}202203/**204* Returns the {@code BufferStrategy} used by this component. This205* method will return null if a {@code BufferStrategy} has not yet206* been created or has been disposed.207*208* @return the buffer strategy used by this component209* @see #createBufferStrategy210* @since 1.4211*/212public BufferStrategy getBufferStrategy() {213return super.getBufferStrategy();214}215216/*217* --- Accessibility Support ---218*219*/220221/**222* Gets the AccessibleContext associated with this Canvas.223* For canvases, the AccessibleContext takes the form of an224* AccessibleAWTCanvas.225* A new AccessibleAWTCanvas instance is created if necessary.226*227* @return an AccessibleAWTCanvas that serves as the228* AccessibleContext of this Canvas229* @since 1.3230*/231public AccessibleContext getAccessibleContext() {232if (accessibleContext == null) {233accessibleContext = new AccessibleAWTCanvas();234}235return accessibleContext;236}237238/**239* This class implements accessibility support for the240* {@code Canvas} class. It provides an implementation of the241* Java Accessibility API appropriate to canvas user-interface elements.242* @since 1.3243*/244protected class AccessibleAWTCanvas extends AccessibleAWTComponent245{246/**247* Use serialVersionUID from JDK 1.3 for interoperability.248*/249@Serial250private static final long serialVersionUID = -6325592262103146699L;251252/**253* Constructs an {@code AccessibleAWTCanvas}.254*/255protected AccessibleAWTCanvas() {}256257/**258* Get the role of this object.259*260* @return an instance of AccessibleRole describing the role of the261* object262* @see AccessibleRole263*/264public AccessibleRole getAccessibleRole() {265return AccessibleRole.CANVAS;266}267268} // inner class AccessibleAWTCanvas269}270271272