// GreenBug.java // User-defined class GreenBug.java // Shows dynamic method binding and polymorphism import java.awt.*; public class GreenBug extends Bug { private int s; // speed of crawl private int x, y; // x and y coordinates of first body segment private int w; // body segment width private int l, f; // leg and foot size private int seg; // number of body segments public GreenBug( String name, int speed, int xCoord, int yCoord, int width, int leg, int foot, int segments ) { super( name ); s = speed; x = xCoord; y = yCoord; w = width; l = leg; f = foot; seg = segments; } public void draw( Graphics g ) { g.setColor( Color.black ); for ( int i = 1; i < seg; i ++ ) { g.drawLine( x + i * w - f, y - l, x + i * w, y - l ); // top foot g.drawLine( x + i * w, y - l, x + i * w, y + w + l ); // long line g.drawLine( x + i * w - f, y + w + l, x + i * w, y + w + l ); // bottom foot } g.drawLine( x - 3, y + w / 2 - 7, x + 1, y + w / 2 - 2 ); g.drawLine( x - 3, y + w / 2 + 7, x + 1, y + w / 2 + 2 ); g.setColor( Color.red ); g.fillOval( x - 4, y + w / 2 - 8, 3, 3 ); g.fillOval( x - 4, y + w / 2 + 6, 3, 3 ); // draw body segments g.setColor( Color.green ); for ( int i = 0; i < seg; i++ ) { g.fillOval( x + i * w, y, w, w ); } g.setColor( Color.black ); } public void crawl( ) { x -= s; } public String toString( ) { return "The centipede is named: " + getBugName( ); } }