Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/Canvas.java
38829 views
/*1* Copyright (c) 1995, 2010, 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*/24package java.awt;2526import java.awt.image.BufferStrategy;27import java.awt.peer.CanvasPeer;28import javax.accessibility.*;2930/**31* A <code>Canvas</code> component represents a blank rectangular32* area of the screen onto which the application can draw or from33* which the application can trap input events from the user.34* <p>35* An application must subclass the <code>Canvas</code> class in36* order to get useful functionality such as creating a custom37* component. The <code>paint</code> method must be overridden38* in order to perform custom graphics on the canvas.39*40* @author Sami Shaio41* @since JDK1.042*/43public class Canvas extends Component implements Accessible {4445private static final String base = "canvas";46private static int nameCounter = 0;4748/*49* JDK 1.1 serialVersionUID50*/51private static final long serialVersionUID = -2284879212465893870L;5253/**54* Constructs a new Canvas.55*/56public Canvas() {57}5859/**60* Constructs a new Canvas given a GraphicsConfiguration object.61*62* @param config a reference to a GraphicsConfiguration object.63*64* @see GraphicsConfiguration65*/66public Canvas(GraphicsConfiguration config) {67this();68setGraphicsConfiguration(config);69}7071@Override72void setGraphicsConfiguration(GraphicsConfiguration gc) {73synchronized(getTreeLock()) {74CanvasPeer peer = (CanvasPeer)getPeer();75if (peer != null) {76gc = peer.getAppropriateGraphicsConfiguration(gc);77}78super.setGraphicsConfiguration(gc);79}80}8182/**83* Construct a name for this component. Called by getName() when the84* name is null.85*/86String constructComponentName() {87synchronized (Canvas.class) {88return base + nameCounter++;89}90}9192/**93* Creates the peer of the canvas. This peer allows you to change the94* user interface of the canvas without changing its functionality.95* @see java.awt.Toolkit#createCanvas(java.awt.Canvas)96* @see java.awt.Component#getToolkit()97*/98public void addNotify() {99synchronized (getTreeLock()) {100if (peer == null)101peer = getToolkit().createCanvas(this);102super.addNotify();103}104}105106/**107* Paints this canvas.108* <p>109* Most applications that subclass <code>Canvas</code> should110* override this method in order to perform some useful operation111* (typically, custom painting of the canvas).112* The default operation is simply to clear the canvas.113* Applications that override this method need not call114* super.paint(g).115*116* @param g the specified Graphics context117* @see #update(Graphics)118* @see Component#paint(Graphics)119*/120public void paint(Graphics g) {121g.clearRect(0, 0, width, height);122}123124/**125* Updates this canvas.126* <p>127* This method is called in response to a call to <code>repaint</code>.128* The canvas is first cleared by filling it with the background129* color, and then completely redrawn by calling this canvas's130* <code>paint</code> method.131* Note: applications that override this method should either call132* super.update(g) or incorporate the functionality described133* above into their own code.134*135* @param g the specified Graphics context136* @see #paint(Graphics)137* @see Component#update(Graphics)138*/139public void update(Graphics g) {140g.clearRect(0, 0, width, height);141paint(g);142}143144boolean postsOldMouseEvents() {145return true;146}147148/**149* Creates a new strategy for multi-buffering on this component.150* Multi-buffering is useful for rendering performance. This method151* attempts to create the best strategy available with the number of152* buffers supplied. It will always create a <code>BufferStrategy</code>153* with that number of buffers.154* A page-flipping strategy is attempted first, then a blitting strategy155* using accelerated buffers. Finally, an unaccelerated blitting156* strategy is used.157* <p>158* Each time this method is called,159* the existing buffer strategy for this component is discarded.160* @param numBuffers number of buffers to create, including the front buffer161* @exception IllegalArgumentException if numBuffers is less than 1.162* @exception IllegalStateException if the component is not displayable163* @see #isDisplayable164* @see #getBufferStrategy165* @since 1.4166*/167public void createBufferStrategy(int numBuffers) {168super.createBufferStrategy(numBuffers);169}170171/**172* Creates a new strategy for multi-buffering on this component with the173* required buffer capabilities. This is useful, for example, if only174* accelerated memory or page flipping is desired (as specified by the175* buffer capabilities).176* <p>177* Each time this method178* is called, the existing buffer strategy for this component is discarded.179* @param numBuffers number of buffers to create180* @param caps the required capabilities for creating the buffer strategy;181* cannot be <code>null</code>182* @exception AWTException if the capabilities supplied could not be183* supported or met; this may happen, for example, if there is not enough184* accelerated memory currently available, or if page flipping is specified185* but not possible.186* @exception IllegalArgumentException if numBuffers is less than 1, or if187* caps is <code>null</code>188* @see #getBufferStrategy189* @since 1.4190*/191public void createBufferStrategy(int numBuffers,192BufferCapabilities caps) throws AWTException {193super.createBufferStrategy(numBuffers, caps);194}195196/**197* Returns the <code>BufferStrategy</code> used by this component. This198* method will return null if a <code>BufferStrategy</code> has not yet199* been created or has been disposed.200*201* @return the buffer strategy used by this component202* @see #createBufferStrategy203* @since 1.4204*/205public BufferStrategy getBufferStrategy() {206return super.getBufferStrategy();207}208209/*210* --- Accessibility Support ---211*212*/213214/**215* Gets the AccessibleContext associated with this Canvas.216* For canvases, the AccessibleContext takes the form of an217* AccessibleAWTCanvas.218* A new AccessibleAWTCanvas instance is created if necessary.219*220* @return an AccessibleAWTCanvas that serves as the221* AccessibleContext of this Canvas222* @since 1.3223*/224public AccessibleContext getAccessibleContext() {225if (accessibleContext == null) {226accessibleContext = new AccessibleAWTCanvas();227}228return accessibleContext;229}230231/**232* This class implements accessibility support for the233* <code>Canvas</code> class. It provides an implementation of the234* Java Accessibility API appropriate to canvas user-interface elements.235* @since 1.3236*/237protected class AccessibleAWTCanvas extends AccessibleAWTComponent238{239private static final long serialVersionUID = -6325592262103146699L;240241/**242* Get the role of this object.243*244* @return an instance of AccessibleRole describing the role of the245* object246* @see AccessibleRole247*/248public AccessibleRole getAccessibleRole() {249return AccessibleRole.CANVAS;250}251252} // inner class AccessibleAWTCanvas253}254255256