/* FILE NAME: Balloon.java * AUTHOR: ?? * DATE: ?? * COMMENTS: Builds an animation of balloons moving up and down in a given space. * * MODIFICATION HISTORY: Stella, Nov 07 * Added "stuck" instance var, and re-wrote the updateState() */ import java.awt.*; class Balloon extends Sprite { private int x; //the x-coordinate of the center of this ballon private int y; //the y-coordinate of the center of this ballon private int radius; private Color color; private boolean up; // true if balloon is going up private int d; // move increment private int timeStuck; // how long (number of frames) //the balloon will be stuck at the ceiling // add any other instance variable(s) that you need here: private int stuckCounter; //for how many *more* frames the ballon //will be stuck at the ceiling. Starts out the same as "timeStuck", //and gets reduced by 1 after every frame. private boolean stuck; //stuck on the ceiling public Balloon(int x, int radius, Color color, int d, int timeStuck) { this.x = x; this.y = height - radius; //the ballon will start resting on the floor this.radius = radius; this.color = color; this.stuckCounter = this.timeStuck = timeStuck; this.d = d; this.up = true; this.stuck = false; } public void drawState(Graphics g) { g.setColor(color); g.fillOval(x - radius, y - radius, 2*radius, 2*radius); // adding a word on each balloon in white g.setColor(Color.red); Font myfont = new Font("Chalkboard", Font.BOLD, 14); g.setFont(myfont); g.drawString("Hi",x-3,y); g.setColor(color); } public void updateState() { //Balloon is going up if (up) { y = y - d; //try to move it up by d if (y < radius) { //check to see whether this would move the balloon //too far to the North. y = radius; //in that case, just stick it to the ceiling } //at some point, the balloon will get stuck to the ceiling if (y == radius) { up = false; stuck = true; } } //Balloon is stuck to the ceiling if (stuck) { if (stuckCounter > 0) //it should still stay stuck stuckCounter = stuckCounter - 1; else //it should start coming down stuck = false; } //Ballon is on its way down if (!up && !stuck) { y = y + d; //try to move it down by d if (y > height-radius) // that would move the ballon too far to the South y = height - radius; //correct the y, so the balloon rests on the floor } } // resetState() is invoked to put the sprite into the initial position public void resetState() { y = height - radius; //place the ballon so it rests on the floor. up = true; //ready to go up stuck = false; stuckCounter = timeStuck; } // For debugging: public String className() { return "Balloon"; } public String toString() { return className() + "[" + "x=" + x + "; y=" + y + "; radius=" + radius + "; color=" + color + "; up=" + up + "; d=" + d + "; timeStuck=" + timeStuck + "]"; } } // class Balloon