1  /* MovableTriangle.java */
  2  
  3  import java.awt.*;
  4  
  5  public class MovableTriangle extends MovableRectangle {
  6  
  7    public MovableTriangle( int new_x, int new_y, int new_w, int new_h ) {
  8      // Call MovableRectangle's constructor:
  9      super( new_x, new_y, new_w, new_h );
 10    }
 11    
 12    // Override MovableRectangle.paint( Graphics ):
 13    public void paint( Graphics g ) {
 14      // Compute the vertices of the triangle:
 15      int xpoints[] = { x, x + w, x + w/2 };
 16      int ypoints[] = { y + h, y + h, y };
 17      // Draw the triangle:
 18      g.setColor( color );
 19      g.fillPolygon( xpoints, ypoints, 3 );
 20    }
 21    
 22    // Override MovableRectangle.contains( int, int ):
 23    public boolean contains( int some_x, int some_y ) {
 24      // Compute the vertices of the triangle:
 25      int xpoints[] = { x, x + w, x + w/2 };
 26      int ypoints[] = { y + h, y + h, y };
 27      // Construct a polygon:
 28      Polygon p = new Polygon( xpoints, ypoints, 3 );
 29      return p.contains( some_x, some_y );
 30    }
 31  
 32  }