1. /*
  2. * @(#)Canvas.java 1.22 01/11/29
  3. *
  4. * Copyright 2002 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.peer.CanvasPeer;
  9. /**
  10. * A <code>Canvas</code> component represents a blank rectangular
  11. * area of the screen onto which the application can draw or from
  12. * which the application can trap input events from the user.
  13. * <p>
  14. * An application must subclass the <code>Canvas</code> class in
  15. * order to get useful functionality such as creating a custom
  16. * component. The <code>paint</code> method must be overridden
  17. * in order to perform custom graphics on the canvas.
  18. *
  19. * @version 1.22 11/29/01
  20. * @author Sami Shaio
  21. * @since JDK1.0
  22. */
  23. public class Canvas extends Component {
  24. private static final String base = "canvas";
  25. private static int nameCounter = 0;
  26. /*
  27. * A reference to a GraphicsConfiguration object
  28. * used to describe the characteristics of a graphics
  29. * destination.
  30. *
  31. * @serial
  32. * @see java.awt.GraphicsConfiguration
  33. */
  34. private GraphicsConfiguration graphicsConfig = null;
  35. /*
  36. * JDK 1.1 serialVersionUID
  37. */
  38. private static final long serialVersionUID = -2284879212465893870L;
  39. /**
  40. * Constructs a new Canvas.
  41. */
  42. public Canvas() {
  43. }
  44. /**
  45. * Constructs a new Canvas given a GraphicsConfiguration object.
  46. *
  47. * @param config a reference to a GraphicsConfiguration object.
  48. *
  49. * @see GraphicsConfiguration
  50. */
  51. public Canvas(GraphicsConfiguration config) {
  52. this();
  53. graphicsConfig = config;
  54. }
  55. /**
  56. * Construct a name for this component. Called by getName() when the
  57. * name is null.
  58. */
  59. String constructComponentName() {
  60. synchronized (getClass()) {
  61. return base + nameCounter++;
  62. }
  63. }
  64. /**
  65. * Creates the peer of the canvas. This peer allows you to change the
  66. * user interface of the canvas without changing its functionality.
  67. * @see java.awt.Toolkit#createCanvas(java.awt.Canvas)
  68. * @see java.awt.Component#getToolkit()
  69. */
  70. public void addNotify() {
  71. synchronized (getTreeLock()) {
  72. if (peer == null)
  73. peer = getToolkit().createCanvas(this);
  74. super.addNotify();
  75. }
  76. }
  77. /**
  78. * This method is called to repaint this canvas. Most applications
  79. * that subclass <code>Canvas</code> should override this method in
  80. * order to perform some useful operation.
  81. * <p>
  82. * The <code>paint</code> method provided by <code>Canvas</code>
  83. * redraws this canvas's rectangle in the background color.
  84. * <p>
  85. * The graphics context's origin (0, 0) is the top-left corner
  86. * of this canvas. Its clipping region is the area of the context.
  87. * @param g the graphics context.
  88. * @see java.awt.Graphics
  89. */
  90. public void paint(Graphics g) {
  91. g.setColor(getBackground());
  92. g.fillRect(0, 0, width, height);
  93. }
  94. boolean postsOldMouseEvents() {
  95. return true;
  96. }
  97. }