1  /* MovableObject.java */
  2  
  3  import java.awt.*;
  4  
  5  // This class is "abstract" since it defines abstract methods:
  6  public abstract class MovableObject extends Point {
  7  
  8    // The Point class defines public variables  x  and  y .
  9    
 10    // Instance variables:
 11    protected int dx, dy;    // displacement of object
 12    protected Color color;   // color of object
 13    
 14    // Constructor:
 15    public MovableObject( int new_x, int new_y ) {
 16      // Invoke the constructor of the superclass:
 17      super( new_x, new_y );
 18      dx = 1; dy = 1;
 19      color = Color.black;
 20    }
 21    
 22    // In addition to the following accessors and mutators,
 23    // the Point class also defines methods getLocation(),
 24    // setLocation( int, int ), and setLocation( Point ):
 25    public int getX() { return x; }
 26    public int getY() { return y; }
 27    public void setDelta( int new_dx, int new_dy ) {
 28      dx = new_dx; dy = new_dy;
 29    }
 30    public Color getColor() { return color; }
 31    public void setColor( Color new_color ) {
 32      color = new_color;
 33    }
 34    
 35    // The Point class defines a method translate( int, int ).
 36    // Here we define an alternative form of that method:
 37    public void translate() {
 38      x += dx; y += dy;
 39    }
 40    
 41    // Class methods to be implemented by subclasses:
 42    public abstract void paint( Graphics g );
 43    public abstract void checkBoundary( Rectangle rect );
 44    public abstract boolean contains( int some_x, int some_y );
 45  
 46  } // end of MovableObject class
 47