1. /*
  2. * @(#)Canvas.java 1.28 00/03/15
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. import java.awt.peer.CanvasPeer;
  12. import javax.accessibility.*;
  13. /**
  14. * A <code>Canvas</code> component represents a blank rectangular
  15. * area of the screen onto which the application can draw or from
  16. * which the application can trap input events from the user.
  17. * <p>
  18. * An application must subclass the <code>Canvas</code> class in
  19. * order to get useful functionality such as creating a custom
  20. * component. The <code>paint</code> method must be overridden
  21. * in order to perform custom graphics on the canvas.
  22. *
  23. * @version 1.28 03/15/00
  24. * @author Sami Shaio
  25. * @since JDK1.0
  26. */
  27. public class Canvas extends Component implements Accessible {
  28. private static final String base = "canvas";
  29. private static int nameCounter = 0;
  30. /*
  31. * JDK 1.1 serialVersionUID
  32. */
  33. private static final long serialVersionUID = -2284879212465893870L;
  34. /**
  35. * Constructs a new Canvas.
  36. */
  37. public Canvas() {
  38. }
  39. /**
  40. * Constructs a new Canvas given a GraphicsConfiguration object.
  41. *
  42. * @param config a reference to a GraphicsConfiguration object.
  43. *
  44. * @see GraphicsConfiguration
  45. */
  46. public Canvas(GraphicsConfiguration config) {
  47. this();
  48. graphicsConfig = config;
  49. }
  50. /**
  51. * Construct a name for this component. Called by getName() when the
  52. * name is null.
  53. */
  54. String constructComponentName() {
  55. synchronized (getClass()) {
  56. return base + nameCounter++;
  57. }
  58. }
  59. /**
  60. * Creates the peer of the canvas. This peer allows you to change the
  61. * user interface of the canvas without changing its functionality.
  62. * @see java.awt.Toolkit#createCanvas(java.awt.Canvas)
  63. * @see java.awt.Component#getToolkit()
  64. */
  65. public void addNotify() {
  66. synchronized (getTreeLock()) {
  67. if (peer == null)
  68. peer = getToolkit().createCanvas(this);
  69. super.addNotify();
  70. }
  71. }
  72. /**
  73. * This method is called to repaint this canvas. Most applications
  74. * that subclass <code>Canvas</code> should override this method in
  75. * order to perform some useful operation.
  76. * <p>
  77. * The <code>paint</code> method provided by <code>Canvas</code>
  78. * redraws this canvas's rectangle in the background color.
  79. * <p>
  80. * The graphics context's origin (0, 0) is the top-left corner
  81. * of this canvas. Its clipping region is the area of the context.
  82. * @param g the graphics context.
  83. * @see java.awt.Graphics
  84. */
  85. public void paint(Graphics g) {
  86. g.setColor(getBackground());
  87. g.fillRect(0, 0, width, height);
  88. }
  89. boolean postsOldMouseEvents() {
  90. return true;
  91. }
  92. /*
  93. * --- Accessibility Support ---
  94. *
  95. */
  96. /**
  97. * Gets the AccessibleContext associated with this Canvas.
  98. * For canvases, the AccessibleContext takes the form of an
  99. * AccessibleAWTCanvas.
  100. * A new AccessibleAWTCanvas instance is created if necessary.
  101. *
  102. * @return an AccessibleAWTCanvas that serves as the
  103. * AccessibleContext of this Canvas
  104. */
  105. public AccessibleContext getAccessibleContext() {
  106. if (accessibleContext == null) {
  107. accessibleContext = new AccessibleAWTCanvas();
  108. }
  109. return accessibleContext;
  110. }
  111. /**
  112. * This class implements accessibility support for the
  113. * <code>Canvas</code> class. It provides an implementation of the
  114. * Java Accessibility API appropriate to canvas user-interface elements.
  115. */
  116. protected class AccessibleAWTCanvas extends AccessibleAWTComponent {
  117. /**
  118. * Get the role of this object.
  119. *
  120. * @return an instance of AccessibleRole describing the role of the
  121. * object
  122. * @see AccessibleRole
  123. */
  124. public AccessibleRole getAccessibleRole() {
  125. return AccessibleRole.CANVAS;
  126. }
  127. } // inner class AccessibleAWTCanvas
  128. }