1. /*
  2. * @(#)Canvas.java 1.33 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.image.BufferStrategy;
  9. import java.awt.peer.CanvasPeer;
  10. import javax.accessibility.*;
  11. /**
  12. * A <code>Canvas</code> component represents a blank rectangular
  13. * area of the screen onto which the application can draw or from
  14. * which the application can trap input events from the user.
  15. * <p>
  16. * An application must subclass the <code>Canvas</code> class in
  17. * order to get useful functionality such as creating a custom
  18. * component. The <code>paint</code> method must be overridden
  19. * in order to perform custom graphics on the canvas.
  20. *
  21. * @version 1.33 01/23/03
  22. * @author Sami Shaio
  23. * @since JDK1.0
  24. */
  25. public class Canvas extends Component implements Accessible {
  26. private static final String base = "canvas";
  27. private static int nameCounter = 0;
  28. /*
  29. * JDK 1.1 serialVersionUID
  30. */
  31. private static final long serialVersionUID = -2284879212465893870L;
  32. /**
  33. * Constructs a new Canvas.
  34. */
  35. public Canvas() {
  36. }
  37. /**
  38. * Constructs a new Canvas given a GraphicsConfiguration object.
  39. *
  40. * @param config a reference to a GraphicsConfiguration object.
  41. *
  42. * @see GraphicsConfiguration
  43. */
  44. public Canvas(GraphicsConfiguration config) {
  45. this();
  46. graphicsConfig = config;
  47. }
  48. /**
  49. * Construct a name for this component. Called by getName() when the
  50. * name is null.
  51. */
  52. String constructComponentName() {
  53. synchronized (getClass()) {
  54. return base + nameCounter++;
  55. }
  56. }
  57. /**
  58. * Creates the peer of the canvas. This peer allows you to change the
  59. * user interface of the canvas without changing its functionality.
  60. * @see java.awt.Toolkit#createCanvas(java.awt.Canvas)
  61. * @see java.awt.Component#getToolkit()
  62. */
  63. public void addNotify() {
  64. synchronized (getTreeLock()) {
  65. if (peer == null)
  66. peer = getToolkit().createCanvas(this);
  67. super.addNotify();
  68. }
  69. }
  70. /**
  71. * Paints this canvas.
  72. * <p>
  73. * Most applications that subclass <code>Canvas</code> should
  74. * override this method in order to perform some useful operation
  75. * (typically, custom painting of the canvas).
  76. * The default operation is simply to clear the canvas.
  77. * Applications that override this method need not call
  78. * super.paint(g).
  79. *
  80. * @param g the specified Graphics context
  81. * @see #update(Graphics)
  82. * @see Component#paint(Graphics)
  83. */
  84. public void paint(Graphics g) {
  85. g.clearRect(0, 0, width, height);
  86. }
  87. /**
  88. * Updates this canvas.
  89. * <p>
  90. * This method is called in response to a call to <code>repaint</code>.
  91. * The canvas is first cleared by filling it with the background
  92. * color, and then completely redrawn by calling this canvas's
  93. * <code>paint</code> method.
  94. * Note: applications that override this method should either call
  95. * super.update(g) or incorporate the functionality described
  96. * above into their own code.
  97. *
  98. * @param g the specified Graphics context
  99. * @see #paint(Graphics)
  100. * @see Component#update(Graphics)
  101. */
  102. public void update(Graphics g) {
  103. g.clearRect(0, 0, width, height);
  104. paint(g);
  105. }
  106. boolean postsOldMouseEvents() {
  107. return true;
  108. }
  109. /**
  110. * Creates a new strategy for multi-buffering on this component.
  111. * Multi-buffering is useful for rendering performance. This method
  112. * attempts to create the best strategy available with the number of
  113. * buffers supplied. It will always create a <code>BufferStrategy</code>
  114. * with that number of buffers.
  115. * A page-flipping strategy is attempted first, then a blitting strategy
  116. * using accelerated buffers. Finally, an unaccelerated blitting
  117. * strategy is used.
  118. * <p>
  119. * Each time this method is called,
  120. * the existing buffer strategy for this component is discarded.
  121. * @param numBuffers number of buffers to create, including the front buffer
  122. * @exception IllegalArgumentException if numBuffers is less than 1.
  123. * @exception IllegalStateException if the component is not displayable
  124. * @see #isDisplayable
  125. * @see #getBufferStrategy
  126. * @since 1.4
  127. */
  128. public void createBufferStrategy(int numBuffers) {
  129. super.createBufferStrategy(numBuffers);
  130. }
  131. /**
  132. * Creates a new strategy for multi-buffering on this component with the
  133. * required buffer capabilities. This is useful, for example, if only
  134. * accelerated memory or page flipping is desired (as specified by the
  135. * buffer capabilities).
  136. * <p>
  137. * Each time this method
  138. * is called, the existing buffer strategy for this component is discarded.
  139. * @param numBuffers number of buffers to create
  140. * @param caps the required capabilities for creating the buffer strategy;
  141. * cannot be <code>null</code>
  142. * @exception AWTException if the capabilities supplied could not be
  143. * supported or met; this may happen, for example, if there is not enough
  144. * accelerated memory currently available, or if page flipping is specified
  145. * but not possible.
  146. * @exception IllegalArgumentException if numBuffers is less than 1, or if
  147. * caps is <code>null</code>
  148. * @see #getBufferStrategy
  149. * @since 1.4
  150. */
  151. public void createBufferStrategy(int numBuffers,
  152. BufferCapabilities caps) throws AWTException {
  153. super.createBufferStrategy(numBuffers, caps);
  154. }
  155. /**
  156. * @return the buffer strategy used by this component
  157. * @see #createBufferStrategy
  158. * @since 1.4
  159. */
  160. public BufferStrategy getBufferStrategy() {
  161. return super.getBufferStrategy();
  162. }
  163. /*
  164. * --- Accessibility Support ---
  165. *
  166. */
  167. /**
  168. * Gets the AccessibleContext associated with this Canvas.
  169. * For canvases, the AccessibleContext takes the form of an
  170. * AccessibleAWTCanvas.
  171. * A new AccessibleAWTCanvas instance is created if necessary.
  172. *
  173. * @return an AccessibleAWTCanvas that serves as the
  174. * AccessibleContext of this Canvas
  175. */
  176. public AccessibleContext getAccessibleContext() {
  177. if (accessibleContext == null) {
  178. accessibleContext = new AccessibleAWTCanvas();
  179. }
  180. return accessibleContext;
  181. }
  182. /**
  183. * This class implements accessibility support for the
  184. * <code>Canvas</code> class. It provides an implementation of the
  185. * Java Accessibility API appropriate to canvas user-interface elements.
  186. */
  187. protected class AccessibleAWTCanvas extends AccessibleAWTComponent {
  188. /**
  189. * Get the role of this object.
  190. *
  191. * @return an instance of AccessibleRole describing the role of the
  192. * object
  193. * @see AccessibleRole
  194. */
  195. public AccessibleRole getAccessibleRole() {
  196. return AccessibleRole.CANVAS;
  197. }
  198. } // inner class AccessibleAWTCanvas
  199. }