1   // JRobo    Basic JRobo class
2   // Author:  David 4/9/99
3   
4   import java.awt.*;
5   
6   public class JRobo
7   {
8   
9       int x;          // current x, y location
10      int y;
11      boolean draw;   // pen up/down
12      int heading;    // direction - in degrees
13  
14      Graphics robog; // keep info about where to draw
15  
16      // Constructor
17      JRobo( Graphics g ) {
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/1000 );
42          dy = ( int ) Math.round( distance * -Math.cos( heading * 2 * 3.142 / 360 ) * 500/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