home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / ADA / LOVELACE / doodle.ada < prev    next >
Text File  |  1996-10-01  |  3KB  |  84 lines

  1.  
  2. -- "Doodle" by David A. Wheeler
  3.  
  4. with java.applet.Applet; use java.applet.Applet;
  5. with java.awt.Graphics; use java.awt.Graphics;
  6. with java.awt.Event; use java.awt.Event;
  7. with java.util.Vector; use java.util.Vector;
  8.  
  9. package Doodle is
  10.   type Doodle_Obj is new Applet_Obj with private;
  11.   type Doodle_Ptr is access all Doodle_Obj'Class;
  12.  
  13.   procedure init(Obj : access Doodle_Obj);
  14.   function  mouseDown(Obj : access Doodle_Obj; e : Event_Ptr;
  15.                       x : Integer ; y : Integer) return Boolean;
  16.   function  mouseDrag(Obj : access Doodle_Obj; evt : Event_Ptr;
  17.                       x : Integer ; y : Integer) return Boolean;
  18.   procedure paint(Obj : access Doodle_Obj; g : Graphics_Ptr);
  19.  
  20. private
  21.   type Doodle_Obj is new Applet_Obj with record
  22.      -- Last position drawn:
  23.      Last_X, Last_Y  : Integer := 0;
  24.      -- Store all lines drawn so they can be redrawn:
  25.      Starting_Points, Ending_Points : Vector_Ptr := null;
  26.   end record;
  27. end Doodle;
  28.  
  29.  
  30.  
  31. with interfaces.Java;  use interfaces.Java; -- for "+" on strings
  32. with java.awt.Point; use java.awt.Point;
  33. with java.lang; use java.lang;  -- Defines "Object"
  34.  
  35. package body Doodle is
  36.  
  37.   procedure init(Obj : access Doodle_Obj) is
  38.   begin
  39.     -- Initialize applet by setting up vector of start and end points.
  40.     Obj.Starting_Points := new_Vector(100, 100); -- Constructor.
  41.     Obj.Ending_Points   := new_Vector(100, 100);
  42.   end init;
  43.  
  44.   function mouseDown(Obj : access Doodle_Obj; e : Event_Ptr;
  45.                      x : Integer ; y : Integer) return Boolean is
  46.     -- Memorize where the button was pressed for later use.
  47.   begin
  48.     Obj.Last_X := X;
  49.     Obj.Last_Y := Y;
  50.     return True;  -- "true" means we've handled mouseDown.
  51.   end mouseDown;
  52.  
  53.   function mouseDrag(Obj : access Doodle_Obj; evt : Event_Ptr;
  54.                      x : Integer ; y : Integer) return Boolean is
  55.     Graphic_Image      : Graphics_Ptr := getGraphics(Obj);
  56.     Starting_Point : Point_Ptr := new_Point(Obj.Last_X, Obj.Last_Y);
  57.     Ending_Point   : Point_Ptr := new_Point(x, y);
  58.     -- Draw a line from last to current point, then store information.
  59.   begin
  60.     drawLine(Graphic_Image, Obj.Last_X, Obj.Last_Y, x, y);
  61.     -- Store the ending position in case the dragging continues.
  62.     Obj.Last_X := x;
  63.     Obj.Last_Y := y;
  64.     -- Store the line drawn so it can be repainted.
  65.     addElement(Obj.Starting_Points, Object_Ptr(Starting_Point));
  66.     addElement(Obj.Ending_Points,   Object_Ptr(Ending_Point));
  67.     return True;  -- "true" means we've handled mouseDrag.
  68.   end mouseDrag;
  69.  
  70.   procedure paint(Obj : access Doodle_Obj; g : Graphics_Ptr) is
  71.     Starting_Point, Ending_Point : Point_Ptr := null;
  72.       -- Redraw the doodling.
  73.   begin
  74.     for I in 0 .. size(Obj.Starting_Points) - 1 loop
  75.       Starting_Point := Point_Ptr(elementAt(Obj.Starting_Points, I));
  76.       Ending_Point   := Point_Ptr(elementAt(Obj.Ending_Points, I));
  77.       drawLine(g, Starting_Point.x, Starting_Point.y,
  78.                   Ending_Point.x, Ending_Point.y);
  79.     end loop;
  80.   end paint;
  81.  
  82. end Doodle;
  83.  
  84.