// BlueBug.java
// User-defined class BlueBug.java
// Subclass of Bug
// Shows dynamic method binding and polymorphism
import java.awt.*;
public class BlueBug extends Bug {
// instance variables
private Triangle t; // shape of body
private int s; // speed of crawl
private int x, y; // x and y coordinates of head
private int w, h; // body width and height
private int l, f; // leg and foot size
// constructor
public BlueBug( String name, int speed, Triangle tri, int leg, int foot ) {
super( name );
s = speed;
t = tri;
l = leg;
f = foot;
}
public void draw( Graphics g ) {
x = t.getX( );
y = t.getY( );
w = t.getW( );
h = t.getH( );
g.setColor( Color.black );
for ( int i = 1; i <= 2; i ++ ) {
g.drawLine( x + i * w / 3 - f, y - i * h / 6 - l , x + i * w / 3, y - i * h / 6 - l ); // top foot
g.drawLine( x + i * w / 3, y - i * h / 6 - l , x + i * w / 3, y + i * h / 6 + l ); // long line
g.drawLine( x + i * w / 3 - f, y + i * h / 6 + l , x + i * w / 3, y + i * h / 6 + l ); // bottom foot
}
g.drawLine( x - 3, y - 3, x, y );
g.drawLine( x - 3, y + 3, x, y );
g.setColor( Color.red );
g.fillOval( x - 4, y - 4, 3, 3 );
g.fillOval( x - 4, y + 4, 3, 3 );
// draw body
t.fill( g, Color.blue );
g.setColor( Color.black );
}
public void crawl( ) {
x = t.getX( );
y = t.getY( );
w = t.getW( );
h = t.getH( );
x -= s;
t.setPointA( x, y );
t.setPointB( x, y, w, h );
t.setPointC( x, y, w, h );
}
public String toString( ) {
return "The bluebug is named: " + getBugName( );
}
}