1   // JRobo    Basic class giving f, r, l, p
2   // Author:  David 4/9/99
3   
4   import java.awt.*;
5   
6   public class JRobo
7   {
8       int x;          // current x, y location
9       int y;
10      boolean draw;   // pen up/down
11      int heading;    // direction - in degrees
12  
13      Graphics robog; // keep info about where to draw
14  
15      // Constructor
16      JRobo( Graphics g )
17      {
18          x = 200;
19          y = 150;
20          draw = true;
21          heading = 0;
22          robog = g;
23      }
24  
25      // r - Turn Right
26      public void r( int degrees )
27       {
28            heading = ( heading + degrees ) % 360;
29      }
30  
31      // l - Turn Left
32      public void l( int degrees )
33      {
34            heading = ( heading - degrees ) % 360;
35      }
36  
37      // f - Move forward
38      public void f( int distance )
39      {
40          int dx, dy;
41          dx = ( int ) Math.round( distance * Math.sin( heading * 2 * 3.142 / 360 ) * 500.0/1000 );
42          dy = ( int ) Math.round( distance * - Math.cos( heading * 2 * 3.142 / 360 ) * 500.0/1000 );
43          if ( draw )
44               robog.drawLine( x,  y,  x + dx, y + dy );
45          x = x + dx;
46          y = y + dy;
47      }
48  
49      // p - Change pen state
50      public void p()
51      {
52          draw = ! draw;
53      }
54  
55  } // end JRobo class
56