// Lab 3: WorldDemo.ijava sample program // Show how to use the World class to produce an animation import draw.*; import colors.*; import geometry.*; // a class to represent a world with one dying red sun class MiniWorld extends World{ int x; MiniWorld(int x){ this.x = x; } // start the world and the timer boolean go() { return this.bigBang(100,100,1.0); } // make the world grow with the timer World onTick() { return new MiniWorld(x + 2); } // decrease the size of the sun when "d" is pressed World onKeyEvent(String ke){ if (ke.equals("d")) // decrease the radius of the circle return new MiniWorld(this.x - 3); else if (ke.equals("i")) // increase the radius of the circle return new MiniWorld(this.x + 3); else return this; } // draw the sun boolean draw(){ return this.theCanvas.drawDisk(new Posn(50, 50), this.x, new Red()); } // erase the sun - clear the canvas boolean erase(){ return this.theCanvas.drawRect(new Posn(0, 0), 200, 200, new White()); } } class Examples{ Examples() {} // define a new world MiniWorld mw = new MiniWorld(30); // test the method onTick in the class MiniWorld boolean testOnTick = check this.mw.onTick() expect new MiniWorld(32); // test the method onKeyEvent in the class MiniWorld boolean testOnKeyEvent = (check this.mw.onKeyEvent("x") expect this.mw) && (check this.mw.onKeyEvent("d") expect new MiniWorld(27)) && (check this.mw.onKeyEvent("i") expect new MiniWorld(33)) && (check this.mw.onKeyEvent("left") expect this.mw); // start our world --- by displaying the canvas, // starting the timer // and responding to key events boolean run = this.mw.go(); }