Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.7 KiB

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. //
  5. //
  6. // ScribbleCanvas-
  7. // This class manages all the events generated on the canvas.
  8. // The class draws the lines on the canvas, creates a Stroke object for each stroke, and notifies the registered
  9. // listeners about it.
  10. // A stoke is a line that is drawn while the the left mouse button is held down.
  11. // The class keeps a list of all strokes drawn (for repaint) in a vector (strokes).
  12. //
  13. //
  14. public class ScribbleC extends Canvas
  15. {
  16. protected Vector strokes = new Vector(5,5);
  17. private Stroke currentStroke = null;
  18. private Point currentPoint = null;
  19. private Vector listeners = new Vector(5,5);
  20. public ScribbleC()
  21. {
  22. // Turns on mouse motion and clicks
  23. enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
  24. }
  25. public void processMouseEvent(MouseEvent e)
  26. {
  27. // Handle clicks
  28. if(e.getID() == e.MOUSE_PRESSED)
  29. {
  30. currentStroke = new Stroke();
  31. currentPoint = e.getPoint();
  32. currentStroke.addElement(currentPoint);
  33. }
  34. else if(e.getID() == e.MOUSE_RELEASED)
  35. {
  36. currentStroke.addElement(e.getPoint());
  37. strokes.addElement(currentStroke);
  38. for(Enumeration enum = listeners.elements(); enum.hasMoreElements(); )
  39. {
  40. ScribbleL sl = (ScribbleL)enum.nextElement();
  41. sl.strokeCreated(currentStroke);
  42. }
  43. currentStroke = null;
  44. currentPoint = null;
  45. }
  46. }
  47. public void processMouseMotionEvent(MouseEvent e)
  48. {
  49. // Handle motion
  50. if(e.getID() == e.MOUSE_DRAGGED)
  51. {
  52. if(currentStroke != null)
  53. {
  54. Point newPoint = e.getPoint();
  55. Graphics g = getGraphics();
  56. g.drawLine(currentPoint.x, currentPoint.y, newPoint.x, newPoint.y);
  57. currentPoint = newPoint;
  58. currentStroke.addElement(newPoint);
  59. }
  60. }
  61. }
  62. public void paint(Graphics g)
  63. {
  64. // Enumerate through strokes and paint them.
  65. for(Enumeration e = strokes.elements(); e.hasMoreElements(); )
  66. {
  67. Stroke stroke = (Stroke)e.nextElement();
  68. drawStroke(g, stroke);
  69. }
  70. }
  71. public void drawStroke(Graphics g, Stroke stroke)
  72. {
  73. Enumeration points = stroke.elements();
  74. if(points.hasMoreElements())
  75. {
  76. for(Point currPoint = (Point)points.nextElement() ; points.hasMoreElements(); )
  77. {
  78. Point tempPoint = (Point)points.nextElement();
  79. g.drawLine(currPoint.x, currPoint.y, tempPoint.x, tempPoint.y);
  80. currPoint = tempPoint;
  81. }
  82. }
  83. }
  84. public void addScribbleListener(ScribbleL l)
  85. {
  86. listeners.addElement(l);
  87. }
  88. public void removeScribbleListener(ScribbleL l)
  89. {
  90. listeners.removeElement(l);
  91. }
  92. public void addStroke(Stroke stroke)
  93. {
  94. strokes.addElement(stroke);
  95. Graphics g = getGraphics();
  96. drawStroke(g, stroke);
  97. }
  98. public void clear()
  99. {
  100. strokes = new Vector(5,5);
  101. repaint();
  102. }
  103. }