java.awt

Class Canvas

public class Canvas extends Component implements Serializable, Accessible

The Canvas component provides a blank rectangular area, which the client application can use for drawing and for capturing events. By overriding the paint() method, the canvas can be used for anything from simple line drawings to full-scale custom components.

Since: 1.0

Nested Class Summary
protected classCanvas.AccessibleAWTCanvas
This class provides accessibility support for the canvas.
Constructor Summary
Canvas()
Initializes a new instance of Canvas.
Canvas(GraphicsConfiguration graphicsConfiguration)
Initializes a new instance of Canvas with the supplied graphics configuration.
Method Summary
voidaddNotify()
Creates the native peer for this object.
voidcreateBufferStrategy(int numBuffers)
Creates a buffering strategy that manages how this canvas is repainted.
voidcreateBufferStrategy(int numBuffers, BufferCapabilities caps)
Creates a buffering strategy that manages how this canvas is repainted.
AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this Canvas.
BufferStrategygetBufferStrategy()
Returns the buffer strategy used by the canvas.
voidpaint(Graphics gfx)
Repaints the canvas window.
voidupdate(Graphics graphics)
Updates the canvas in response to a request to repaint() it.

Constructor Detail

Canvas

public Canvas()
Initializes a new instance of Canvas.

Canvas

public Canvas(GraphicsConfiguration graphicsConfiguration)
Initializes a new instance of Canvas with the supplied graphics configuration.

Parameters: graphicsConfiguration the graphics configuration to use for this particular canvas.

Method Detail

addNotify

public void addNotify()
Creates the native peer for this object.

createBufferStrategy

public void createBufferStrategy(int numBuffers)
Creates a buffering strategy that manages how this canvas is repainted. This method attempts to create the optimum strategy based on the desired number of buffers. Hardware or software acceleration may be used. createBufferStrategy attempts different levels of optimization, but guarantees that some strategy with the requested number of buffers will be created even if it is not optimal. First it attempts to create a page flipping strategy, then an accelerated blitting strategy, then an unaccelerated blitting strategy. Calling this method causes any existing buffer strategy to be destroyed.

Parameters: numBuffers the number of buffers in this strategy

Throws: IllegalArgumentException if requested number of buffers is less than one IllegalStateException if this canvas is not displayable

Since: 1.4

createBufferStrategy

public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
Creates a buffering strategy that manages how this canvas is repainted. This method attempts to create a strategy based on the specified capabilities and throws an exception if the requested strategy is not supported. Calling this method causes any existing buffer strategy to be destroyed.

Parameters: numBuffers the number of buffers in this strategy caps the requested buffering capabilities

Throws: AWTException if the requested capabilities are not supported IllegalArgumentException if requested number of buffers is less than one or if caps is null

Since: 1.4

getAccessibleContext

public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Canvas. The context is created, if necessary.

Returns: the associated context

getBufferStrategy

public BufferStrategy getBufferStrategy()
Returns the buffer strategy used by the canvas.

Returns: the buffer strategy.

Since: 1.4

paint

public void paint(Graphics gfx)
Repaints the canvas window. This method should be overridden by a subclass to do something useful, as this method simply paints the window with the background color.

Parameters: gfx the Graphics to use for painting

update

public void update(Graphics graphics)
Updates the canvas in response to a request to repaint() it. The canvas is cleared with the current background colour, before paint() is called to add the new contents. Subclasses which override this method should either call this method via super.update(graphics) or re-implement this behaviour, so as to ensure that the canvas is clear before painting takes place.

Parameters: graphics the graphics context.