APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public Class Canvas

extends Component
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Known Direct Subclasses
sun.awt.X11.Separator, sun.awt.X11.XTrayIconPeer.IconCanvas
Imports
java.awt.image.BufferStrategy, java.awt.peer.CanvasPeer, java.io.Serial, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole

A Canvas component represents a blank rectangular area of the screen onto which the application can draw or from which the application can trap input events from the user.

An application must subclass the Canvas class in order to get useful functionality such as creating a custom component. The paint method must be overridden in order to perform custom graphics on the canvas.

Author
Sami Shaio
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
Canvas.AccessibleAWTCanvas

This class implements accessibility support for the Canvas class.

Field Summary

Modifier and TypeField and Description
private static final String
private static int
private static final long
serialVersionUID

Hides java.awt.Component.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.
Inherited from java.awt.Component:
accessibleContextactionListenerKadjustmentListenerKbackgroundbackgroundEraseDisabledBOTTOM_ALIGNMENTCENTER_ALIGNMENTcomponentListenercomponentListenerKcomponentOrientationcontainerListenerKcursordropTargetenabledeventCacheeventMaskfocusListenerfocusListenerKfocusTraversalKeysfontforegroundheighthierarchyBoundsListenerhierarchyBoundsListenerKhierarchyListenerhierarchyListenerKignoreRepaintincRateinputMethodListenerinputMethodListenerKisIncisPackeditemListenerKkeyListenerkeyListenerKLEFT_ALIGNMENTlocaleLOCKmaxSizemaxSizeSetminSizeminSizeSetmouseListenermouseListenerKmouseMotionListenermouseMotionListenerKmouseWheelListenermouseWheelListenerKnewEventsOnlyownedWindowKparentpeerpeerFontpopupsprefSizeprefSizeSetRIGHT_ALIGNMENTtextListenerKTOP_ALIGNMENTvisiblewidthwindowFocusListenerKwindowListenerKwindowStateListenerKxy

Constructor Summary

AccessConstructor and Description
public
Canvas()

Constructs a new Canvas.

public
Canvas(GraphicsConfiguration
a reference to a GraphicsConfiguration object or null
config
)

Constructs a new Canvas given a GraphicsConfiguration object.

Method Summary

Modifier and TypeMethod and Description
public void
addNotify()

Overrides java.awt.Component.addNotify.

Creates the peer of the canvas.

pack-priv String
constructComponentName()

Overrides java.awt.Component.constructComponentName.

Construct a name for this component.

public void
createBufferStrategy(int
number of buffers to create, including the front buffer
numBuffers
)

Overrides java.awt.Component.createBufferStrategy.

Creates a new strategy for multi-buffering on this component.

public void
createBufferStrategy(int
number of buffers to create
numBuffers
,
BufferCapabilities
the required capabilities for creating the buffer strategy; cannot be null
caps
)

Overrides java.awt.Component.createBufferStrategy.

Creates a new strategy for multi-buffering on this component with the required buffer capabilities.

public AccessibleContext

Returns:

an AccessibleAWTCanvas that serves as the AccessibleContext of this Canvas
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this Canvas.

public BufferStrategy

Returns:

the buffer strategy used by this component
getBufferStrategy
()

Overrides java.awt.Component.getBufferStrategy.

Returns the BufferStrategy used by this component.

public void
paint(Graphics
the specified Graphics context
g
)

Overrides java.awt.Component.paint.

Paints this canvas.

pack-priv boolean
pack-priv void
public void
update(Graphics
the specified Graphics context
g
)

Overrides java.awt.Component.update.

Updates this canvas.

Inherited from java.awt.Component:
actionaddaddComponentListeneraddFocusListeneraddHierarchyBoundsListeneraddHierarchyListeneraddInputMethodListeneraddKeyListeneraddMouseListeneraddMouseMotionListeneraddMouseWheelListeneraddPropertyChangeListeneraddPropertyChangeListeneradjustListeningChildrenOnParentapplyComponentOrientationapplyCompoundShapeapplyCurrentShapeareBoundsValidareFocusTraversalKeysSetareInputMethodsEnabledautoProcessMouseWheelboundscanBeFocusOwnercanBeFocusOwnerRecursivelycheckGDcheckImagecheckImagecheckTreeLockclearCurrentFocusCycleRootOnHideclearMostRecentFocusOwnerOnHidecoalesceEventscontainscontainscontainsFocuscountHierarchyMemberscreateHierarchyEventscreateImagecreateImagecreateVolatileImagecreateVolatileImagedeliverEventdisabledisableEventsdispatchEventdispatchEventImpldispatchMouseWheelToAncestordoLayoutenableenableenableEventsenableInputMethodseventEnabledeventTypeEnabledfindUnderMouseInWindowfirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangegetAccessibleIndexInParentgetAccessibleStateSetgetAlignmentXgetAlignmentYgetBackBuffergetBackgroundgetBaselinegetBaselineResizeBehaviorgetBoundsgetBoundsgetBoundsOpgetColorModelgetComponentAtgetComponentAtgetComponentFactorygetComponentListenersgetComponentOrientationgetContainergetContainingWindowgetCursorgetCursor_NoClientCodegetDropTargetgetFocusCycleRootAncestorgetFocusListenersgetFocusTraversalKeysgetFocusTraversalKeys_NoIDCheckgetFocusTraversalKeysEnabledgetFontgetFont_NoClientCodegetFontMetricsgetForegroundgetGraphicsgetGraphics_NoClientCodegetGraphicsConfigurationgetGraphicsConfiguration_NoClientCodegetHeightgetHierarchyBoundsListenersgetHierarchyListenersgetHWPeerAboveMegetIgnoreRepaintgetInputContextgetInputMethodListenersgetInputMethodRequestsgetKeyListenersgetListenersgetLocalegetLocationgetLocationgetLocationOnScreengetLocationOnScreen_NoTreeLockgetLocationOnWindowgetMaximumSizegetMinimumSizegetMouseListenersgetMouseMotionListenersgetMousePositiongetMouseWheelListenersgetNamegetNativeContainergetNextFocusCandidategetNormalShapegetObjectLockgetOpaqueShapegetParentgetParent_NoClientCodegetPreferredSizegetPropertyChangeListenersgetPropertyChangeListenersgetSiblingIndexAbovegetSiblingIndexBelowgetSizegetSizegetToolkitgetToolkitImplgetTraversalRootgetTreeLockgetWidthgetXgetYgotFocushandleEventhasFocushideimageUpdateinitializeFocusTraversalKeysinsideinvalidateinvalidateIfValidinvalidateParentisAutoFocusTransferOnDisposalisBackgroundSetisCoalescingEnabledisCursorSetisDisplayableisDoubleBufferedisEnabledisEnabledImplisFocusableisFocusCycleRootisFocusOwnerisFocusTraversableisFocusTraversableOverriddenisFontSetisForegroundSetisInstanceOfisLightweightisMaximumSizeSetisMinimumSizeSetisMixingNeededisNonOpaqueForMixingisOpaqueisPreferredSizeSetisRecursivelyVisibleisSameOrAncestorOfisShowingisValidisVisibleisVisible_NoClientCodekeyDownkeyUplayoutlightweightPaintlightweightPrintlistlistlistlistlistlocatelocationlostFocusminimumSizemixOnHidingmixOnReshapingmixOnShowingmixOnValidatingmixOnZOrderChangingmouseDownmouseDragmouseEntermouseExitmouseMovemouseUpmovenextFocusnumListeningpaintAllpaintHeavyweightComponentsparamStringpointRelativeToComponentpostEventpreferredSizeprepareImageprepareImageprintprintAllprintHeavyweightComponentsprocessComponentEventprocessEventprocessFocusEventprocessHierarchyBoundsEventprocessHierarchyEventprocessInputMethodEventprocessKeyEventprocessMouseEventprocessMouseMotionEventprocessMouseWheelEventrelocateComponentremoveremoveComponentListenerremoveFocusListenerremoveHierarchyBoundsListenerremoveHierarchyListenerremoveInputMethodListenerremoveKeyListenerremoveMouseListenerremoveMouseMotionListenerremoveMouseWheelListenerremoveNotifyremovePropertyChangeListenerremovePropertyChangeListenerrepaintrepaintrepaintrepaintrequestFocusrequestFocusrequestFocusrequestFocusrequestFocusHelperrequestFocusHelperrequestFocusInWindowrequestFocusInWindowrequestFocusInWindowrequestFocusInWindowreshaperesizeresizerevalidaterevalidateSynchronouslysetAutoFocusTransferOnDisposalsetBackgroundsetBoundssetBoundssetBoundsOpsetComponentOrientationsetCursorsetDropTargetsetEnabledsetFocusablesetFocusTraversalKeyssetFocusTraversalKeys_NoIDChecksetFocusTraversalKeysEnabledsetFontsetForegroundsetIgnoreRepaintsetLocalesetLocationsetLocationsetMaximumSizesetMinimumSizesetMixingCutoutShapesetNamesetPreferredSizesetRequestFocusControllersetSizesetSizesetVisibleshowshowsizesubtractAndApplyShapesubtractAndApplyShapeBelowMetoStringtransferFocustransferFocustransferFocusBackwardtransferFocusBackwardtransferFocusUpCycleupdateChildGraphicsDataupdateCursorImmediatelyupdateGraphicsDataupdateZOrdervalidate

Field Detail

baseback to summary
private static final String base
nameCounterback to summary
private static int nameCounter
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.Component.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.

Annotations
@Serial

Constructor Detail

Canvasback to summary
public Canvas()

Constructs a new Canvas.

Canvasback to summary
public Canvas(GraphicsConfiguration config)

Constructs a new Canvas given a GraphicsConfiguration object. If null is passed, then the default GraphicsConfiguration will be used.

Parameters
config:GraphicsConfiguration

a reference to a GraphicsConfiguration object or null

See Also
GraphicsConfiguration, Component#getGraphicsConfiguration()

Method Detail

addNotifyback to summary
public void addNotify()

Overrides java.awt.Component.addNotify.

Creates the peer of the canvas. This peer allows you to change the user interface of the canvas without changing its functionality.

See Also
java.awt.Component#getToolkit()
constructComponentNameback to summary
pack-priv String constructComponentName()

Overrides java.awt.Component.constructComponentName.

Construct a name for this component. Called by getName() when the name is null.

createBufferStrategyback to summary
public void createBufferStrategy(int numBuffers)

Overrides java.awt.Component.createBufferStrategy.

Creates a new strategy for multi-buffering on this component. Multi-buffering is useful for rendering performance. This method attempts to create the best strategy available with the number of buffers supplied. It will always create a BufferStrategy with that number of buffers. A page-flipping strategy is attempted first, then a blitting strategy using accelerated buffers. Finally, an unaccelerated blitting strategy is used.

Each time this method is called, the existing buffer strategy for this component is discarded.

Parameters
numBuffers:int

number of buffers to create, including the front buffer

Exceptions
IllegalArgumentException:
if numBuffers is less than 1.
IllegalStateException:
if the component is not displayable
Since
1.4
See Also
isDisplayable, getBufferStrategy
createBufferStrategyback to summary
public void createBufferStrategy(int numBuffers, BufferCapabilities caps) throws AWTException

Overrides java.awt.Component.createBufferStrategy.

Creates a new strategy for multi-buffering on this component with the required buffer capabilities. This is useful, for example, if only accelerated memory or page flipping is desired (as specified by the buffer capabilities).

Each time this method is called, the existing buffer strategy for this component is discarded.

Parameters
numBuffers:int

number of buffers to create

caps:BufferCapabilities

the required capabilities for creating the buffer strategy; cannot be null

Exceptions
AWTException:
if the capabilities supplied could not be supported or met; this may happen, for example, if there is not enough accelerated memory currently available, or if page flipping is specified but not possible.
IllegalArgumentException:
if numBuffers is less than 1, or if caps is null
Since
1.4
See Also
getBufferStrategy
getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this Canvas. For canvases, the AccessibleContext takes the form of an AccessibleAWTCanvas. A new AccessibleAWTCanvas instance is created if necessary.

Returns:AccessibleContext

an AccessibleAWTCanvas that serves as the AccessibleContext of this Canvas

Since
1.3
getBufferStrategyback to summary
public BufferStrategy getBufferStrategy()

Overrides java.awt.Component.getBufferStrategy.

Returns the BufferStrategy used by this component. This method will return null if a BufferStrategy has not yet been created or has been disposed.

Returns:BufferStrategy

the buffer strategy used by this component

Since
1.4
See Also
createBufferStrategy
paintback to summary
public void paint(Graphics g)

Overrides java.awt.Component.paint.

Paints this canvas.

Most applications that subclass Canvas should override this method in order to perform some useful operation (typically, custom painting of the canvas). The default operation is simply to clear the canvas. Applications that override this method need not call super.paint(g).

Parameters
g:Graphics

the specified Graphics context

See Also
update(Graphics), Component#paint(Graphics)
postsOldMouseEventsback to summary
pack-priv boolean postsOldMouseEvents()

Overrides java.awt.Component.postsOldMouseEvents.

setGraphicsConfigurationback to summary
pack-priv void setGraphicsConfiguration(GraphicsConfiguration gc)

Overrides java.awt.Component.setGraphicsConfiguration.

Annotations
@Override
updateback to summary
public void update(Graphics g)

Overrides java.awt.Component.update.

Updates this canvas.

This method is called in response to a call to repaint. The canvas is first cleared by filling it with the background color, and then completely redrawn by calling this canvas's paint method.

Note

applications that override this method should either call super.update(g) or incorporate the functionality described above into their own code.

Parameters
g:Graphics

the specified Graphics context

See Also
paint(Graphics), Component#update(Graphics)
java.awt back to summary

protected Class Canvas.AccessibleAWTCanvas

extends AccessibleAWTComponent
Class Inheritance

This class implements accessibility support for the Canvas class. It provides an implementation of the Java Accessibility API appropriate to canvas user-interface elements.
Since
1.3

Field Summary

Modifier and TypeField and Description
private static final long
serialVersionUID

Hides java.awt.Component.AccessibleAWTComponent.serialVersionUID.

Use serialVersionUID from JDK 1.3 for interoperability.
Inherited from java.awt.Component.AccessibleAWTComponent:
accessibleAWTComponentHandleraccessibleAWTFocusHandler

Constructor Summary

AccessConstructor and Description
protected
AccessibleAWTCanvas()

Constructs an AccessibleAWTCanvas.

Method Summary

Modifier and TypeMethod and Description
public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object
getAccessibleRole
()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleRole.

Get the role of this object.

Inherited from java.awt.Component.AccessibleAWTComponent:
addFocusListeneraddPropertyChangeListenercontainsgetAccessibleAtgetAccessibleChildgetAccessibleChildrenCountgetAccessibleComponentgetAccessibleDescriptiongetAccessibleIndexInParentgetAccessibleNamegetAccessibleParentgetAccessibleStateSetgetBackgroundgetBoundsgetCursorgetFontgetFontMetricsgetForegroundgetLocalegetLocationgetLocationOnScreengetSizeisEnabledisFocusTraversableisShowingisVisibleremoveFocusListenerremovePropertyChangeListenerrequestFocussetBackgroundsetBoundssetCursorsetEnabledsetFontsetForegroundsetLocationsetSizesetVisible

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.Component.AccessibleAWTComponent.serialVersionUID.

Use serialVersionUID from JDK 1.3 for interoperability.

Annotations
@Serial

Constructor Detail

AccessibleAWTCanvasback to summary
protected AccessibleAWTCanvas()

Constructs an AccessibleAWTCanvas.

Method Detail

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleRole.

Get the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object

See Also
AccessibleRole