| JRobo.java |
// JRobo Basic JRobo class
// Author: David 4/9/99
//
// Version: 1.1 Added name & datOfBirth properties
// plus accessor methods. Constructor modified.
// Author: David 4/9/99
// 1.0 Basic f, r, l, p
import java.awt.*;
import java.util.Date; // added v1.1
public class JRobo
{
String name; // added v1.1
Date dateOfBirth; // added v1.1
int x; // current x, y location
int y;
boolean draw; // pen up/down
int heading; // direction - in degrees
Graphics robog; // keep info about where to draw
// v1.0 Constructor
// JRobo( Graphics g ) {
// v1.1 Constructor
// Adds given name & current date of birth
public JRobo ( Graphics g, String theName )
{
name = theName;
dateOfBirth = new Date( );
x = 200;
y = 150;
draw = true;
heading = 0;
robog = g;
}
// r - Turn Right
public void r( int degrees )
{
heading = ( heading + degrees ) % 360;
}
// l - Turn Left
public void l( int degrees )
{
heading = ( heading - degrees ) % 360;
}
// f - Move forward
public void f( int distance )
{
int dx, dy;
dx = ( int ) Math.round( distance * Math.sin( heading * 2 * 3.142 / 360 ) * 500/1000 );
dy = ( int ) Math.round( distance * -Math.cos( heading * 2 * 3.142 / 360 ) * 500/1000 );
if ( draw )
robog.drawLine( x, y, x + dx, y + dy );
x = x + dx;
y = y + dy;
}
// p - Change pen state
public void p( )
{
draw = !draw;
}
// v1.1 methods for new properties follow...
public void sayName( )
{
System.out.println( name );
}
public void setName( String newName )
{
name = newName;
}
public String getName( )
{
return name;
}
public Date getDateOfBirth( )
{
return dateOfBirth;
}
} // end JRobo class