import draw.*; import colors.*; import geometry.*; import java.util.Random; class Star { Posn loc; private int dx; private int dy; Random r = new Random(); private Star(Posn loc, int dx, int dy) { this.loc = loc; this.dx = dx; this.dy = dy; } public Star() { this.loc = new Posn(100,0); this.dx = this.randIn(-10,10); this.dy = this.randIn(-10,10); } // Generate a random number in [i,j]. int randIn(int i, int j) { return i + r.nextInt() % ((j-i)+1) ; } Star move() { return new Star(new Posn(loc.x+dx, loc.y+dy), dx, dy); } boolean draw(Canvas c) { return c.drawDisk(this.loc, 4, new White()); } } // to represent shooting stars in our world interface LoStars { // move the stars in this list down LoStars move(); // draw the stars in this list boolean draw(Canvas x); } // to represent an empty list of stars class MTLoStars implements LoStars { MTLoStars() {} // move the stars in this list down LoStars move() { return this; } // draw the stars in this list boolean draw(Canvas x) { return true; } } // to represent a nonempty list of stars class ConsLoStars implements LoStars { Star first; LoStars rest; ConsLoStars(Star first, LoStars rest) { this.first = first; this.rest = rest; } // move the stars in this list down LoStars move() { return new ConsLoStars(this.first.move(), this.rest.move()); } // draw the stars in this list boolean draw(Canvas c) { return this.first.draw(c) && this.rest.draw(c); } } // the world of shooting stars class StarWorld extends World { LoStars stars; StarWorld(LoStars stars){ this.stars = stars; } World onTick(){ return new StarWorld(new ConsLoStars(new Star(), this.stars.move())); } World onKeyEvent(String ke){ return this; } boolean draw(){ return theCanvas.drawRect(new Posn(0, 0), 200, 300, new Blue()) && this.stars.draw(this.theCanvas); } boolean erase(){ return theCanvas.drawRect(new Posn(0, 0), 200, 300, new Blue()); } } class Examples { StarWorld sw = new StarWorld(new MTLoStars()); boolean go = sw.bigBang(200, 300, 0.1); }